Skip to content

Conversation

@stevebauman
Copy link
Contributor

@stevebauman stevebauman commented Jan 15, 2025

This PR removes duplicate logic that exists in FormRequest and Fluent by leveraging the InteractsWithData trait.

This also exposes the ValidatedInput class to its additional helpful methods (and any others that may be added to it in the future), such as the enums method.

Comment on lines +47 to 63
* @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;
}
Copy link
Contributor

@shaedrich shaedrich Jan 15, 2025

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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:

/**
* 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:

/**
* 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;
}

Copy link
Contributor

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 👍🏻

Copy link
Contributor Author

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...

Copy link
Contributor

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

@taylorotwell taylorotwell merged commit 7594f44 into laravel:11.x Jan 15, 2025
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants