mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
fix array structure to make it easier to parse. update tests
This commit is contained in:
parent
035de87cf7
commit
173ad40b7d
@ -515,25 +515,14 @@ class Validator
|
||||
public function rules($rules)
|
||||
{
|
||||
foreach ($rules as $ruleType => $params) {
|
||||
if (is_array($params) && !empty($params)) {
|
||||
// differentiate between a single rule taking an array of params
|
||||
// and an array of rules within a single type
|
||||
if (count($params) > 1 && is_array($params[0]) && is_array($params[1])) {
|
||||
if (is_array($params)) {
|
||||
foreach ($params as $innerParams) {
|
||||
$this->callRuleWithParams($ruleType, $innerParams);
|
||||
}
|
||||
} else {
|
||||
$this->callRuleWithParams($ruleType, $params);
|
||||
array_unshift($innerParams, $ruleType);
|
||||
call_user_func_array(array($this, "rule"), $innerParams);
|
||||
}
|
||||
} else {
|
||||
$this->rule($ruleType, $params);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function callRuleWithParams($ruleType, $params)
|
||||
{
|
||||
array_unshift($params, $ruleType);
|
||||
call_user_func_array(array($this, 'rule'), $params);
|
||||
}
|
||||
}
|
||||
@ -421,9 +421,15 @@ class ValidateTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAcceptBulkRulesWithMultipleParams()
|
||||
{
|
||||
$rules = array(
|
||||
'required' => array(array('nonexistent_field', 'other_missing_field')),
|
||||
'equals' => array('foo', 'bar'),
|
||||
'length' => array('foo', 5)
|
||||
'required' => array(
|
||||
array(array('nonexistent_field', 'other_missing_field'))
|
||||
),
|
||||
'equals' => array(
|
||||
array('foo', 'bar')
|
||||
),
|
||||
'length' => array(
|
||||
array('foo', 5)
|
||||
)
|
||||
);
|
||||
|
||||
$v1 = new Validator(array('foo' => 'bar', 'bar' => 'baz'));
|
||||
@ -481,5 +487,23 @@ class ValidateTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals($v1->errors(), $v2->errors());
|
||||
}
|
||||
|
||||
public function testAcceptBulkRulesWithMultipleArrayParams()
|
||||
{
|
||||
$rules = array(
|
||||
'in' => array(
|
||||
array(array('foo', 'bar'), array('x', 'y'))
|
||||
)
|
||||
);
|
||||
|
||||
$v1 = new Validator(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo'));
|
||||
$v1->rules($rules);
|
||||
$v1->validate();
|
||||
|
||||
$v2 = new Validator(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo'));
|
||||
$v2->rule('in', array('foo', 'bar'), array('x', 'y'));
|
||||
$v2->validate();
|
||||
|
||||
$this->assertEquals($v1->errors(), $v2->errors());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user