Skip to content
This repository
Browse code

Implementing IteratorAggregate for CakeValidationSet

  • Loading branch information...
commit 989a8b83983b831050429dfd409f293380990c6c 1 parent 6f16a66
José Lorenzo Rodríguez authored May 05, 2012
13  lib/Cake/Model/Validator/CakeValidationSet.php
@@ -27,7 +27,7 @@
27 27
  * @package       Cake.Model.Validator
28 28
  * @link          http://book.cakephp.org/2.0/en/data-validation.html
29 29
  */
30  
-class CakeValidationSet implements ArrayAccess {
  30
+class CakeValidationSet implements ArrayAccess, IteratorAggregate {
31 31
 
32 32
 /**
33 33
  * Holds the ValidationRule objects
@@ -284,4 +284,13 @@ public function offsetUnset($index) {
284 284
 		unset($this->_rules[$index]);
285 285
 	}
286 286
 
287  
-}
  287
+/**
  288
+ * Returns an iterator for each of the rules to be applied
  289
+ *
  290
+ * @return ArrayIterator
  291
+ **/
  292
+	public function getIterator() {
  293
+		return new ArrayIterator($this->_rules);
  294
+	}
  295
+
  296
+}
30  lib/Cake/Test/Case/Model/Validator/CakeValidationSetTest.php
@@ -244,4 +244,34 @@ public function testArrayAccessUnset() {
244 244
 		unset($Set['other']);
245 245
 		$this->assertFalse(isset($Set['notEmpty']));
246 246
 	}
  247
+
  248
+/**
  249
+ * Tests it is possible to iterate a validation set object
  250
+ *
  251
+ * @return void
  252
+ */
  253
+	public function testIterator() {
  254
+		$Set = new CakeValidationSet('title', array(
  255
+			'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
  256
+			'numeric' => array('rule' => 'numeric'),
  257
+			'other' => array('rule' => array('other', 1)),
  258
+		));
  259
+
  260
+		$i = 0;
  261
+		foreach ($Set as $name => $rule) {
  262
+			if ($i === 0) {
  263
+				$this->assertEquals('notEmpty', $name);
  264
+			}
  265
+			if ($i === 1) {
  266
+				$this->assertEquals('numeric', $name);
  267
+			}
  268
+			if ($i === 2) {
  269
+				$this->assertEquals('other', $name);
  270
+			}
  271
+			$this->assertInstanceOf('CakeRule', $rule);
  272
+			$i++;
  273
+		}
  274
+		$this->assertEquals(3, $i);
  275
+	}
  276
+
247 277
 }

0 notes on commit 989a8b8

Please sign in to comment.
Something went wrong with that request. Please try again.