Skip to content
Permalink
Browse files

rename to chunkWithKeys

  • Loading branch information...
jorisvaesen authored and markstory committed Sep 26, 2016
1 parent 572bfff commit 17fcc701c0fa485c8176765483f5baca1e047d5f
@@ -926,16 +926,16 @@ public function chunk($chunkSize);
* ### 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
@@ -647,7 +647,7 @@ public function chunk($chunkSize)
* {@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;
@@ -1771,54 +1771,54 @@ public function testChunkNested()
}
/**
* 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);
}

0 comments on commit 17fcc70

Please sign in to comment.
You can’t perform that action at this time.