From 1e7da8084a5b496f29c191408117a97e39787ff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Lundb=C3=B8l?= Date: Sat, 6 Jan 2018 01:59:24 +0100 Subject: [PATCH 1/2] Regression if the first value is not countable e.g. not if type array then count throws exception/warning on php7.2. So just return an empty collection. --- src/macros/transpose.php | 8 +++++++- tests/TransposeTest.php | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/macros/transpose.php b/src/macros/transpose.php index d9121a4..e0e2856 100644 --- a/src/macros/transpose.php +++ b/src/macros/transpose.php @@ -14,7 +14,13 @@ return new static(); } - $expectedLength = count($this->first()); + $firstItem = $this->first(); + + if (! $firstItem instanceof Countable) { + return new static(); + } + + $expectedLength = count($firstItem); array_walk($this->items, function ($row) use ($expectedLength) { if (count($row) !== $expectedLength) { diff --git a/tests/TransposeTest.php b/tests/TransposeTest.php index a6a8bec..9e743bf 100644 --- a/tests/TransposeTest.php +++ b/tests/TransposeTest.php @@ -149,4 +149,16 @@ public function it_can_transpose_a_single_row_array() $this->assertEquals($expected, $collection->transpose()); } + + /** @test */ + public function it_can_handle_null_values() + { + $collection = new Collection([ + null, + ]); + + $expected = new Collection(); + + $this->assertEquals($expected, $collection->transpose()); + } } From a0ca00df57abb42759e1c7b07c11e924636f5db5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Lundb=C3=B8l?= Date: Sat, 6 Jan 2018 02:31:34 +0100 Subject: [PATCH 2/2] fix implimentation --- src/macros/transpose.php | 8 ++------ tests/TransposeTest.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/macros/transpose.php b/src/macros/transpose.php index e0e2856..c330dd5 100644 --- a/src/macros/transpose.php +++ b/src/macros/transpose.php @@ -16,14 +16,10 @@ $firstItem = $this->first(); - if (! $firstItem instanceof Countable) { - return new static(); - } - - $expectedLength = count($firstItem); + $expectedLength = is_array($firstItem) || $firstItem instanceof Countable ? count($firstItem) : 0; array_walk($this->items, function ($row) use ($expectedLength) { - if (count($row) !== $expectedLength) { + if ((is_array($row) || $row instanceof Countable) && count($row) !== $expectedLength) { throw new \LengthException("Element's length must be equal."); } }); diff --git a/tests/TransposeTest.php b/tests/TransposeTest.php index 9e743bf..46c63b0 100644 --- a/tests/TransposeTest.php +++ b/tests/TransposeTest.php @@ -161,4 +161,20 @@ public function it_can_handle_null_values() $this->assertEquals($expected, $collection->transpose()); } + + /** @test */ + public function it_can_handle_collections_values() + { + $collection = new Collection([ + new Collection([1, 2, 3]), + ]); + + $expected = new Collection([ + new Collection([1]), + new Collection([2]), + new Collection([3]), + ]); + + $this->assertEquals($expected, $collection->transpose()); + } }