Skip to content

Commit ff91a09

Browse files
committed
Implemented countable interface for CakeValidationSet
1 parent 989a8b8 commit ff91a09

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

lib/Cake/Model/ModelValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ public function getIterator() {
482482
}
483483

484484
/**
485-
* Returns the numbers of fields having validation rules
485+
* Returns the number of fields having validation rules
486486
*
487487
* @return int
488488
**/

lib/Cake/Model/Validator/CakeValidationSet.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* @package Cake.Model.Validator
2828
* @link http://book.cakephp.org/2.0/en/data-validation.html
2929
*/
30-
class CakeValidationSet implements ArrayAccess, IteratorAggregate {
30+
class CakeValidationSet implements ArrayAccess, IteratorAggregate, Countable {
3131

3232
/**
3333
* Holds the ValidationRule objects
@@ -293,4 +293,13 @@ public function getIterator() {
293293
return new ArrayIterator($this->_rules);
294294
}
295295

296+
/**
297+
* Returns the number of rules in this set
298+
*
299+
* @return int
300+
**/
301+
public function count() {
302+
return count($this->_rules);
303+
}
304+
296305
}

lib/Cake/Test/Case/Model/Validator/CakeValidationSetTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,21 @@ public function testIterator() {
274274
$this->assertEquals(3, $i);
275275
}
276276

277+
/**
278+
* Tests countable interface
279+
*
280+
* @return void
281+
*/
282+
public function testCount() {
283+
$Set = new CakeValidationSet('title', array(
284+
'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
285+
'numeric' => array('rule' => 'numeric'),
286+
'other' => array('rule' => array('other', 1)),
287+
));
288+
$this->assertCount(3, $Set);
289+
290+
unset($Set['other']);
291+
$this->assertCount(2, $Set);
292+
}
293+
277294
}

0 commit comments

Comments
 (0)