Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Fix eachSpread mapSpread with nested collections #20962

Merged
merged 1 commit into from
Sep 4, 2017
Merged
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
4 changes: 2 additions & 2 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ public function each(callable $callback)
public function eachSpread(callable $callback)
{
return $this->each(function ($chunk, $key) use ($callback) {
array_push($chunk, $key);
$chunk[] = $key;

return $callback(...$chunk);
});
Expand Down Expand Up @@ -844,7 +844,7 @@ public function map(callable $callback)
public function mapSpread(callable $callback)
{
return $this->map(function ($chunk, $key) use ($callback) {
array_push($chunk, $key);
$chunk[] = $key;

return $callback(...$chunk);
});
Expand Down
13 changes: 13 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,13 @@ public function testEachSpread()
$result[] = [$number, $character, $key];
});
$this->assertEquals([[1, 'a', 0], [2, 'b', 1]], $result);

$c = new Collection([new Collection([1, 'a']), new Collection([2, 'b'])]);
$result = [];
$c->eachSpread(function ($number, $character, $key) use (&$result) {
$result[] = [$number, $character, $key];
});
$this->assertEquals([[1, 'a', 0], [2, 'b', 1]], $result);
}

public function testIntersectNull()
Expand Down Expand Up @@ -1225,6 +1232,12 @@ public function testMapSpread()
return "{$number}-{$character}-{$key}";
});
$this->assertEquals(['1-a-0', '2-b-1'], $result->all());

$c = new Collection([new Collection([1, 'a']), new Collection([2, 'b'])]);
$result = $c->mapSpread(function ($number, $character, $key) use (&$result) {
return "{$number}-{$character}-{$key}";
});
$this->assertEquals(['1-a-0', '2-b-1'], $result->all());
}

public function testFlatMap()
Expand Down