Skip to content

Commit

Permalink
when() accept callable first parameter (#54005)
Browse files Browse the repository at this point in the history
  • Loading branch information
ziadoz authored Dec 27, 2024
1 parent 91c72ac commit f0bf46d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Illuminate/Collections/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ function value($value, ...$args)
*/
function when($condition, $value, $default = null)
{
$condition = $condition instanceof Closure ? $condition() : $condition;

if ($condition) {
return value($value, $condition);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ public function testWhen()
$this->assertEquals('False', when([], 'True', 'False')); // Empty Array = Falsy
$this->assertTrue(when(true, fn ($value) => $value, fn ($value) => ! $value)); // lazy evaluation
$this->assertTrue(when(false, fn ($value) => $value, fn ($value) => ! $value)); // lazy evaluation
$this->assertEquals('Hello', when(fn () => true, 'Hello')); // lazy evaluation condition
$this->assertEquals('World', when(fn () => false, 'Hello', 'World')); // lazy evaluation condition
}

public function testFilled()
Expand Down

0 comments on commit f0bf46d

Please sign in to comment.