From b07c1797db69226bfd333b6e37dca6148b7d2dc6 Mon Sep 17 00:00:00 2001 From: Nicolas Reynis Date: Sat, 23 May 2020 13:52:41 +0200 Subject: [PATCH 1/5] wip --- README.md | 1 + src/Chain.php | 2 ++ src/Link/Chunk.php | 28 ++++++++++++++++++++++++++++ tests/ChainTest.php | 1 + tests/Link/ChunkTest.php | 27 +++++++++++++++++++++++++++ 5 files changed, 59 insertions(+) create mode 100644 src/Link/Chunk.php create mode 100644 tests/Link/ChunkTest.php diff --git a/README.md b/README.md index 84de644..934ac8e 100644 --- a/README.md +++ b/README.md @@ -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)` diff --git a/src/Chain.php b/src/Chain.php index d445db7..5c42478 100644 --- a/src/Chain.php +++ b/src/Chain.php @@ -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; @@ -54,6 +55,7 @@ class Chain extends AbstractChain implements Countable { use ChangeKeyCase; + use Chunk; use Combine; use Count; use CountValues; diff --git a/src/Link/Chunk.php b/src/Link/Chunk.php new file mode 100644 index 0000000..a41b57d --- /dev/null +++ b/src/Link/Chunk.php @@ -0,0 +1,28 @@ +array = array_chunk($this->array, $size, $options['preserveKeys']); + } else { + $this->array = array_chunk($this->array, $size); + } + + return $this; + } +} diff --git a/tests/ChainTest.php b/tests/ChainTest.php index 428e221..85f9ce6 100644 --- a/tests/ChainTest.php +++ b/tests/ChainTest.php @@ -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')); diff --git a/tests/Link/ChunkTest.php b/tests/Link/ChunkTest.php new file mode 100644 index 0000000..c61da14 --- /dev/null +++ b/tests/Link/ChunkTest.php @@ -0,0 +1,27 @@ +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); + } +} From cf321f15c060a38848809fc642d48d67f674c940 Mon Sep 17 00:00:00 2001 From: Nicolas Reynis Date: Sun, 24 May 2020 13:53:05 +0200 Subject: [PATCH 2/5] fix --- src/Link/Chunk.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Link/Chunk.php b/src/Link/Chunk.php index a41b57d..728e82e 100644 --- a/src/Link/Chunk.php +++ b/src/Link/Chunk.php @@ -10,7 +10,7 @@ trait Chunk { /** - * @param int $size + * @param int $size * @param array $options options, including `preserveKeys` to prevent reindexing * * @return self From 860e1983bef2026578fa725e8b9a7d53300a1c4b Mon Sep 17 00:00:00 2001 From: Nicolas Reynis Date: Sun, 24 May 2020 14:06:24 +0200 Subject: [PATCH 3/5] fix --- src/Link/Chunk.php | 10 +++++++++- tests/Link/ChunkTest.php | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Link/Chunk.php b/src/Link/Chunk.php index 728e82e..7c5d164 100644 --- a/src/Link/Chunk.php +++ b/src/Link/Chunk.php @@ -11,7 +11,9 @@ trait Chunk { /** * @param int $size - * @param array $options options, including `preserveKeys` to prevent reindexing + * @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 */ @@ -23,6 +25,12 @@ public function chunk(int $size, array $options = []): self $this->array = array_chunk($this->array, $size); } + if (empty($options['decorate']) || $options['decorate'] === true) { + foreach ($this->array as $index => $chunk) { + $this->array[$index] = new static($chunk); + } + } + return $this; } } diff --git a/tests/Link/ChunkTest.php b/tests/Link/ChunkTest.php index c61da14..8f929a1 100644 --- a/tests/Link/ChunkTest.php +++ b/tests/Link/ChunkTest.php @@ -1,6 +1,6 @@ Date: Sun, 24 May 2020 14:07:30 +0200 Subject: [PATCH 4/5] fix --- src/Link/Chunk.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Link/Chunk.php b/src/Link/Chunk.php index 7c5d164..94429eb 100644 --- a/src/Link/Chunk.php +++ b/src/Link/Chunk.php @@ -12,8 +12,8 @@ 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 + * bool `preserveKeys` to prevent reindexing, default to false + * bool `decorate` to generate chains instead of arrays, default tu true * * @return self */ From 9d42cc9024c31d998f13fafda8593edb2cece772 Mon Sep 17 00:00:00 2001 From: Nicolas Reynis Date: Sun, 24 May 2020 14:08:27 +0200 Subject: [PATCH 5/5] fix --- src/Link/Chunk.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Link/Chunk.php b/src/Link/Chunk.php index 94429eb..822b3ed 100644 --- a/src/Link/Chunk.php +++ b/src/Link/Chunk.php @@ -25,7 +25,7 @@ public function chunk(int $size, array $options = []): self $this->array = array_chunk($this->array, $size); } - if (empty($options['decorate']) || $options['decorate'] === true) { + if (empty($options['decorate']) || true === $options['decorate']) { foreach ($this->array as $index => $chunk) { $this->array[$index] = new static($chunk); }