Implementing IteratorAggregate interface for ModelValidator

This commit is contained in:
Jose Lorenzo Rodriguez 2012-05-05 17:34:35 -04:30
parent d35f8e6b00
commit 81c0c3d91d
2 changed files with 37 additions and 1 deletions

View file

@ -27,7 +27,7 @@ App::uses('CakeValidationSet', 'Model/Validator');
* @package Cake.Model
* @link http://book.cakephp.org/2.0/en/data-validation.html
*/
class ModelValidator implements ArrayAccess {
class ModelValidator implements ArrayAccess, IteratorAggregate {
/**
* Holds the CakeValidationSet objects array
@ -470,4 +470,13 @@ class ModelValidator implements ArrayAccess {
unset($this->_fields[$field]);
}
/**
* Returns an iterator for each of the fields to be validated
*
* @return ArrayIterator
**/
public function getIterator() {
$this->_parseRules();
return new ArrayIterator($this->_fields);
}
}

View file

@ -1789,4 +1789,31 @@ class ModelValidationTest extends BaseModelTest {
$this->assertFalse(isset($Validator['title']));
}
/**
* Tests it is possible to iterate a validation object
*
* @return void
*/
public function testIterator() {
$this->loadFixtures('Article');
$TestModel = new Article();
$Validator = $TestModel->validator();
$i = 0;
foreach ($Validator as $field => $rules) {
if ($i === 0) {
$this->assertEquals('user_id', $field);
}
if ($i === 1) {
$this->assertEquals('title', $field);
}
if ($i === 2) {
$this->assertEquals('body', $field);
}
$this->assertInstanceOf('CakeValidationSet', $rules);
$i++;
}
$this->assertEquals(3, $i);
}
}