Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] New object syntax for the rule in #15923

Merged
merged 1 commit into from
Oct 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}