From e3d958ba253964dad7be80f82806541672581d7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jergu=C5=A1=20Lejko?= Date: Tue, 17 Apr 2018 13:31:29 +0100 Subject: [PATCH] allow to pass collection to in/notIn rules (#23875) --- src/Illuminate/Validation/Rule.php | 13 +++++++++++-- tests/Validation/ValidationInRuleTest.php | 4 ++++ tests/Validation/ValidationNotInRuleTest.php | 4 ++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Validation/Rule.php b/src/Illuminate/Validation/Rule.php index 0bd7738daec3..07812c804a1e 100644 --- a/src/Illuminate/Validation/Rule.php +++ b/src/Illuminate/Validation/Rule.php @@ -2,6 +2,7 @@ namespace Illuminate\Validation; +use Illuminate\Support\Collection; use Illuminate\Support\Traits\Macroable; class Rule @@ -34,22 +35,30 @@ public static function exists($table, $column = 'NULL') /** * Get an in constraint builder instance. * - * @param array|string $values + * @param array|string|\Illuminate\Support\Collection $values * @return \Illuminate\Validation\Rules\In */ public static function in($values) { + if ($values instanceof Collection) { + $values = $values->toArray(); + } + return new Rules\In(is_array($values) ? $values : func_get_args()); } /** * Get a not_in constraint builder instance. * - * @param array|string $values + * @param array|string|\Illuminate\Support\Collection $values * @return \Illuminate\Validation\Rules\NotIn */ public static function notIn($values) { + if ($values instanceof Collection) { + $values = $values->toArray(); + } + return new Rules\NotIn(is_array($values) ? $values : func_get_args()); } diff --git a/tests/Validation/ValidationInRuleTest.php b/tests/Validation/ValidationInRuleTest.php index 9607fbc9d91b..9b08fda53f20 100644 --- a/tests/Validation/ValidationInRuleTest.php +++ b/tests/Validation/ValidationInRuleTest.php @@ -26,6 +26,10 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule() $this->assertEquals('in:"1","2","3","4"', (string) $rule); + $rule = Rule::in(collect([1, 2, 3, 4])); + + $this->assertEquals('in:"1","2","3","4"', (string) $rule); + $rule = Rule::in('1', '2', '3', '4'); $this->assertEquals('in:"1","2","3","4"', (string) $rule); diff --git a/tests/Validation/ValidationNotInRuleTest.php b/tests/Validation/ValidationNotInRuleTest.php index 1482391b6194..68793044a96a 100644 --- a/tests/Validation/ValidationNotInRuleTest.php +++ b/tests/Validation/ValidationNotInRuleTest.php @@ -18,6 +18,10 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule() $this->assertEquals('not_in:"1","2","3","4"', (string) $rule); + $rule = Rule::notIn(collect([1, 2, 3, 4])); + + $this->assertEquals('not_in:"1","2","3","4"', (string) $rule); + $rule = Rule::notIn('1', '2', '3', '4'); $this->assertEquals('not_in:"1","2","3","4"', (string) $rule);