Skip to content

Commit

Permalink
Add new object syntax for the not_in validation rule
Browse files Browse the repository at this point in the history
  • Loading branch information
sileence committed Oct 17, 2016
1 parent f6262c9 commit 68ba973
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Illuminate/Validation/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ public static function in(array $values)
return new Rules\In($values);
}

/**
* Get a not_in constraint builder instance.
*
* @param array $values
* @return \Illuminate\Validation\Rules\In
*/
public static function notIn(array $values)
{
return new Rules\NotIn($values);
}

/**
* Get a unique constraint builder instance.
*
Expand Down
7 changes: 6 additions & 1 deletion src/Illuminate/Validation/Rules/In.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

class In
{
/**
* The name of the rule.
*/
protected $rule = 'in';

/**
* The accepted values.
*
Expand All @@ -29,6 +34,6 @@ public function __construct(array $values)
*/
public function __toString()
{
return 'in:'.implode(',', $this->values);
return $this->rule.':'.implode(',', $this->values);
}
}
11 changes: 11 additions & 0 deletions src/Illuminate/Validation/Rules/NotIn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Illuminate\Validation\Rules;

class NotIn extends In
{
/**
* The name of the rule.
*/
protected $rule = 'not_in';
}
18 changes: 18 additions & 0 deletions tests/Validation/ValidationNotInRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\NotIn;

class ValidationNotInRuleTest extends PHPUnit_Framework_TestCase
{
public function testItCorrectlyFormatsAStringVersionOfTheRule()
{
$rule = new NotIn(['Laravel', 'Framework', 'PHP']);

$this->assertEquals('not_in:Laravel,Framework,PHP', (string) $rule);

$rule = Rule::notIn([1, 2, 3, 4]);

$this->assertEquals('not_in:1,2,3,4', (string) $rule);
}
}

0 comments on commit 68ba973

Please sign in to comment.