Browse Source

Using an identity function in unfold() is a very common case
Doing that automatically is of great help

Jose Lorenzo Rodriguez 11 years ago
parent
commit
39607af730

+ 7 - 1
src/Collection/CollectionTrait.php

@@ -441,7 +441,13 @@ trait CollectionTrait {
 		return new StoppableIterator($this, $condition);
 	}
 
-	public function unfold(callable $unfolder) {
+	public function unfold(callable $unfolder = null) {
+		if ($unfolder === null) {
+			$unfolder = function ($item) {
+				return $item;
+			};
+		}
+
 		return new Collection(
 			new RecursiveIteratorIterator(
 				new UnfoldIterator($this, $unfolder), RecursiveIteratorIterator::LEAVES_ONLY

+ 1 - 6
tests/TestCase/Collection/CollectionTest.php

@@ -1002,13 +1002,8 @@ class CollectionTest extends TestCase {
 			[7, 8]
 		];
 
-		$collection = (new Collection($items))->unfold(function ($item) {
-			return $item;
-		});
-
+		$collection = (new Collection($items))->unfold();
 		$this->assertEquals(range(1, 8), $collection->toArray(false));
-
-
 	}
 
 }