From 8610fc5c94b24a4eadd3680b8ffb5d05af60a951 Mon Sep 17 00:00:00 2001 From: Tom Schlick Date: Wed, 15 Feb 2017 12:39:35 -0500 Subject: [PATCH 1/4] add optional $default param to when() to bring it in line with eloquent's when() method --- src/Illuminate/Support/Collection.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 4ba71af8610a..9ecce103cc86 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -315,10 +315,12 @@ public function filter(callable $callback = null) * @param callable $callback * @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; From 58b8f8e574603d7cbe654a75f49118078269010f Mon Sep 17 00:00:00 2001 From: Tom Schlick Date: Wed, 15 Feb 2017 12:44:03 -0500 Subject: [PATCH 2/4] update collection tests for when() default param --- tests/Support/SupportCollectionTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 60e4c21f3995..9085dd0ae17a 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1914,6 +1914,20 @@ 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 From dff1c11dc33b1b1bd7b3899cc6db0eed47fb3b0a Mon Sep 17 00:00:00 2001 From: Tom Schlick Date: Wed, 15 Feb 2017 12:47:08 -0500 Subject: [PATCH 3/4] styleci fix --- tests/Support/SupportCollectionTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 9085dd0ae17a..a99d2b1477a9 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1914,7 +1914,7 @@ public function testWhen() $this->assertSame(['michael', 'tom'], $collection->toArray()); } - + public function testWhenDefault() { $collection = new Collection(['michael', 'tom']); @@ -1927,7 +1927,6 @@ public function testWhenDefault() $this->assertSame(['michael', 'tom', 'taylor'], $collection->toArray()); } - } class TestSupportCollectionHigherOrderItem From f75a44b411ab3fa2d514ed8f1448ab2e6faee147 Mon Sep 17 00:00:00 2001 From: Tom Schlick Date: Wed, 15 Feb 2017 14:15:44 -0500 Subject: [PATCH 4/4] add $default param to docblock --- src/Illuminate/Support/Collection.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 9ecce103cc86..0eef56bede69 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -313,6 +313,7 @@ public function filter(callable $callback = null) * * @param bool $value * @param callable $callback + * @param callable $default * @return mixed */ public function when($value, callable $callback, callable $default = null)