Skip to content

Commit

Permalink
Adds request merge and mergeifmissing (#38)
Browse files Browse the repository at this point in the history
* Adds Request::merge() and Request::mergeIfMissing()

* Improves IDE
  • Loading branch information
huangdijia authored Sep 29, 2022
1 parent 7998687 commit 4f86b4d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,24 @@
* @document https://github.com/friendsofhyperf/components/blob/3.x/README.md
* @contact [email protected]
*/
namespace Hyperf\HttpServer;
namespace Hyperf\HttpServer\Contract;

class Request
interface RequestInterface
{
/**
* Get an array of all of the files on the request.
*
* @return array
*/
public function allFiles()
{
}
public function allFiles();

/**
* Determine if the request contains a non-empty value for any of the given inputs.
*
* @param array|string $keys
* @return bool
*/
public function anyFilled($keys)
{
}
public function anyFilled($keys);

/**
* Retrieve input as a boolean value.
Expand All @@ -40,19 +36,15 @@ public function anyFilled($keys)
* @param bool $default
* @return bool
*/
public function boolean($key = null, $default = false)
{
}
public function boolean($key = null, $default = false);

/**
* Retrieve input from the request as a collection.
*
* @param null|array|string $key
* @return \Hyperf\Utils\Collection
*/
public function collect($key = null)
{
}
public function collect($key = null);

/**
* Retrieve input from the request as a Carbon instance.
Expand All @@ -62,142 +54,126 @@ public function collect($key = null)
* @param null|string $tz
* @return null|\Carbon\Carbon
*/
public function date($key, $format = null, $tz = null)
{
}
public function date($key, $format = null, $tz = null);

/**
* Get all of the input except for a specified array of items.
*
* @param array|mixed $keys
* @return array
*/
public function except($keys)
{
}
public function except($keys);

/**
* Determine if the request contains a non-empty value for an input item.
*
* @param array|string $key
* @return bool
*/
public function filled($key)
{
}
public function filled($key);

/**
* Determine if the request contains any of the given inputs.
*
* @param array|string $keys
* @return bool
*/
public function hasAny($keys)
{
}
public function hasAny($keys);

/**
* Determine if the given input key is an empty string for "has".
*
* @param string $key
* @return bool
*/
public function isEmptyString($key)
{
}
public function isEmptyString($key);

/**
* Determine if the request contains an empty value for an input item.
*
* @param array|string $key
* @return bool
*/
public function isNotFilled($key)
{
}
public function isNotFilled($key);

/**
* Get the keys for all of the input and files.
*
* @return array
*/
public function keys()
{
}
public function keys();

/**
* Get the host name.
*
* @return string
*/
public function host()
{
}
public function host();

/**
* Get the HTTP host being requested.
*
* @return string
*/
public function httpHost()
{
}
public function httpHost();

/**
* Get the scheme and HTTP host.
*
* @return string
*/
public function schemeAndHttpHost()
{
}
public function schemeAndHttpHost();

/**
* Merge new input into the current request's input array.
*
* @return $this
*/
public function merge(array $input);

/**
* Merge new input into the request's input, but only when that key is missing from the request.
*
* @return $this
*/
public function mergeIfMissing(array $input);

/**
* Determine if the request is missing a given input item key.
*
* @param array|string $key
* @return bool
*/
public function missing($key)
{
}
public function missing($key);

/**
* Get a subset containing the provided keys with values from the input data.
*
* @param array|mixed $keys
* @return array
*/
public function only($keys)
{
}
public function only($keys);

/**
* Apply the callback if the request contains a non-empty value for the given input item key.
*
* @param string $key
* @return $this|mixed
*/
public function whenFilled($key, callable $callback, callable $default = null)
{
}
public function whenFilled($key, callable $callback, callable $default = null);

/**
* Apply the callback if the request contains the given input item key.
*
* @param string $key
* @return $this|mixed
*/
public function whenHas($key, callable $callback, callable $default = null)
{
}
public function whenHas($key, callable $callback, callable $default = null);

/**
* Determine if the request is sending JSON.
*
* @return bool
*/
public function isJson()
{
}
public function isJson();
}
15 changes: 15 additions & 0 deletions src/request.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @contact [email protected]
*/
use Carbon\Carbon;
use Hyperf\Context\Context;
use Hyperf\HttpServer\Request;
use Hyperf\Utils\Arr;
use Hyperf\Utils\Str;
Expand Down Expand Up @@ -154,6 +155,20 @@
});
}

if (! Request::hasMacro('merge')) {
Request::macro('merge', function (array $input) {
Context::override($this->contextkeys['parsedData'], fn ($inputs) => array_replace((array) $inputs, $input));

return $this;
});
}

if (! Request::hasMacro('mergeIfMissing')) {
Request::macro('mergeIfMissing', function (array $input) {
return $this->merge(collect($input)->filter(fn ($value, $key) => $this->missing($key))->toArray());
});
}

if (! Request::hasMacro('missing')) {
Request::macro('missing', function ($key) {
$keys = is_array($key) ? $key : func_get_args();
Expand Down

0 comments on commit 4f86b4d

Please sign in to comment.