From 173ad40b7dc79c2ef0144ad6bf8dab4869a68546 Mon Sep 17 00:00:00 2001 From: Joel Clermont Date: Tue, 5 Mar 2013 17:12:42 -0600 Subject: [PATCH] fix array structure to make it easier to parse. update tests --- src/Valitron/Validator.php | 19 ++++--------------- tests/Valitron/Validate.php | 30 +++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 9df216b..d6043b4 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -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])) { - foreach ($params as $innerParams) { - $this->callRuleWithParams($ruleType, $innerParams); - } - } else { - $this->callRuleWithParams($ruleType, $params); + if (is_array($params)) { + foreach ($params as $innerParams) { + 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); - } } \ No newline at end of file diff --git a/tests/Valitron/Validate.php b/tests/Valitron/Validate.php index a07b800..bdfd69e 100644 --- a/tests/Valitron/Validate.php +++ b/tests/Valitron/Validate.php @@ -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()); + } }