Skip to content

Commit 982710a

Browse files
[11.x] add lazy default to when helper (#52747)
* add lazy default to when helper * Update helpers.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent def4668 commit 982710a

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/Illuminate/Collections/helpers.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,18 +239,19 @@ function value($value, ...$args)
239239

240240
if (! function_exists('when')) {
241241
/**
242-
* Output a value if the given condition is true.
242+
* Return a value if the given condition is true.
243243
*
244244
* @param mixed $condition
245-
* @param \Closure|mixed $output
245+
* @param \Closure|mixed $value
246+
* @param \Closure|mixed $default
246247
* @return mixed
247248
*/
248-
function when($condition, $output)
249+
function when($condition, $value, $default = null)
249250
{
250251
if ($condition) {
251-
return value($output);
252+
return value($value, $condition);
252253
}
253254

254-
return null;
255+
return value($default, $condition);
255256
}
256257
}

tests/Support/SupportHelpersTest.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,23 @@ public function testClassBasename()
103103
public function testWhen()
104104
{
105105
$this->assertEquals('Hello', when(true, 'Hello'));
106-
$this->assertEquals(null, when(false, 'Hello'));
106+
$this->assertNull(when(false, 'Hello'));
107107
$this->assertEquals('There', when(1 === 1, 'There')); // strict types
108108
$this->assertEquals('There', when(1 == '1', 'There')); // loose types
109-
$this->assertEquals(null, when(1 == 2, 'There'));
110-
$this->assertEquals(null, when('1', fn () => null));
111-
$this->assertEquals(null, when(0, fn () => null));
109+
$this->assertNull(when(1 == 2, 'There'));
110+
$this->assertNull(when('1', fn () => null));
111+
$this->assertNull(when(0, fn () => null));
112112
$this->assertEquals('True', when([1, 2, 3, 4], 'True')); // Array
113-
$this->assertEquals(null, when([], 'True')); // Empty Array = Falsy
113+
$this->assertNull(when([], 'True')); // Empty Array = Falsy
114114
$this->assertEquals('True', when(new StdClass, fn () => 'True')); // Object
115+
$this->assertEquals('World', when(false, 'Hello', 'World'));
116+
$this->assertEquals('World', when(1 === 0, 'Hello', 'World')); // strict types
117+
$this->assertEquals('World', when(1 == '0', 'Hello', 'World')); // loose types
118+
$this->assertNull(when('', fn () => 'There', fn () => null));
119+
$this->assertNull(when(0, fn () => 'There', fn () => null));
120+
$this->assertEquals('False', when([], 'True', 'False')); // Empty Array = Falsy
121+
$this->assertTrue(when(true, fn ($value) => $value, fn ($value) => ! $value)); // lazy evaluation
122+
$this->assertTrue(when(false, fn ($value) => $value, fn ($value) => ! $value)); // lazy evaluation
115123
}
116124

117125
public function testFilled()

0 commit comments

Comments
 (0)