From f94b338615d1fcb299566237cc9399bb67046a66 Mon Sep 17 00:00:00 2001 From: romeOz Date: Sat, 23 May 2015 16:36:48 +0300 Subject: [PATCH] Added method `Validate::getRawRules()`. --- src/Validate.php | 24 ++++++++++++++++-------- tests/ValidateTest.php | 9 +++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/Validate.php b/src/Validate.php index 552bdf5..f029667 100644 --- a/src/Validate.php +++ b/src/Validate.php @@ -178,7 +178,7 @@ class Validate implements ObjectInterface public $skipEmpty = true; public $remainder = '*'; /** @var array */ - protected $_rules = []; + protected $rawRules = []; /** * Received errors. * @var array @@ -259,7 +259,7 @@ public function validate($input) { $this->errors = []; - foreach($this->_rules as $rules){ + foreach($this->rawRules as $rules){ list($ruleName, $rule) = $rules; @@ -369,6 +369,14 @@ public function existsRule($name) return isset($this->rules[$name]); } + /** + * @return rules\Rule[] + */ + public function getRawRules() + { + return $this->rawRules; + } + public function __call($name, $arguments) { if (method_exists($this, "{$name}Internal")) { @@ -384,7 +392,7 @@ public function __call($name, $arguments) /** @var Rule $rule */ $reflect = new \ReflectionClass($this->rules[$name]['class']); $rule = $reflect->newInstanceArgs($arguments); - $this->_rules[] = [$name, $rule]; + $this->rawRules[] = [$name, $rule]; return $this; } @@ -414,28 +422,28 @@ protected function isEmpty($value, Rule $rule) protected function attributesInternal($attributes) { - $this->_rules = []; - $this->_rules[] = ['attributes', new Attributes(['attributes' => $attributes, 'one' => $this->one, 'valid' => $this->valid, 'remainder' => $this->remainder])]; + $this->rawRules = []; + $this->rawRules[] = ['attributes', new Attributes(['attributes' => $attributes, 'one' => $this->one, 'valid' => $this->valid, 'remainder' => $this->remainder])]; return $this; } protected function oneOfInternal(Validate $validate) { $validate->one = true; - $this->_rules[] = ['oneOf', $validate]; + $this->rawRules[] = ['oneOf', $validate]; return $this; } protected function notOfInternal(Validate $validate) { $validate->valid = false; - $this->_rules[] = ['notOf', $validate]; + $this->rawRules[] = ['notOf', $validate]; return $this; } protected function whenInternal(Validate $if, Validate $then, Validate $else = null) { - $this->_rules[] = ['when', new When(['if' => $if, 'then' => $then, 'else' => $else, 'valid' => $this->valid])]; + $this->rawRules[] = ['when', new When(['if' => $if, 'then' => $then, 'else' => $else, 'valid' => $this->valid])]; return $this; } diff --git a/tests/ValidateTest.php b/tests/ValidateTest.php index a5305a6..665107c 100644 --- a/tests/ValidateTest.php +++ b/tests/ValidateTest.php @@ -2,6 +2,7 @@ namespace rockunit; +use rock\validate\Attributes; use rock\validate\locale\en\Date; use rock\validate\locale\en\Numeric; use rock\validate\Validate; @@ -912,6 +913,14 @@ public function testRemainder() ); $this->assertTrue($validate->labelRemainder('_rem')->validate($input)); } + + public function testGetRawRules() + { + $rawRules = Validate::attributes(Validate::required()->string())->getRawRules(); + $rawRule = current($rawRules); + list(,$rule) = $rawRule; + $this->assertTrue($rule instanceof Attributes); + } }