Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ All of these methods manipulate the array, but not all of them return an instanc
`->shift()` removes the first element from the array and returns it.

- `->changeKeyCase()`
- `->chunk(int[, array])`
- `->combine(array|Chain, array|Chain)`
- `->count()`
- `->diff(array|Chain)`
Expand Down
2 changes: 2 additions & 0 deletions src/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cocur\Chain;

use Cocur\Chain\Link\ChangeKeyCase;
use Cocur\Chain\Link\Chunk;
use Cocur\Chain\Link\Combine;
use Cocur\Chain\Link\Count;
use Cocur\Chain\Link\CountValues;
Expand Down Expand Up @@ -54,6 +55,7 @@
class Chain extends AbstractChain implements Countable
{
use ChangeKeyCase;
use Chunk;
use Combine;
use Count;
use CountValues;
Expand Down
36 changes: 36 additions & 0 deletions src/Link/Chunk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Cocur\Chain\Link;

/**
* Chunk.
*
* @author Nicolas Reynis
*/
trait Chunk
{
/**
* @param int $size
* @param array $options options, including:
* bool `preserveKeys` to prevent reindexing, default to false
* bool `decorate` to generate chains instead of arrays, default tu true
*
* @return self
*/
public function chunk(int $size, array $options = []): self
{
if (!empty($options['preserveKeys'])) {
$this->array = array_chunk($this->array, $size, $options['preserveKeys']);
} else {
$this->array = array_chunk($this->array, $size);
}

if (empty($options['decorate']) || true === $options['decorate']) {
foreach ($this->array as $index => $chunk) {
$this->array[$index] = new static($chunk);
}
}

return $this;
}
}
1 change: 1 addition & 0 deletions tests/ChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public function chainHasTraits(): void
$c = new Chain();

$this->assertTrue(method_exists($c, 'changeKeyCase'));
$this->assertTrue(method_exists($c, 'chunk'));
$this->assertTrue(method_exists($c, 'combine'));
$this->assertTrue(method_exists($c, 'count'));
$this->assertTrue(method_exists($c, 'countValues'));
Expand Down
27 changes: 27 additions & 0 deletions tests/Link/ChunkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Cocur\Chain\Link;

/**
* EveryTest.
*
* @author Nicolas Reynis
* @group unit
*/
class ChunkTest extends \PHPUnit\Framework\TestCase
{
/**
* @test
* @covers \Cocur\Chain\Link\Chunk::chunk()
*/
public function everyReturnTrueWhenConditionPass(): void
{
/** @var Every $mock */
$mock = $this->getMockForTrait(Chunk::class);
$mock->array = [1, 2, 3, 4];
$mock->chunk(2);

$this->assertEquals([1, 2], $mock->array[0]->array);
$this->assertEquals([3, 4], $mock->array[1]->array);
}
}