Implemented countable interface for CakeValidationSet

This commit is contained in:
Jose Lorenzo Rodriguez 2012-05-06 01:07:13 -04:30
parent 989a8b8398
commit ff91a0909a
3 changed files with 28 additions and 2 deletions

View file

@ -482,7 +482,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
} }
/** /**
* Returns the numbers of fields having validation rules * Returns the number of fields having validation rules
* *
* @return int * @return int
**/ **/

View file

@ -27,7 +27,7 @@ App::uses('CakeRule', 'Model/Validator');
* @package Cake.Model.Validator * @package Cake.Model.Validator
* @link http://book.cakephp.org/2.0/en/data-validation.html * @link http://book.cakephp.org/2.0/en/data-validation.html
*/ */
class CakeValidationSet implements ArrayAccess, IteratorAggregate { class CakeValidationSet implements ArrayAccess, IteratorAggregate, Countable {
/** /**
* Holds the ValidationRule objects * Holds the ValidationRule objects
@ -293,4 +293,13 @@ class CakeValidationSet implements ArrayAccess, IteratorAggregate {
return new ArrayIterator($this->_rules); return new ArrayIterator($this->_rules);
} }
/**
* Returns the number of rules in this set
*
* @return int
**/
public function count() {
return count($this->_rules);
}
} }

View file

@ -274,4 +274,21 @@ class CakeValidationSetTest extends CakeTestCase {
$this->assertEquals(3, $i); $this->assertEquals(3, $i);
} }
/**
* Tests countable interface
*
* @return void
*/
public function testCount() {
$Set = new CakeValidationSet('title', array(
'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
'numeric' => array('rule' => 'numeric'),
'other' => array('rule' => array('other', 1)),
));
$this->assertCount(3, $Set);
unset($Set['other']);
$this->assertCount(2, $Set);
}
} }