From 02f2c23cc4d0f946980a554970af677ca932ed53 Mon Sep 17 00:00:00 2001 From: Rodrigo Pedra Brum Date: Wed, 11 Sep 2024 19:26:20 -0300 Subject: [PATCH 1/2] add lazy default to when helper --- src/Illuminate/Collections/helpers.php | 9 +++++---- tests/Support/SupportHelpersTest.php | 18 +++++++++++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/Illuminate/Collections/helpers.php b/src/Illuminate/Collections/helpers.php index 58dcecfb1452..132eb0df9def 100644 --- a/src/Illuminate/Collections/helpers.php +++ b/src/Illuminate/Collections/helpers.php @@ -242,15 +242,16 @@ function value($value, ...$args) * Output a value if the given condition is true. * * @param mixed $condition - * @param \Closure|mixed $output + * @param \Closure|mixed $value + * @param \Closure|mixed $default * @return mixed */ - function when($condition, $output) + function when($condition, $value, $default = null) { if ($condition) { - return value($output); + return value($value, $condition); } - return null; + return value($default, $condition); } } diff --git a/tests/Support/SupportHelpersTest.php b/tests/Support/SupportHelpersTest.php index 69877bfb3a2a..f750ab4518c9 100644 --- a/tests/Support/SupportHelpersTest.php +++ b/tests/Support/SupportHelpersTest.php @@ -103,15 +103,23 @@ public function testClassBasename() public function testWhen() { $this->assertEquals('Hello', when(true, 'Hello')); - $this->assertEquals(null, when(false, 'Hello')); + $this->assertNull(when(false, 'Hello')); $this->assertEquals('There', when(1 === 1, 'There')); // strict types $this->assertEquals('There', when(1 == '1', 'There')); // loose types - $this->assertEquals(null, when(1 == 2, 'There')); - $this->assertEquals(null, when('1', fn () => null)); - $this->assertEquals(null, when(0, fn () => null)); + $this->assertNull(when(1 == 2, 'There')); + $this->assertNull(when('1', fn () => null)); + $this->assertNull(when(0, fn () => null)); $this->assertEquals('True', when([1, 2, 3, 4], 'True')); // Array - $this->assertEquals(null, when([], 'True')); // Empty Array = Falsy + $this->assertNull(when([], 'True')); // Empty Array = Falsy $this->assertEquals('True', when(new StdClass, fn () => 'True')); // Object + $this->assertEquals('World', when(false, 'Hello', 'World')); + $this->assertEquals('World', when(1 === 0, 'Hello', 'World')); // strict types + $this->assertEquals('World', when(1 == '0', 'Hello', 'World')); // loose types + $this->assertNull(when('', fn () => 'There', fn () => null)); + $this->assertNull(when(0, fn () => 'There', fn () => null)); + $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 } public function testFilled() From ab00d61d7b8bd8307db0a544a187272d2d236897 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 12 Sep 2024 09:45:40 -0500 Subject: [PATCH 2/2] Update helpers.php --- src/Illuminate/Collections/helpers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Collections/helpers.php b/src/Illuminate/Collections/helpers.php index 132eb0df9def..ef9c45e2a79b 100644 --- a/src/Illuminate/Collections/helpers.php +++ b/src/Illuminate/Collections/helpers.php @@ -239,7 +239,7 @@ function value($value, ...$args) if (! function_exists('when')) { /** - * Output a value if the given condition is true. + * Return a value if the given condition is true. * * @param mixed $condition * @param \Closure|mixed $value