Skip to content

Commit

Permalink
[5.4] add default param to collection's when() method (#17941)
Browse files Browse the repository at this point in the history
* add optional $default param to when() to bring it in line with eloquent's when() method

* update collection tests for when() default param

* styleci fix

* add $default param to docblock
  • Loading branch information
tomschlick authored and taylorotwell committed Feb 15, 2017
1 parent 3a2d1aa commit 2728ea7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
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
* @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);
}

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

0 comments on commit 2728ea7

Please sign in to comment.