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.4] add default param to collection's when() method #17941

Merged
merged 4 commits into from
Feb 15, 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
5 changes: 4 additions & 1 deletion src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,15 @@ public function filter(callable $callback = null)
*
* @param bool $value
* @param callable $callback
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docblock needs updating as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* @param callable $default
* @return mixed
*/
public function when($value, callable $callback)
public function when($value, callable $callback, callable $default = null)
{
if ($value) {
return $callback($this);
} elseif ($default) {
return $default($this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this elseif can just be an if

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gonna leave that as is. Thats what we currently use for the eloquent QB and it works just fine for now

}

return $this;
Expand Down
13 changes: 13 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1914,6 +1914,19 @@ public function testWhen()

$this->assertSame(['michael', 'tom'], $collection->toArray());
}

public function testWhenDefault()
{
$collection = new Collection(['michael', 'tom']);

$collection->when(false, function ($collection) {
return $collection->push('adam');
}, function ($collection) {
return $collection->push('taylor');
});

$this->assertSame(['michael', 'tom', 'taylor'], $collection->toArray());
}
}

class TestSupportCollectionHigherOrderItem
Expand Down