Skip to content

Commit

Permalink
Implementing IteratorAggregate for CakeValidationSet
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed May 6, 2012
1 parent 6f16a66 commit 989a8b8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/Cake/Model/Validator/CakeValidationSet.php
Expand Up @@ -27,7 +27,7 @@
* @package Cake.Model.Validator
* @link http://book.cakephp.org/2.0/en/data-validation.html
*/
class CakeValidationSet implements ArrayAccess {
class CakeValidationSet implements ArrayAccess, IteratorAggregate {

/**
* Holds the ValidationRule objects
Expand Down Expand Up @@ -284,4 +284,13 @@ public function offsetUnset($index) {
unset($this->_rules[$index]);
}

}
/**
* Returns an iterator for each of the rules to be applied
*
* @return ArrayIterator
**/
public function getIterator() {
return new ArrayIterator($this->_rules);
}

}
30 changes: 30 additions & 0 deletions lib/Cake/Test/Case/Model/Validator/CakeValidationSetTest.php
Expand Up @@ -244,4 +244,34 @@ public function testArrayAccessUnset() {
unset($Set['other']);
$this->assertFalse(isset($Set['notEmpty']));
}

/**
* Tests it is possible to iterate a validation set object
*
* @return void
*/
public function testIterator() {
$Set = new CakeValidationSet('title', array(
'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
'numeric' => array('rule' => 'numeric'),
'other' => array('rule' => array('other', 1)),
));

$i = 0;
foreach ($Set as $name => $rule) {
if ($i === 0) {
$this->assertEquals('notEmpty', $name);
}
if ($i === 1) {
$this->assertEquals('numeric', $name);
}
if ($i === 2) {
$this->assertEquals('other', $name);
}
$this->assertInstanceOf('CakeRule', $rule);
$i++;
}
$this->assertEquals(3, $i);
}

}

0 comments on commit 989a8b8

Please sign in to comment.