Skip to content

Commit

Permalink
allow to pass collection to in/notIn rules (#23875)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerguslejko authored and taylorotwell committed Apr 17, 2018
1 parent 00e92dd commit e3d958b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Illuminate/Validation/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Validation;

use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Macroable;

class Rule
Expand Down Expand Up @@ -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());
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Validation/ValidationInRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions tests/Validation/ValidationNotInRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e3d958b

Please sign in to comment.