diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 4f25bacc05ce..c12961d2433d 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -501,6 +501,32 @@ public function when($value, callable $callback, callable $default = null) return $this; } + /** + * Apply the callback if the collection is empty. + * + * @param bool $value + * @param callable $callback + * @param callable $default + * @return static|mixed + */ + public function whenEmpty(callable $callback, callable $default = null) + { + return $this->when($this->isEmpty(), $callback, $default); + } + + /** + * Apply the callback if the collection is not empty. + * + * @param bool $value + * @param callable $callback + * @param callable $default + * @return static|mixed + */ + public function whenNotEmpty(callable $callback, callable $default = null) + { + return $this->when($this->isNotEmpty(), $callback, $default); + } + /** * Apply the callback if the value is falsy. * @@ -514,6 +540,32 @@ public function unless($value, callable $callback, callable $default = null) return $this->when(! $value, $callback, $default); } + /** + * Apply the callback unless the collection is empty. + * + * @param bool $value + * @param callable $callback + * @param callable $default + * @return static|mixed + */ + public function unlessEmpty(callable $callback, callable $default = null) + { + return $this->unless($this->isEmpty(), $callback, $default); + } + + /** + * Apply the callback unless the collection is not empty. + * + * @param bool $value + * @param callable $callback + * @param callable $default + * @return static|mixed + */ + public function unlessNotEmpty(callable $callback, callable $default = null) + { + return $this->unless($this->isNotEmpty(), $callback, $default); + } + /** * Filter items by the given key value pair. * diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 975cbd9045d6..06fff7a0e22b 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -2693,6 +2693,70 @@ public function testWhenDefault() $this->assertSame(['michael', 'tom', 'taylor'], $collection->toArray()); } + public function testWhenEmpty() + { + $collection = new Collection(['michael', 'tom']); + + $collection->whenEmpty(function ($collection) { + return $collection->push('adam'); + }); + + $this->assertSame(['michael', 'tom'], $collection->toArray()); + + $collection = new Collection; + + $collection->whenEmpty(function ($collection) { + return $collection->push('adam'); + }); + + $this->assertSame(['adam'], $collection->toArray()); + } + + public function testWhenEmptyDefault() + { + $collection = new Collection(['michael', 'tom']); + + $collection->whenEmpty(function ($collection) { + return $collection->push('adam'); + }, function ($collection) { + return $collection->push('taylor'); + }); + + $this->assertSame(['michael', 'tom', 'taylor'], $collection->toArray()); + } + + public function testWhenNotEmpty() + { + $collection = new Collection(['michael', 'tom']); + + $collection->whenNotEmpty(function ($collection) { + return $collection->push('adam'); + }); + + $this->assertSame(['michael', 'tom', 'adam'], $collection->toArray()); + + $collection = new Collection; + + $collection->whenNotEmpty(function ($collection) { + return $collection->push('adam'); + }); + + $this->assertSame([], $collection->toArray()); + } + + public function testWhenNotEmptyDefault() + { + $collection = new Collection(['michael', 'tom']); + + $collection->whenNotEmpty(function ($collection) { + return $collection->push('adam'); + }, function ($collection) { + return $collection->push('taylor'); + }); + + $this->assertSame(['michael', 'tom', 'adam'], $collection->toArray()); + } + public function testUnless() { $collection = new Collection(['michael', 'tom']); @@ -2725,6 +2789,70 @@ public function testUnlessDefault() $this->assertSame(['michael', 'tom', 'taylor'], $collection->toArray()); } + public function testUnlessEmpty() + { + $collection = new Collection(['michael', 'tom']); + + $collection->unlessEmpty(function ($collection) { + return $collection->push('adam'); + }); + + $this->assertSame(['michael', 'tom', 'adam'], $collection->toArray()); + + $collection = new Collection; + + $collection->unlessEmpty(function ($collection) { + return $collection->push('adam'); + }); + + $this->assertSame([], $collection->toArray()); + } + + public function testUnlessEmptyDefault() + { + $collection = new Collection(['michael', 'tom']); + + $collection->unlessEmpty(function ($collection) { + return $collection->push('adam'); + }, function ($collection) { + return $collection->push('taylor'); + }); + + $this->assertSame(['michael', 'tom', 'adam'], $collection->toArray()); + } + + public function testUnlessNotEmpty() + { + $collection = new Collection(['michael', 'tom']); + + $collection->unlessNotEmpty(function ($collection) { + return $collection->push('adam'); + }); + + $this->assertSame(['michael', 'tom'], $collection->toArray()); + + $collection = new Collection; + + $collection->unlessNotEmpty(function ($collection) { + return $collection->push('adam'); + }); + + $this->assertSame(['adam'], $collection->toArray()); + } + + public function testUnlessNotEmptyDefault() + { + $collection = new Collection(['michael', 'tom']); + + $collection->unlessNotEmpty(function ($collection) { + return $collection->push('adam'); + }, function ($collection) { + return $collection->push('taylor'); + }); + + $this->assertSame(['michael', 'tom', 'taylor'], $collection->toArray()); + } + public function testHasReturnsValidResults() { $collection = new Collection(['foo' => 'one', 'bar' => 'two', 1 => 'three']);