Browse Source

Implemented {n} notation for Collection::extract()

Jose Lorenzo Rodriguez 10 years ago
parent
commit
faec33eee5

+ 13 - 0
src/Collection/CollectionInterface.php

@@ -213,6 +213,19 @@ interface CollectionInterface extends Iterator, JsonSerializable
      * ['Mark', 'Renan']
      * ```
      *
+     * It is also possible to extract a flattened collection out of nested properties
+     *
+     * ```
+     *  $items = [
+     *      ['comment' => ['votes' => [['value' => 1], ['value' => 2], ['value' => 3]]],
+     *      ['comment' => ['votes' => [['value' => 4]]
+     * ];
+     * $extracted = (new Collection($items))->extract('comment.votes.{n}.value');
+     *
+     * // Result will contain
+     * [1, 2, 3, 4]
+     * ```
+     *
      * @param string $matcher a dot separated string symbolizing the path to follow
      * inside the hierarchy of each value so that the column can be extracted.
      * @return \Cake\Collection\CollectionInterface

+ 5 - 0
tests/TestCase/Collection/CollectionTest.php

@@ -1382,6 +1382,11 @@ class CollectionTest extends TestCase
         $this->assertEquals(11, $collection->sumOf());
     }
 
+    /**
+     * Tests using extract with the {n} notation
+     *
+     * @return void
+     */
     public function testUnfoldedExtract()
     {
         $items = [