Skip to content
Permalink
Browse files

Change chunk to chunks

  • Loading branch information...
jorisvaesen authored and markstory committed Sep 23, 2016
1 parent a4cbd70 commit 572bffff5830fff9fcfd11fc75dafd7bd608ebaf
@@ -915,10 +915,27 @@ public function zipWith($items, $callable);
* ```
*
* @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
@@ -627,7 +627,27 @@ public function zipWith($items, $callable)
* {@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;
@@ -1771,14 +1771,53 @@ public function testChunkNested()
}
/**
* 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);
}

0 comments on commit 572bfff

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