Skip to content

Commit

Permalink
Added method Validate::getRawRules().
Browse files Browse the repository at this point in the history
  • Loading branch information
romeOz committed May 23, 2015
1 parent 7dccf4e commit f94b338
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class Validate implements ObjectInterface
public $skipEmpty = true;
public $remainder = '*';
/** @var array */
protected $_rules = [];
protected $rawRules = [];
/**
* Received errors.
* @var array
Expand Down Expand Up @@ -259,7 +259,7 @@ public function validate($input)
{
$this->errors = [];

foreach($this->_rules as $rules){
foreach($this->rawRules as $rules){

list($ruleName, $rule) = $rules;

Expand Down Expand Up @@ -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")) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
9 changes: 9 additions & 0 deletions tests/ValidateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}


Expand Down

0 comments on commit f94b338

Please sign in to comment.