From ff91a0909af6fdcad0fcfe8dc526c299c8431079 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Sun, 6 May 2012 01:07:13 -0430 Subject: [PATCH] Implemented countable interface for CakeValidationSet --- lib/Cake/Model/ModelValidator.php | 2 +- lib/Cake/Model/Validator/CakeValidationSet.php | 11 ++++++++++- .../Model/Validator/CakeValidationSetTest.php | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Model/ModelValidator.php b/lib/Cake/Model/ModelValidator.php index ca0936a37..4c29b1078 100644 --- a/lib/Cake/Model/ModelValidator.php +++ b/lib/Cake/Model/ModelValidator.php @@ -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 **/ diff --git a/lib/Cake/Model/Validator/CakeValidationSet.php b/lib/Cake/Model/Validator/CakeValidationSet.php index f3c29b62d..1fde937ee 100644 --- a/lib/Cake/Model/Validator/CakeValidationSet.php +++ b/lib/Cake/Model/Validator/CakeValidationSet.php @@ -27,7 +27,7 @@ App::uses('CakeRule', 'Model/Validator'); * @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 @@ -293,4 +293,13 @@ class CakeValidationSet implements ArrayAccess, IteratorAggregate { return new ArrayIterator($this->_rules); } +/** + * Returns the number of rules in this set + * + * @return int + **/ + public function count() { + return count($this->_rules); + } + } \ No newline at end of file diff --git a/lib/Cake/Test/Case/Model/Validator/CakeValidationSetTest.php b/lib/Cake/Test/Case/Model/Validator/CakeValidationSetTest.php index ce5fcd5ef..0314cc4df 100644 --- a/lib/Cake/Test/Case/Model/Validator/CakeValidationSetTest.php +++ b/lib/Cake/Test/Case/Model/Validator/CakeValidationSetTest.php @@ -274,4 +274,21 @@ class CakeValidationSetTest extends CakeTestCase { $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); + } + }