From 9a02b3c3dd70241c1c5bd7178ce35a75360d6fd7 Mon Sep 17 00:00:00 2001 From: mychidarko Date: Mon, 16 Sep 2024 17:57:47 +0000 Subject: [PATCH] feat: add query and getOrDefault methods --- src/Request.php | 69 +++++++++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 25 deletions(-) diff --git a/src/Request.php b/src/Request.php index e57f258..530b010 100755 --- a/src/Request.php +++ b/src/Request.php @@ -111,29 +111,6 @@ public static function input(bool $safeData = true) return $safeData ? \Leaf\Anchor::sanitize($data) : $data; } - /** - * Fetch GET and POST data - * - * This method returns a union of GET and POST data as a key-value array, or the value - * of the array key if requested. If the array key does not exist, NULL is returned, - * unless there is a default value specified. - * - * @param string|null $key - * @param mixed|null $default - * - * @return mixed - */ - public static function params(string $key = null, $default = null) - { - $union = static::body(); - - if ($key) { - return $union[$key] ?? $default; - } - - return $union; - } - /** * Attempt to retrieve data from the request. * @@ -187,6 +164,17 @@ public static function urlData($item = null, $default = null) return \Leaf\Anchor::deepGet($_GET, $item) ?? $default; } + /** + * Return only get request data + * + * @param string|array $item The items to output + * @param mixed $default The default value to return if no data is available + */ + public static function query($item = null, $default = null) + { + return \Leaf\Anchor::deepGet($_GET, $item) ?? $default; + } + /** * Return only get request data * @@ -201,8 +189,7 @@ public static function postData($item = null, $default = null) /** * Returns request data * - * This method returns data passed into the request (request or form data). - * This method returns get, post, put patch, delete or raw faw form data or NULL + * This method returns get, post, put patch, delete or raw form data or NULL * if the data isn't found. * * @param array|string $params The parameter(s) to return @@ -223,6 +210,38 @@ public static function get($params, bool $safeData = true) return $data; } + /** + * Returns request data + * + * This method returns get, post, put patch, delete or raw form data or NULL + * if the data isn't found. + * + * @param string|null $key + * @param mixed|null $default + * + * @return mixed + */ + public static function params(string $key = null, $default = null) + { + return static::get($key) ?? $default; + } + + /** + * Returns request data + * + * This method returns get, post, put patch, delete or raw form data or NULL + * if the data isn't found. + * + * @param string|null $key + * @param mixed|null $default + * + * @return mixed + */ + public static function getOrDefault(string $key = null, $default = null) + { + return static::get($key) ?? $default; + } + /** * Get all the request data as an associative array *