From 583e45b31cf7a7cb65384934014b50d5bfc638d9 Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Tue, 12 Dec 2023 22:25:41 +0200 Subject: [PATCH] type orianted methods and responses --- server/autoloader.php | 10 +++++++ server/index.php | 14 ++++++++++ .../urlManipulator/AbstractUrlManipulator.php | 15 +++++++++++ .../ManipulationResponseInterface.php | 0 .../UrlManipulator.php} | 26 +++++++------------ web-client/script.js | 2 +- 6 files changed, 49 insertions(+), 18 deletions(-) create mode 100644 server/autoloader.php create mode 100644 server/index.php create mode 100644 server/urlManipulator/AbstractUrlManipulator.php create mode 100644 server/urlManipulator/ManipulationResponseInterface.php rename server/{process.php => urlManipulator/UrlManipulator.php} (73%) diff --git a/server/autoloader.php b/server/autoloader.php new file mode 100644 index 0000000..c2df845 --- /dev/null +++ b/server/autoloader.php @@ -0,0 +1,10 @@ +manipulateUrl($url); + + header('Content-Type: application/json'); + echo json_encode($result); +} \ No newline at end of file diff --git a/server/urlManipulator/AbstractUrlManipulator.php b/server/urlManipulator/AbstractUrlManipulator.php new file mode 100644 index 0000000..f8013e9 --- /dev/null +++ b/server/urlManipulator/AbstractUrlManipulator.php @@ -0,0 +1,15 @@ + 'Invalid URL', 'input' => $url]; @@ -31,13 +33,10 @@ class UrlManipulator $modifiedUrl = $this->addParamToUrl($url, $newParam, $randomValue); - return [ - 'before' => $url, - 'after' => $modifiedUrl, - ]; + return ['status' => 200, 'data' => $modifiedUrl]; } - private function addParamToUrl($url, $param, $value) + protected function addParamToUrl(string $url, string $param, string $value): string { $separator = (parse_url($url, PHP_URL_QUERY) == null) ? '?' : '&'; @@ -50,12 +49,13 @@ class UrlManipulator return $modifiedUrl; } - private function generateRandomValue() + protected function generateRandomValue(): string { return uniqid(); } - private function getNewParam($existingParams){ + protected function getNewParam(array $existingParams): string | null + { while (!empty($this->paramsArray)) { $newParam = array_pop($this->paramsArray); @@ -69,11 +69,3 @@ class UrlManipulator } -if ($_SERVER['REQUEST_METHOD'] === 'POST') { - $urlManipulator = new UrlManipulator(); - $url = $_POST['url']; - $result = $urlManipulator->manipulateUrl($url); - - header('Content-Type: application/json'); - echo json_encode($result); -} diff --git a/web-client/script.js b/web-client/script.js index a67629f..ca6102c 100644 --- a/web-client/script.js +++ b/web-client/script.js @@ -3,7 +3,7 @@ function manipulateUrl() { $.ajax({ type: 'POST', - url: '../server/process.php', + url: '../server/index.php', data: { url: url }, dataType: 'json', success: function (result) {