Browse Source

Renamed {n} to {*} in collections

Jose Lorenzo Rodriguez 10 years ago
parent
commit
d5b06604e7

+ 1 - 1
src/Collection/CollectionInterface.php

@@ -220,7 +220,7 @@ interface CollectionInterface extends Iterator, JsonSerializable
      *      ['comment' => ['votes' => [['value' => 1], ['value' => 2], ['value' => 3]]],
      *      ['comment' => ['votes' => [['value' => 4]]
      * ];
-     * $extracted = (new Collection($items))->extract('comment.votes.{n}.value');
+     * $extracted = (new Collection($items))->extract('comment.votes.{*}.value');
      *
      * // Result will contain
      * [1, 2, 3, 4]

+ 2 - 3
src/Collection/CollectionTrait.php

@@ -162,11 +162,10 @@ trait CollectionTrait
     public function extract($matcher)
     {
         $extractor = new ExtractIterator($this->unwrap(), $matcher);
-
-        if (is_string($matcher) && strpos($matcher, '{n}') !== false) {
+        if (is_string($matcher) && strpos($matcher, '{*}') !== false) {
             $extractor = $extractor
                 ->filter(function ($data) {
-                    return $data instanceof \Traversable || is_array($data);
+                    return $data !== null && ($data instanceof \Traversable || is_array($data));
                 })
                 ->unfold();
         }

+ 3 - 3
src/Collection/ExtractTrait.php

@@ -38,7 +38,7 @@ trait ExtractTrait
 
         $path = explode('.', $callback);
 
-        if (strpos($callback, '{n}') !== false) {
+        if (strpos($callback, '{*}') !== false) {
             return function ($element) use ($path) {
                 return $this->_extract($element, $path);
             };
@@ -52,7 +52,7 @@ trait ExtractTrait
     /**
      * Returns a column from $data that can be extracted
      * by iterating over the column names contained in $path.
-     * It will return arrays for elements in represented with `{n}`
+     * It will return arrays for elements in represented with `{*}`
      *
      * @param array|\ArrayAccess $data Data.
      * @param array $path Path to extract from.
@@ -64,7 +64,7 @@ trait ExtractTrait
         $collectionTransform = false;
 
         foreach ($path as $i => $column) {
-            if ($column === '{n}') {
+            if ($column === '{*}') {
                 $collectionTransform = true;
                 continue;
             }

+ 3 - 3
tests/TestCase/Collection/CollectionTest.php

@@ -1383,7 +1383,7 @@ class CollectionTest extends TestCase
     }
 
     /**
-     * Tests using extract with the {n} notation
+     * Tests using extract with the {*} notation
      *
      * @return void
      */
@@ -1395,7 +1395,7 @@ class CollectionTest extends TestCase
             ['comments' => [['id' => 7], ['nope' => 8]]],
         ];
 
-        $extracted = (new Collection($items))->extract('comments.{n}.id');
+        $extracted = (new Collection($items))->extract('comments.{*}.id');
         $this->assertEquals([1, 2, 3, 4, 7, null], $extracted->toList());
 
         $items = [
@@ -1429,7 +1429,7 @@ class CollectionTest extends TestCase
             ],
             ['not_comments' => []]
         ];
-        $extracted = (new Collection($items))->extract('comments.{n}.voters.{n}.id');
+        $extracted = (new Collection($items))->extract('comments.{*}.voters.{*}.id');
         $expected = [1, 2, 3, 4, 5, null, 6];
         $this->assertEquals($expected, $extracted->toList());
     }