Browse Source

Change chunk to chunks

Joris Vaesen 9 years ago
parent
commit
572bffff58

+ 19 - 2
src/Collection/CollectionInterface.php

@@ -915,10 +915,27 @@ interface CollectionInterface extends Iterator, JsonSerializable
      * ```
      *
      * @param int $chunkSize The maximum size for each chunk
-     * @param boolean $preserveKeys If the keys of the array should be preserved
      * @return \Cake\Collection\CollectionInterface
+     * @deprecated 4.0.0 Deprecated in favor of chunks
      */
-    public function chunk($chunkSize, $preserveKeys = false);
+    public function chunk($chunkSize);
+
+    /**
+     * Breaks the collection into smaller arrays of the given size.
+     *
+     * ### 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]]
+     * ```
+     *
+     * @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);
 
     /**
      * Returns whether or not there are elements in this collection

+ 21 - 1
src/Collection/CollectionTrait.php

@@ -627,7 +627,27 @@ trait CollectionTrait
      * {@inheritDoc}
      *
      */
-    public function chunk($chunkSize, $preserveKeys = false)
+    public function chunk($chunkSize)
+    {
+        return $this->map(function ($v, $k, $iterator) use ($chunkSize) {
+            $values = [$v];
+            for ($i = 1; $i < $chunkSize; $i++) {
+                $iterator->next();
+                if (!$iterator->valid()) {
+                    break;
+                }
+                $values[] = $iterator->current();
+            }
+
+            return $values;
+        });
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     */
+    public function chunks($chunkSize, $preserveKeys = false)
     {
         return $this->map(function ($v, $k, $iterator) use ($chunkSize, $preserveKeys) {
             $key = 0;

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

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