[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.
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.
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.
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
And UriQueryString:
framework/src/Illuminate/Support/UriQueryString.php
Lines 22 to 43 in cf7886b
There was a problem hiding this comment.
Ah, okay, I see 😬 Thanks for the clarification 👍🏻
There was a problem hiding this comment.
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.
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.