Skip to content

Commit

Permalink
New object syntax for the rule in (laravel#15923)
Browse files Browse the repository at this point in the history
  • Loading branch information
sileence authored and bepsvpt committed Oct 18, 2016
1 parent 50a41c3 commit 23daa21
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Illuminate/Validation/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ public static function exists($table, $column = 'NULL')
return new Rules\Exists($table, $column);
}

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

/**
* Get a unique constraint builder instance.
*
Expand Down
34 changes: 34 additions & 0 deletions src/Illuminate/Validation/Rules/In.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Illuminate\Validation\Rules;

class In
{
/**
* The accepted values.
*
* @var array
*/
protected $values;

/**
* Create a new in rule instance.
*
* @param array $values
* @return void
*/
public function __construct(array $values)
{
$this->values = $values;
}

/**
* Convert the rule to a validation string.
*
* @return string
*/
public function __toString()
{
return 'in:'.implode(',', $this->values);
}
}
18 changes: 18 additions & 0 deletions tests/Validation/ValidationInRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\In;

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

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

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

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

0 comments on commit 23daa21

Please sign in to comment.