Browse Source

Implemented the 3 iterator modes for listNested

Jose Lorenzo Rodriguez 12 years ago
parent
commit
07bd2dcdf3

+ 9 - 3
src/Collection/CollectionTrait.php

@@ -862,12 +862,18 @@ trait CollectionTrait {
 /**
  * 
  *
- * @return void
+ * @return \Cake\Collection\Iterator\TreeIterator
  */
-	public function listNested($nestingKey = 'children') {
+	public function listNested($dir = 'desc', $nestingKey = 'children') {
+		$dir = strtolower($dir);
+		$modes = [
+			'desc' => TreeIterator::SELF_FIRST,
+			'asc' => TreeIterator::CHILD_FIRST,
+			'leaves' => TreeIterator::LEAVES_ONLY
+		];
 		return new TreeIterator(
 			new NestIterator($this, $nestingKey),
-			TreeIterator::SELF_FIRST
+			isset($modes[$dir]) ? $modes[$dir] : $dir
 		);
 	}
 

+ 1 - 1
src/Collection/Iterator/NestIterator.php

@@ -38,7 +38,7 @@ class NestIterator extends Collection implements RecursiveIterator{
 		if (is_array($children)) {
 			return !empty($children);
 		}
-		
+
 		if ($children instanceof \Traversable) {
 			return true;
 		}

+ 18 - 7
tests/TestCase/Collection/CollectionTest.php

@@ -873,11 +873,25 @@ class CollectionTest extends TestCase {
 	}
 
 /**
+ * Provider for testing each of the direcations for listNested
+ *
+ * @return void
+ */
+	public function nestedListProvider() {
+		return [
+			['desc', [1, 2, 3, 5, 7, 4, 8, 6, 9, 10]],
+			['asc', [5, 7, 3, 8, 4, 2, 1, 9, 10, 6]],
+			['leaves', [5, 7, 8, 9, 10]]
+		];
+	}
+
+/**
  * Tests the listNested method with the default 'children' nesting key
  *
+ * @dataProvider nestedListProvider
  * @return void
  */
-	public function testListNested() {
+	public function testListNested($dir, $expected) {
 		$items = [
 			['id' => 1, 'parent_id' => null],
 			['id' => 2, 'parent_id' => 1],
@@ -890,11 +904,8 @@ class CollectionTest extends TestCase {
 			['id' => 9, 'parent_id' => 6],
 			['id' => 10, 'parent_id' => 6]
 		];
-		$collection = (new Collection($items))->nest('id', 'parent_id')->listNested();
-		$this->assertEquals(
-			[1, 2, 3, 5, 7, 4, 8, 6, 9, 10],
-			$collection->extract('id')->toArray(false)
-		);
+		$collection = (new Collection($items))->nest('id', 'parent_id')->listNested($dir);
+		$this->assertEquals($expected, $collection->extract('id')->toArray(false));
 	}
 
 /**
@@ -907,7 +918,7 @@ class CollectionTest extends TestCase {
 			['id' => 1, 'stuff' => [['id' => 2, 'stuff' => [['id' => 3]]]]],
 			['id' => 4, 'stuff' => [['id' => 5]]]
 		];
-		$collection = (new Collection($items))->listNested('stuff');
+		$collection = (new Collection($items))->listNested('desc', 'stuff');
 		$this->assertEquals(range(1, 5), $collection->extract('id')->toArray(false));
 	}