Skip to content

Commit

Permalink
Add whenEmpty + variants to Collection (#26345)
Browse files Browse the repository at this point in the history
  • Loading branch information
Propaganistas authored and taylorotwell committed Nov 1, 2018
1 parent 483bd3f commit dfe84e6
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down
128 changes: 128 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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']);
Expand Down

0 comments on commit dfe84e6

Please sign in to comment.