Skip to content

Commit dfe84e6

Browse files
Propaganistastaylorotwell
authored andcommitted
Add whenEmpty + variants to Collection (#26345)
1 parent 483bd3f commit dfe84e6

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed

src/Illuminate/Support/Collection.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,32 @@ public function when($value, callable $callback, callable $default = null)
501501
return $this;
502502
}
503503

504+
/**
505+
* Apply the callback if the collection is empty.
506+
*
507+
* @param bool $value
508+
* @param callable $callback
509+
* @param callable $default
510+
* @return static|mixed
511+
*/
512+
public function whenEmpty(callable $callback, callable $default = null)
513+
{
514+
return $this->when($this->isEmpty(), $callback, $default);
515+
}
516+
517+
/**
518+
* Apply the callback if the collection is not empty.
519+
*
520+
* @param bool $value
521+
* @param callable $callback
522+
* @param callable $default
523+
* @return static|mixed
524+
*/
525+
public function whenNotEmpty(callable $callback, callable $default = null)
526+
{
527+
return $this->when($this->isNotEmpty(), $callback, $default);
528+
}
529+
504530
/**
505531
* Apply the callback if the value is falsy.
506532
*
@@ -514,6 +540,32 @@ public function unless($value, callable $callback, callable $default = null)
514540
return $this->when(! $value, $callback, $default);
515541
}
516542

543+
/**
544+
* Apply the callback unless the collection is empty.
545+
*
546+
* @param bool $value
547+
* @param callable $callback
548+
* @param callable $default
549+
* @return static|mixed
550+
*/
551+
public function unlessEmpty(callable $callback, callable $default = null)
552+
{
553+
return $this->unless($this->isEmpty(), $callback, $default);
554+
}
555+
556+
/**
557+
* Apply the callback unless the collection is not empty.
558+
*
559+
* @param bool $value
560+
* @param callable $callback
561+
* @param callable $default
562+
* @return static|mixed
563+
*/
564+
public function unlessNotEmpty(callable $callback, callable $default = null)
565+
{
566+
return $this->unless($this->isNotEmpty(), $callback, $default);
567+
}
568+
517569
/**
518570
* Filter items by the given key value pair.
519571
*

tests/Support/SupportCollectionTest.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2693,6 +2693,70 @@ public function testWhenDefault()
26932693
$this->assertSame(['michael', 'tom', 'taylor'], $collection->toArray());
26942694
}
26952695

2696+
public function testWhenEmpty()
2697+
{
2698+
$collection = new Collection(['michael', 'tom']);
2699+
2700+
$collection->whenEmpty(function ($collection) {
2701+
return $collection->push('adam');
2702+
});
2703+
2704+
$this->assertSame(['michael', 'tom'], $collection->toArray());
2705+
2706+
$collection = new Collection;
2707+
2708+
$collection->whenEmpty(function ($collection) {
2709+
return $collection->push('adam');
2710+
});
2711+
2712+
$this->assertSame(['adam'], $collection->toArray());
2713+
}
2714+
2715+
public function testWhenEmptyDefault()
2716+
{
2717+
$collection = new Collection(['michael', 'tom']);
2718+
2719+
$collection->whenEmpty(function ($collection) {
2720+
return $collection->push('adam');
2721+
}, function ($collection) {
2722+
return $collection->push('taylor');
2723+
});
2724+
2725+
$this->assertSame(['michael', 'tom', 'taylor'], $collection->toArray());
2726+
}
2727+
2728+
public function testWhenNotEmpty()
2729+
{
2730+
$collection = new Collection(['michael', 'tom']);
2731+
2732+
$collection->whenNotEmpty(function ($collection) {
2733+
return $collection->push('adam');
2734+
});
2735+
2736+
$this->assertSame(['michael', 'tom', 'adam'], $collection->toArray());
2737+
2738+
$collection = new Collection;
2739+
2740+
$collection->whenNotEmpty(function ($collection) {
2741+
return $collection->push('adam');
2742+
});
2743+
2744+
$this->assertSame([], $collection->toArray());
2745+
}
2746+
2747+
public function testWhenNotEmptyDefault()
2748+
{
2749+
$collection = new Collection(['michael', 'tom']);
2750+
2751+
$collection->whenNotEmpty(function ($collection) {
2752+
return $collection->push('adam');
2753+
}, function ($collection) {
2754+
return $collection->push('taylor');
2755+
});
2756+
2757+
$this->assertSame(['michael', 'tom', 'adam'], $collection->toArray());
2758+
}
2759+
26962760
public function testUnless()
26972761
{
26982762
$collection = new Collection(['michael', 'tom']);
@@ -2725,6 +2789,70 @@ public function testUnlessDefault()
27252789
$this->assertSame(['michael', 'tom', 'taylor'], $collection->toArray());
27262790
}
27272791

2792+
public function testUnlessEmpty()
2793+
{
2794+
$collection = new Collection(['michael', 'tom']);
2795+
2796+
$collection->unlessEmpty(function ($collection) {
2797+
return $collection->push('adam');
2798+
});
2799+
2800+
$this->assertSame(['michael', 'tom', 'adam'], $collection->toArray());
2801+
2802+
$collection = new Collection;
2803+
2804+
$collection->unlessEmpty(function ($collection) {
2805+
return $collection->push('adam');
2806+
});
2807+
2808+
$this->assertSame([], $collection->toArray());
2809+
}
2810+
2811+
public function testUnlessEmptyDefault()
2812+
{
2813+
$collection = new Collection(['michael', 'tom']);
2814+
2815+
$collection->unlessEmpty(function ($collection) {
2816+
return $collection->push('adam');
2817+
}, function ($collection) {
2818+
return $collection->push('taylor');
2819+
});
2820+
2821+
$this->assertSame(['michael', 'tom', 'adam'], $collection->toArray());
2822+
}
2823+
2824+
public function testUnlessNotEmpty()
2825+
{
2826+
$collection = new Collection(['michael', 'tom']);
2827+
2828+
$collection->unlessNotEmpty(function ($collection) {
2829+
return $collection->push('adam');
2830+
});
2831+
2832+
$this->assertSame(['michael', 'tom'], $collection->toArray());
2833+
2834+
$collection = new Collection;
2835+
2836+
$collection->unlessNotEmpty(function ($collection) {
2837+
return $collection->push('adam');
2838+
});
2839+
2840+
$this->assertSame(['adam'], $collection->toArray());
2841+
}
2842+
2843+
public function testUnlessNotEmptyDefault()
2844+
{
2845+
$collection = new Collection(['michael', 'tom']);
2846+
2847+
$collection->unlessNotEmpty(function ($collection) {
2848+
return $collection->push('adam');
2849+
}, function ($collection) {
2850+
return $collection->push('taylor');
2851+
});
2852+
2853+
$this->assertSame(['michael', 'tom', 'taylor'], $collection->toArray());
2854+
}
2855+
27282856
public function testHasReturnsValidResults()
27292857
{
27302858
$collection = new Collection(['foo' => 'one', 'bar' => 'two', 1 => 'three']);

0 commit comments

Comments
 (0)