Skip to content

Commit

Permalink
feat: add query and getOrDefault methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Sep 16, 2024
1 parent b413eb9 commit 9a02b3c
Showing 1 changed file with 44 additions and 25 deletions.
69 changes: 44 additions & 25 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
*
Expand All @@ -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
Expand All @@ -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
*
Expand Down

0 comments on commit 9a02b3c

Please sign in to comment.