Browse Source

rename to chunkWithKeys

Joris Vaesen 9 years ago
parent
commit
17fcc701c0

+ 4 - 4
src/Collection/CollectionInterface.php

@@ -926,16 +926,16 @@ interface CollectionInterface extends Iterator, JsonSerializable
      * ### Example:
      *
      * ```
-     * $items [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
-     * $chunked = (new Collection($items))->chunks(3)->toList();
-     * // Returns [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11]]
+     * $items ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6];
+     * $chunked = (new Collection($items))->chunkWithKeys(3)->toList();
+     * // Returns [['a' => 1, 'b' => 2, 'c' => 3], ['d' => 4, 'e' => 5, 'f' => 6]]
      * ```
      *
      * @param int $chunkSize The maximum size for each chunk
      * @param bool $preserveKeys If the keys of the array should be preserved
      * @return \Cake\Collection\CollectionInterface
      */
-    public function chunks($chunkSize, $preserveKeys = false);
+    public function chunkWithKeys($chunkSize, $preserveKeys = true);
 
     /**
      * Returns whether or not there are elements in this collection

+ 1 - 1
src/Collection/CollectionTrait.php

@@ -647,7 +647,7 @@ trait CollectionTrait
      * {@inheritDoc}
      *
      */
-    public function chunks($chunkSize, $preserveKeys = false)
+    public function chunkWithKeys($chunkSize, $preserveKeys = true)
     {
         return $this->map(function ($v, $k, $iterator) use ($chunkSize, $preserveKeys) {
             $key = 0;

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

@@ -1771,54 +1771,54 @@ class CollectionTest extends TestCase
     }
 
     /**
-     * Tests the chunks method with exact chunks
+     * Tests the chunkWithKeys method with exact chunks
      *
      * @return void
      */
-    public function testChunks()
+    public function testChunkWithKeys()
     {
-        $collection = new Collection(range(1, 10));
-        $chunked = $collection->chunks(2)->toList();
-        $expected = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]];
+        $collection = new Collection(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6]);
+        $chunked = $collection->chunkWithKeys(2)->toList();
+        $expected = [['a' => 1, 'b' => 2], ['c' => 3, 'd' => 4], ['e' => 5, 'f' => 6]];
         $this->assertEquals($expected, $chunked);
     }
 
     /**
-     * Tests the chunks method with overflowing chunk size
+     * Tests the chunkWithKeys method with overflowing chunk size
      *
      * @return void
      */
-    public function testChunksOverflow()
+    public function testChunkWithKeysOverflow()
     {
-        $collection = new Collection(range(1, 11));
-        $chunked = $collection->chunks(2)->toList();
-        $expected = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11]];
+        $collection = new Collection(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6, 'g' => 7]);
+        $chunked = $collection->chunkWithKeys(2)->toList();
+        $expected = [['a' => 1, 'b' => 2], ['c' => 3, 'd' => 4], ['e' => 5, 'f' => 6], ['g' => 7]];
         $this->assertEquals($expected, $chunked);
     }
 
     /**
-     * Tests the chunks method with non-scalar items
+     * Tests the chunkWithKeys method with non-scalar items
      *
      * @return void
      */
-    public function testChunksNested()
+    public function testChunkWithKeysNested()
     {
-        $collection = new Collection([1, 2, 3, [4, 5], 6, [7, [8, 9], 10], 11]);
-        $chunked = $collection->chunks(2)->toList();
-        $expected = [[1, 2], [3, [4, 5]], [6, [7, [8, 9], 10]], [11]];
+        $collection = new Collection(['a' => 1, 'b' => 2, 'c' => 3, 'd' => [4, 5], 'e' => 6, 'f' => [7, [8, 9], 10], 'g' => 11]);
+        $chunked = $collection->chunkWithKeys(2)->toList();
+        $expected = [['a' => 1, 'b' => 2], ['c' => 3, 'd' => [4, 5]], ['e' => 6, 'f' => [7, [8, 9], 10]], ['g' => 11]];
         $this->assertEquals($expected, $chunked);
     }
 
     /**
-     * Tests the chunks method with preserved keys
+     * Tests the chunkWithKeys method without preserving keys
      *
      * @return void
      */
-    public function testChunksPreserveKeys()
+    public function testChunkWithKeysNoPreserveKeys()
     {
         $collection = new Collection(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6, 'g' => 7]);
-        $chunked = $collection->chunks(2, true)->toList();
-        $expected = [['a' => 1, 'b' => 2], ['c' => 3, 'd' => 4], ['e' => 5, 'f' => 6], ['g' => 7]];
+        $chunked = $collection->chunkWithKeys(2, false)->toList();
+        $expected = [[0 => 1, 1 => 2], [0 => 3, 1 => 4], [0 => 5, 1 => 6], [0 => 7]];
         $this->assertEquals($expected, $chunked);
     }