diff --git a/src/Illuminate/Validation/Rule.php b/src/Illuminate/Validation/Rule.php index a2aa467e3033..35a380405dde 100644 --- a/src/Illuminate/Validation/Rule.php +++ b/src/Illuminate/Validation/Rule.php @@ -4,6 +4,7 @@ use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Traits\Macroable; +use Illuminate\Validation\Rules\ArrayRule; use Illuminate\Validation\Rules\Can; use Illuminate\Validation\Rules\Dimensions; use Illuminate\Validation\Rules\Enum; @@ -59,6 +60,17 @@ public static function unless($condition, $rules, $defaultRules = []) return new ConditionalRules($condition, $defaultRules, $rules); } + /** + * Get an array rule builder instance. + * + * @param array|null $keys + * @return \Illuminate\Validation\ArrayRule + */ + public static function array($keys = null) + { + return new ArrayRule(...func_get_args()); + } + /** * Create a new nested rule set. * @@ -95,7 +107,7 @@ public static function exists($table, $column = 'NULL') } /** - * Get an in constraint builder instance. + * Get an in rule builder instance. * * @param \Illuminate\Contracts\Support\Arrayable|\BackedEnum|\UnitEnum|array|string $values * @return \Illuminate\Validation\Rules\In @@ -110,7 +122,7 @@ public static function in($values) } /** - * Get a not_in constraint builder instance. + * Get a not_in rule builder instance. * * @param \Illuminate\Contracts\Support\Arrayable|\BackedEnum|\UnitEnum|array|string $values * @return \Illuminate\Validation\Rules\NotIn @@ -125,7 +137,7 @@ public static function notIn($values) } /** - * Get a required_if constraint builder instance. + * Get a required_if rule builder instance. * * @param callable|bool $callback * @return \Illuminate\Validation\Rules\RequiredIf @@ -136,7 +148,7 @@ public static function requiredIf($callback) } /** - * Get a exclude_if constraint builder instance. + * Get a exclude_if rule builder instance. * * @param callable|bool $callback * @return \Illuminate\Validation\Rules\ExcludeIf @@ -147,7 +159,7 @@ public static function excludeIf($callback) } /** - * Get a prohibited_if constraint builder instance. + * Get a prohibited_if rule builder instance. * * @param callable|bool $callback * @return \Illuminate\Validation\Rules\ProhibitedIf @@ -158,7 +170,7 @@ public static function prohibitedIf($callback) } /** - * Get an enum constraint builder instance. + * Get an enum rule builder instance. * * @param class-string $type * @return \Illuminate\Validation\Rules\Enum @@ -169,7 +181,7 @@ public static function enum($type) } /** - * Get a file constraint builder instance. + * Get a file rule builder instance. * * @return \Illuminate\Validation\Rules\File */ @@ -179,7 +191,7 @@ public static function file() } /** - * Get an image file constraint builder instance. + * Get an image file rule builder instance. * * @return \Illuminate\Validation\Rules\ImageFile */ @@ -189,7 +201,7 @@ public static function imageFile() } /** - * Get a dimensions constraint builder instance. + * Get a dimensions rule builder instance. * * @param array $constraints * @return \Illuminate\Validation\Rules\Dimensions diff --git a/src/Illuminate/Validation/Rules/ArrayRule.php b/src/Illuminate/Validation/Rules/ArrayRule.php new file mode 100644 index 000000000000..dc11e36f01e1 --- /dev/null +++ b/src/Illuminate/Validation/Rules/ArrayRule.php @@ -0,0 +1,56 @@ +toArray(); + } + + $this->keys = is_array($keys) ? $keys : func_get_args(); + } + + /** + * Convert the rule to a validation string. + * + * @return string + */ + public function __toString() + { + if (empty($this->keys)) { + return 'array'; + } + + $keys = array_map( + static fn ($key) => match (true) { + $key instanceof BackedEnum => $key->value, + $key instanceof UnitEnum => $key->name, + default => $key, + }, + $this->keys, + ); + + return 'array:'.implode(',', $keys); + } +} diff --git a/tests/Validation/Enums.php b/tests/Validation/Enums.php index ad8f9c34f6e6..24cd566e33d5 100644 --- a/tests/Validation/Enums.php +++ b/tests/Validation/Enums.php @@ -19,3 +19,17 @@ enum PureEnum case one; case two; } + +enum ArrayKeys +{ + case key_1; + case key_2; + case key_3; +} + +enum ArrayKeysBacked: string +{ + case key_1 = 'key_1'; + case key_2 = 'key_2'; + case key_3 = 'key_3'; +} diff --git a/tests/Validation/ValidationArrayRuleTest.php b/tests/Validation/ValidationArrayRuleTest.php new file mode 100644 index 000000000000..fcf4dfbe10de --- /dev/null +++ b/tests/Validation/ValidationArrayRuleTest.php @@ -0,0 +1,38 @@ +assertSame('array', (string) $rule); + + $rule = Rule::array('key_1', 'key_2', 'key_3'); + + $this->assertSame('array:key_1,key_2,key_3', (string) $rule); + + $rule = Rule::array(['key_1', 'key_2', 'key_3']); + + $this->assertSame('array:key_1,key_2,key_3', (string) $rule); + + $rule = Rule::array(collect(['key_1', 'key_2', 'key_3'])); + + $this->assertSame('array:key_1,key_2,key_3', (string) $rule); + + $rule = Rule::array([ArrayKeys::key_1, ArrayKeys::key_2, ArrayKeys::key_3]); + + $this->assertSame('array:key_1,key_2,key_3', (string) $rule); + + $rule = Rule::array([ArrayKeysBacked::key_1, ArrayKeysBacked::key_2, ArrayKeysBacked::key_3]); + + $this->assertSame('array:key_1,key_2,key_3', (string) $rule); + } +}