Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
Laravel 5.4.12 changes (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
besologic authored Feb 15, 2017
1 parent de4d58f commit e1973a6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,22 @@ public function filter(callable $callback = null)
return new static(array_filter($this->items));
}

/**
* Apply the callback if the value is truthy.
*
* @param bool $value
* @param callable $callback
* @return mixed
*/
public function when($value, callable $callback)
{
if ($value) {
return $callback($this);
}

return $this;
}

/**
* Filter items by the given key value pair.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1895,6 +1895,25 @@ public function testTap()
$this->assertSame([1], $fromTap);
$this->assertSame([1, 2, 3], $collection->toArray());
}

public function testWhen()
{
$collection = new Collection(['michael', 'tom']);

$collection->when(true, function ($collection) {
return $collection->push('adam');
});

$this->assertSame(['michael', 'tom', 'adam'], $collection->toArray());

$collection = new Collection(['michael', 'tom']);

$collection->when(false, function ($collection) {
return $collection->push('adam');
});

$this->assertSame(['michael', 'tom'], $collection->toArray());
}
}

class TestSupportCollectionHigherOrderItem
Expand Down

0 comments on commit e1973a6

Please sign in to comment.