-
Couldn't load subscription status.
- Fork 11.6k
[11.x] Replace duplicate ValidatedInput functions with InteractsWithData trait
#54208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[11.x] Replace duplicate ValidatedInput functions with InteractsWithData trait
#54208
Conversation
| * @param array|mixed|null $keys | ||
| * @return array | ||
| */ | ||
| public function all() | ||
| public function all($keys = null) | ||
| { | ||
| return $this->input; | ||
| } | ||
|
|
||
| /** | ||
| * Apply the callback if the validated inputs contains the given input item key. | ||
| * | ||
| * @param string $key | ||
| * @param callable $callback | ||
| * @param callable|null $default | ||
| * @return $this|mixed | ||
| */ | ||
| public function whenHas($key, callable $callback, ?callable $default = null) | ||
| { | ||
| if ($this->has($key)) { | ||
| return $callback(data_get($this->all(), $key)) ?: $this; | ||
| } | ||
|
|
||
| if ($default) { | ||
| return $default(); | ||
| } | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Determine if the validated inputs contains a non-empty value for an input item. | ||
| * | ||
| * @param string|array $key | ||
| * @return bool | ||
| */ | ||
| public function filled($key) | ||
| { | ||
| $keys = is_array($key) ? $key : func_get_args(); | ||
|
|
||
| foreach ($keys as $value) { | ||
| if ($this->isEmptyString($value)) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Determine if the validated inputs contains an empty value for an input item. | ||
| * | ||
| * @param string|array $key | ||
| * @return bool | ||
| */ | ||
| public function isNotFilled($key) | ||
| { | ||
| $keys = is_array($key) ? $key : func_get_args(); | ||
|
|
||
| foreach ($keys as $value) { | ||
| if (! $this->isEmptyString($value)) { | ||
| return false; | ||
| } | ||
| if (! $keys) { | ||
| return $this->input; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Determine if the validated inputs contains a non-empty value for any of the given inputs. | ||
| * | ||
| * @param string|array $keys | ||
| * @return bool | ||
| */ | ||
| public function anyFilled($keys) | ||
| { | ||
| $keys = is_array($keys) ? $keys : func_get_args(); | ||
|
|
||
| foreach ($keys as $key) { | ||
| if ($this->filled($key)) { | ||
| return true; | ||
| } | ||
| } | ||
| $input = []; | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Apply the callback if the validated inputs contains a non-empty value for the given input item key. | ||
| * | ||
| * @param string $key | ||
| * @param callable $callback | ||
| * @param callable|null $default | ||
| * @return $this|mixed | ||
| */ | ||
| public function whenFilled($key, callable $callback, ?callable $default = null) | ||
| { | ||
| if ($this->filled($key)) { | ||
| return $callback(data_get($this->all(), $key)) ?: $this; | ||
| } | ||
|
|
||
| if ($default) { | ||
| return $default(); | ||
| foreach (is_array($keys) ? $keys : func_get_args() as $key) { | ||
| Arr::set($input, $key, Arr::get($this->input, $key)); | ||
| } | ||
|
|
||
| return $this; | ||
| return $input; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this change in behavior of all intentional? I mean, it offers to return "all" but sometimes returns just some. Wouldn't that work better with only?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, otherwise the abstract all definition on the trait and class are incompatible and PHP will throw an exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is also basically the same definition and logic that exists on the InteractsWithInput trait:
framework/src/Illuminate/Http/Concerns/InteractsWithInput.php
Lines 80 to 101 in 764c796
| /** | |
| * Get all of the input and files for the request. | |
| * | |
| * @param array|mixed|null $keys | |
| * @return array | |
| */ | |
| public function all($keys = null) | |
| { | |
| $input = array_replace_recursive($this->input(), $this->allFiles()); | |
| if (! $keys) { | |
| return $input; | |
| } | |
| $results = []; | |
| foreach (is_array($keys) ? $keys : func_get_args() as $key) { | |
| Arr::set($results, $key, Arr::get($input, $key)); | |
| } | |
| return $results; | |
| } |
And UriQueryString:
framework/src/Illuminate/Support/UriQueryString.php
Lines 22 to 43 in cf7886b
| /** | |
| * Retrieve all data from the instance. | |
| * | |
| * @param array|mixed|null $keys | |
| * @return array | |
| */ | |
| public function all($keys = null) | |
| { | |
| $query = $this->toArray(); | |
| if (! $keys) { | |
| return $query; | |
| } | |
| $results = []; | |
| foreach (is_array($keys) ? $keys : func_get_args() as $key) { | |
| Arr::set($results, $key, Arr::get($query, $key)); | |
| } | |
| return $results; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, okay, I see 😬 Thanks for the clarification 👍🏻
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should modify this in Laravel 12 and remove the $keys parameter in favour of a standard only method?
Not sure... Might not be worth the breaking changes...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be right but probably denied
This PR removes duplicate logic that exists in
FormRequestandFluentby leveraging theInteractsWithDatatrait.This also exposes the
ValidatedInputclass to its additional helpful methods (and any others that may be added to it in the future), such as theenumsmethod.