8000 [11.x] Fluent Array validation by Ahmad-Mohammad-Kouja · Pull Request #54517 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[11.x] Fluent Array validation #54517

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 125 additions & 12 deletions src/Illuminate/Validation/Rules/ArrayRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
namespace Illuminate\Validation\Rules;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
use Illuminate\Support\Traits\Conditionable;
use Stringable;

use function Illuminate\Support\enum_value;

class ArrayRule implements Stringable
{
use Conditionable;

/**
* The accepted keys.
*
* @var array
* The constraints for the number rule.
*/
protected $keys;
protected array $constraints = [];

/**
* Create a new array rule instance.
Expand All @@ -28,25 +30,136 @@ public function __construct($keys = null)
$keys = $keys->toArray();
}

$this->keys = is_array($keys) ? $keys : func_get_args();
$keys = is_array($keys) ? $keys : func_get_args();

if ($keys === []) {
return $this->addRule('array');
}

$keys = array_map(
static fn ($key) => enum_value($key),
$keys,
);
Comment on lines +39 to +42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simplify this to a first-class callable:

Suggested change
$keys = array_map(
static fn ($key) => enum_value($key),
$keys,
);
$keys = array_map(enum_value(..), $keys);


return $this->addRule('array:'.implode(',', $keys));
}

/**
* Convert the rule to a validation string.
* The field under validation must have a distinct values.
*
* @param bool $strict
* @return $this
*/
public function distinct(bool $strict = false)
{
return $this->addRule($strict ? 'distinct:strict' : 'distinct');
}

/**
* The field under validation must have size less than or equal to a maximum value.
*
* @param int $max
* @return $this
*/
public function max(int $max)
{
return $this->addRule("max:$max");
}

/**
* The field under validation must have size greater than or equal to a minimum value.
*
* @param int $min
* @return $this
*/
public function min(int $min)
{
return $this->addRule("min:$min");
}

/**
* The field under validation must have a size matching the given value.
*
* @param int $size
* @return $this
*/
public function size(int $size)
{
return $this->addRule("size:$size");
}

/**
* The field under validation must have a size between the given min and max.
*
* @return string
* @param int $min
* @param int $max
* @return $this
*/
public function __toString()
public function between(int $min, int $max)
{
if (empty($this->keys)) {
return 'array';
return $this->addRule("between:$min,$max");
}

/**
* The field under validation must be an array that is a list.
* An array is considered a list if its keys consist of consecutive numbers from 0 to count($array) - 1.
*
* @return $this
*/
public function list()
{
return $this->addRule('list');
}

/**
* The field under validation must be an array that contains all of the given parameter values.
*
* @param array|string $keys
* @return $this
*/
public function contains($keys)
{
if ($keys instanceof Arrayable) {
$keys = $keys->toArray();
}

$keys = is_array($keys) ? $keys : func_get_args();

$keys = array_map(
static fn ($key) => enum_value($key),
$this->keys,
$keys,
);

return 'array:'.implode(',', $keys);
return $this->addRule('contains:'.implode(',', $keys));
}

/**
* The field under validation must exist in anotherfield's values.
*
* string $anotherField
*
* @return $this
*/
public function inArray(string $anotherField)
{
return $this->addRule("in_array:$anotherField");
}

/**
* Convert the rule to a validation string.
*/
public function __toString(): string
{
return implode('|', array_unique($this->constraints));
}

/**
* Add custom rules to the validation rules array.
*/
protected function addRule(array|string $rules): ArrayRule
{
$this->constraints = array_merge($this->constraints, Arr::wrap($rules));

return $this;
}
}
3 changes: 2 additions & 1 deletion src/Illuminate/Validation/ValidationRuleParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Validation\Rules\ArrayRule;
use Illuminate\Validation\Rules\Date;
use Illuminate\Validation\Rules\Exists;
use Illuminate\Validation\Rules\Numeric;
Expand Down Expand Up @@ -100,7 +101,7 @@ protected function explodeExplicitRule($rule, $attribute)
$rules = [];

foreach ($rule as $value) {
if ($value instanceof Date || $value instanceof Numeric) {
if ($value instanceof Date || $value instanceof Numeric || $value instanceof ArrayRule) {
$rules = array_merge($rules, explode('|', (string) $value));
} else {
$rules[] = $this->prepareRule($value, $attribute);
Expand Down
Loading
0