diff --git a/src/Illuminate/Validation/Rules/Enum.php b/src/Illuminate/Validation/Rules/Enum.php index 3517deb194de..59991bbdd7c6 100644 --- a/src/Illuminate/Validation/Rules/Enum.php +++ b/src/Illuminate/Validation/Rules/Enum.php @@ -2,6 +2,7 @@ namespace Illuminate\Validation\Rules; +use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Validation\Rule; use Illuminate\Contracts\Validation\ValidatorAwareRule; use Illuminate\Support\Arr; @@ -80,12 +81,12 @@ public function passes($attribute, $value) /** * Specify the cases that should be considered valid. * - * @param \UnitEnum[]|\UnitEnum $values + * @param \UnitEnum[]|\UnitEnum|\Illuminate\Contracts\Support\Arrayable $values * @return $this */ public function only($values) { - $this->only = Arr::wrap($values); + $this->only = $values instanceof Arrayable ? $values->toArray() : Arr::wrap($values); return $this; } @@ -93,12 +94,12 @@ public function only($values) /** * Specify the cases that should be considered invalid. * - * @param \UnitEnum[]|\UnitEnum $values + * @param \UnitEnum[]|\UnitEnum|\Illuminate\Contracts\Support\Arrayable $values * @return $this */ public function except($values) { - $this->except = Arr::wrap($values); + $this->except = $values instanceof Arrayable ? $values->toArray() : Arr::wrap($values); return $this; } diff --git a/tests/Validation/ValidationEnumRuleTest.php b/tests/Validation/ValidationEnumRuleTest.php index 929adb0d4866..68200d3390cb 100644 --- a/tests/Validation/ValidationEnumRuleTest.php +++ b/tests/Validation/ValidationEnumRuleTest.php @@ -3,6 +3,9 @@ namespace Illuminate\Tests\Validation; use Illuminate\Container\Container; +use Illuminate\Contracts\Support\Arrayable; +use Illuminate\Database\Eloquent\Casts\ArrayObject; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\Facade; use Illuminate\Translation\ArrayLoader; use Illuminate\Translation\Translator; @@ -101,7 +104,7 @@ public function testValidationPassesForAllCasesUntilEitherOnlyOrExceptIsPassed() #[DataProvider('conditionalCasesDataProvider')] public function testValidationPassesWhenOnlyCasesProvided( IntegerStatus|int $enum, - array|IntegerStatus $only, + array|Arrayable|IntegerStatus $only, bool $expected ) { $v = new Validator( @@ -120,7 +123,7 @@ public function testValidationPassesWhenOnlyCasesProvided( #[DataProvider('conditionalCasesDataProvider')] public function testValidationPassesWhenExceptCasesProvided( int|IntegerStatus $enum, - array|IntegerStatus $except, + array|Arrayable|IntegerStatus $except, bool $expected ) { $v = new Validator( @@ -251,6 +254,8 @@ public static function conditionalCasesDataProvider(): array return [ [IntegerStatus::done, IntegerStatus::done, true], [IntegerStatus::done, [IntegerStatus::done, IntegerStatus::pending], true], + [IntegerStatus::done, new ArrayObject([IntegerStatus::done, IntegerStatus::pending]), true], + [IntegerStatus::done, new Collection([IntegerStatus::done, IntegerStatus::pending]), true], [IntegerStatus::pending->value, [IntegerStatus::done, IntegerStatus::pending], true], [IntegerStatus::done->value, IntegerStatus::pending, false], ];