Skip to content

Commit

Permalink
fix implimentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Lundbøl committed Jan 6, 2018
1 parent 1e7da80 commit a0ca00d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/macros/transpose.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
});
Expand Down
16 changes: 16 additions & 0 deletions tests/TransposeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit a0ca00d

Please sign in to comment.