Skip to content

Commit

Permalink
Merge a0ca00d into 0ac7398
Browse files Browse the repository at this point in the history
  • Loading branch information
morloderex committed Jan 6, 2018
2 parents 0ac7398 + a0ca00d commit 1b88219
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/macros/transpose.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
return new static();
}

$expectedLength = count($this->first());
$firstItem = $this->first();

$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.");
}
});
Expand Down
28 changes: 28 additions & 0 deletions tests/TransposeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,32 @@ 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());
}

/** @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());
}
}

0 comments on commit 1b88219

Please sign in to comment.