Skip to content

Commit

Permalink
Implemented countable interface for CakeValidationSet
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed May 6, 2012
1 parent 989a8b8 commit ff91a09
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/Cake/Model/ModelValidator.php
Expand Up @@ -482,7 +482,7 @@ public function getIterator() {
}

/**
* Returns the numbers of fields having validation rules
* Returns the number of fields having validation rules
*
* @return int
**/
Expand Down
11 changes: 10 additions & 1 deletion 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, IteratorAggregate {
class CakeValidationSet implements ArrayAccess, IteratorAggregate, Countable {

/**
* Holds the ValidationRule objects
Expand Down Expand Up @@ -293,4 +293,13 @@ public function getIterator() {
return new ArrayIterator($this->_rules);
}

/**
* Returns the number of rules in this set
*
* @return int
**/
public function count() {
return count($this->_rules);
}

}
17 changes: 17 additions & 0 deletions lib/Cake/Test/Case/Model/Validator/CakeValidationSetTest.php
Expand Up @@ -274,4 +274,21 @@ public function testIterator() {
$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);
}

}

0 comments on commit ff91a09

Please sign in to comment.