From 06c2b6eddf1ddc93bd88d84a33b7411638507a04 Mon Sep 17 00:00:00 2001 From: bim-g Date: Thu, 25 Apr 2024 22:20:22 +0200 Subject: [PATCH] [ENH] refactor http input to trigger each specific action, from get,post and files --- controller/exampleController.php | 2 +- middleware/Validation/exampleValidation.php | 2 +- src/Core/Http/Input.php | 63 ++++++++++++++------- 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/controller/exampleController.php b/controller/exampleController.php index 6dceb06..ae2031f 100644 --- a/controller/exampleController.php +++ b/controller/exampleController.php @@ -36,7 +36,7 @@ function home(): void */ function changeLang(): void { - Session::put('lang', Input::get("lang")); + Session::put('lang', Input::post("lang")); Redirect::to("/"); } } \ No newline at end of file diff --git a/middleware/Validation/exampleValidation.php b/middleware/Validation/exampleValidation.php index b7a4958..1aaa7e5 100644 --- a/middleware/Validation/exampleValidation.php +++ b/middleware/Validation/exampleValidation.php @@ -11,7 +11,7 @@ function changeLang() $schema = [ "token" => $this->rule->string("token") ->min(5) - ->max(30) + ->max(50) ->required(), "lang" => $this->rule->string("lang") ->min(1) diff --git a/src/Core/Http/Input.php b/src/Core/Http/Input.php index 1551763..8c4e840 100644 --- a/src/Core/Http/Input.php +++ b/src/Core/Http/Input.php @@ -7,10 +7,11 @@ class Input { /** + * check if the submission form is a port action * @param $type * @return bool */ - public static function exists($type = "POST") + public static function exists($type = "POST"): bool { switch ($type) { case "POST": @@ -41,51 +42,46 @@ private static function put(): array } /** + * extract data from submit form as form data * @param $file_input - * @return array|string[] + * @return array */ - private static function extractFromFormData($file_input) + private static function extractFromFormData($file_input): array { - $fragma = []; + $form_data_fragmentation = []; $explode = explode("\r", implode("\r", explode("\n", $file_input))); $len_Arr = count($explode); for ($i = 1; $i < $len_Arr; $i++) { if (!strchr($explode[$i], "----------------------------")) { if (strlen($explode[$i]) > 0) { $replaced = str_replace("Content-Disposition: form-data; name=", "", $explode[$i]); - array_push($fragma, $replaced); + $form_data_fragmentation[] = $replaced; } } } - $len_object = count($fragma); + $len_object = count($form_data_fragmentation); $object = []; for ($j = 0; $j < $len_object; $j++) { if ($j == 0 || ($j + 1) % 2 != 0) { - $key = str_replace("\"", "", $fragma[$j]); - $object = array_merge($object, [$key => trim($fragma[($j + 1)])]); + $key = str_replace("\"", "", $form_data_fragmentation[$j]); + $object = array_merge($object, [$key => trim($form_data_fragmentation[($j + 1)])]); } } return $object; } /** + * Access data information from $_GET action of url request * @param $item * @return mixed|null */ static function get($item) { - $object_data = self::put(); - if (isset($_POST[$item])) { - return $_POST[$item]; - } else if (isset($_GET[$item])) { - return $_GET[$item]; - } else if (isset($object_data[$item])) { - return $object_data[$item]; - } - return null; + return $_GET[$item] ?? null; } /** + * Access HEADER data information from header * Extract header data information * @param $item * @return mixed|null @@ -100,10 +96,35 @@ public static function header($item) } /** - * @return array|null + * Lists params submitted via POST, PATCH or PUT action event + * @return array */ - public static function body() + public static function body(): ?array { - return isset($_POST) && !empty($_POST) ? $_POST : !empty(self::put() ? self::put() : null); + return !empty($_POST) ? $_POST : (!empty(self::put()) ? self::put() : null); + } + + /** + * Access data information from POST or PUT action event + * @param $item + * @return mixed|string|null + */ + public static function post($item){ + $self_put = self::put(); + if (isset($_POST[$item])) { + return $_POST[$item]; + } else if (isset($self_put[$item])) { + return $self_put[$item]; + } + return null; + } + + /** + * Access data information from file upload event + * @param string $item + * @return mixed|null + */ + public static function file(string $item) { + return $_FILES[$item] ?? null; } -} \ No newline at end of file +}