Skip to content
Permalink
Browse files

Implementing IteratorAggregate for CakeValidationSet

  • Loading branch information...
lorenzo committed May 6, 2012
1 parent 6f16a66 commit 989a8b83983b831050429dfd409f293380990c6c
@@ -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
@@ -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);
}
}
@@ -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.
You can’t perform that action at this time.