From 8a728a1a1ceec1b24db006ebf26ccf57b66ce4e3 Mon Sep 17 00:00:00 2001 From: PH7-Jack Date: Sun, 9 May 2021 18:43:14 -0300 Subject: [PATCH 1/2] Add unless method to stringable --- src/Illuminate/Support/Stringable.php | 19 +++++++++++++++++++ tests/Support/SupportStringableTest.php | 13 +++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index a88e7afa3183..38cf1e3203c3 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -708,6 +708,25 @@ public function ucfirst() return new static(Str::ucfirst($this->value)); } + /** + * Apply the callback's string changes if the given "value" is false. + * + * @param mixed $value + * @param callable $callback + * @param callable|null $default + * @return mixed|$this + */ + public function unless($value, $callback, $default = null) + { + if (! $value) { + return $this->when(true, $callback); + } elseif ($default) { + return $this->when(false, $callback, $default); + } + + return $this; + } + /** * Apply the callback's string changes if the given "value" is true. * diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index 258d34e54072..9158d88e3fc6 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -92,6 +92,19 @@ public function testCanBeLimitedByWords() $this->assertSame('Taylor Otwell', (string) $this->stringable('Taylor Otwell')->words(3)); } + public function testUnless() + { + $this->assertSame('unless false', (string) $this->stringable('unless')->unless(false, function ($stringable, $value) { + return $stringable->append(' false'); + })); + + $this->assertSame('unless true fallbacks to default', (string) $this->stringable('unless')->unless(true, function ($stringable, $value) { + return $stringable->append($value); + }, function ($stringable) { + return $stringable->append(' true fallbacks to default'); + })); + } + public function testWhenEmpty() { tap($this->stringable(), function ($stringable) { From d9c6200c55615b8f224bb80235f354f7a0be8551 Mon Sep 17 00:00:00 2001 From: Pedro Henrique <38913462+PH7-Jack@users.noreply.github.com> Date: Mon, 10 May 2021 08:19:11 -0300 Subject: [PATCH 2/2] Update src/Illuminate/Support/Stringable.php Co-authored-by: Dinh Quoc Han --- src/Illuminate/Support/Stringable.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 38cf1e3203c3..245aaec71dfb 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -718,13 +718,7 @@ public function ucfirst() */ public function unless($value, $callback, $default = null) { - if (! $value) { - return $this->when(true, $callback); - } elseif ($default) { - return $this->when(false, $callback, $default); - } - - return $this; + return $this->when(! $value, $callback, $default); } /**