From 93019f17ea1373a2cb3eb9747a561592730eca6b Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Tue, 1 Jun 2021 15:27:52 -0400 Subject: [PATCH] [8.x] Use "Conditionable" in existing classes that implement when() (#37561) * [8.x] Use "Conditionable" in existing classes that implement when() * Add full implementation of unless() * Reorder traits and add tests * StyleCI --- .../Database/Concerns/BuildsQueries.php | 41 ++----------------- src/Illuminate/Mail/Mailable.php | 22 +--------- .../Notifications/Messages/MailMessage.php | 41 ++----------------- src/Illuminate/Support/Stringable.php | 35 +--------------- .../Support/Traits/Conditionable.php | 12 ++++-- tests/Support/SupportStringableTest.php | 28 +++++++++++++ 6 files changed, 47 insertions(+), 132 deletions(-) diff --git a/src/Illuminate/Database/Concerns/BuildsQueries.php b/src/Illuminate/Database/Concerns/BuildsQueries.php index 3e97b7d0d4a9..461ef71156e3 100644 --- a/src/Illuminate/Database/Concerns/BuildsQueries.php +++ b/src/Illuminate/Database/Concerns/BuildsQueries.php @@ -10,11 +10,14 @@ use Illuminate\Pagination\Paginator; use Illuminate\Support\Collection; use Illuminate\Support\LazyCollection; +use Illuminate\Support\Traits\Conditionable; use InvalidArgumentException; use RuntimeException; trait BuildsQueries { + use Conditionable; + /** * Chunk the results of the query. * @@ -278,25 +281,6 @@ public function sole($columns = ['*']) return $result->first(); } - /** - * Apply the callback's query changes if the given "value" is true. - * - * @param mixed $value - * @param callable $callback - * @param callable|null $default - * @return mixed|$this - */ - public function when($value, $callback, $default = null) - { - if ($value) { - return $callback($this, $value) ?: $this; - } elseif ($default) { - return $default($this, $value) ?: $this; - } - - return $this; - } - /** * Pass the query to a given callback. * @@ -308,25 +292,6 @@ public function tap($callback) return $this->when(true, $callback); } - /** - * Apply the callback's query 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 $callback($this, $value) ?: $this; - } elseif ($default) { - return $default($this, $value) ?: $this; - } - - return $this; - } - /** * Create a new length-aware paginator instance. * diff --git a/src/Illuminate/Mail/Mailable.php b/src/Illuminate/Mail/Mailable.php index 903bd5f5f41b..de319fa78dcd 100644 --- a/src/Illuminate/Mail/Mailable.php +++ b/src/Illuminate/Mail/Mailable.php @@ -12,6 +12,7 @@ use Illuminate\Support\Collection; use Illuminate\Support\HtmlString; use Illuminate\Support\Str; +use Illuminate\Support\Traits\Conditionable; use Illuminate\Support\Traits\ForwardsCalls; use Illuminate\Support\Traits\Localizable; use PHPUnit\Framework\Assert as PHPUnit; @@ -20,7 +21,7 @@ class Mailable implements MailableContract, Renderable { - use ForwardsCalls, Localizable; + use Conditionable, ForwardsCalls, Localizable; /** * The locale of the message. @@ -990,25 +991,6 @@ public static function buildViewDataUsing(callable $callback) static::$viewDataCallback = $callback; } - /** - * Apply the callback's message changes if the given "value" is true. - * - * @param mixed $value - * @param callable $callback - * @param mixed $default - * @return mixed|$this - */ - public function when($value, $callback, $default = null) - { - if ($value) { - return $callback($this, $value) ?: $this; - } elseif ($default) { - return $default($this, $value) ?: $this; - } - - return $this; - } - /** * Dynamically bind parameters to the message. * diff --git a/src/Illuminate/Notifications/Messages/MailMessage.php b/src/Illuminate/Notifications/Messages/MailMessage.php index 08e79d0fa0f5..94342f30b2bc 100644 --- a/src/Illuminate/Notifications/Messages/MailMessage.php +++ b/src/Illuminate/Notifications/Messages/MailMessage.php @@ -6,9 +6,12 @@ use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Renderable; use Illuminate\Mail\Markdown; +use Illuminate\Support\Traits\Conditionable; class MailMessage extends SimpleMessage implements Renderable { + use Conditionable; + /** * The view to be rendered. * @@ -330,42 +333,4 @@ public function withSwiftMessage($callback) return $this; } - - /** - * Apply the callback's message changes if the given "value" is true. - * - * @param mixed $value - * @param callable $callback - * @param callable|null $default - * @return mixed|$this - */ - public function when($value, $callback, $default = null) - { - if ($value) { - return $callback($this, $value) ?: $this; - } elseif ($default) { - return $default($this, $value) ?: $this; - } - - return $this; - } - - /** - * Apply the callback's message 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 $callback($this, $value) ?: $this; - } elseif ($default) { - return $default($this, $value) ?: $this; - } - - return $this; - } } diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 245aaec71dfb..7198670ff066 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -3,6 +3,7 @@ namespace Illuminate\Support; use Closure; +use Illuminate\Support\Traits\Conditionable; use Illuminate\Support\Traits\Macroable; use Illuminate\Support\Traits\Tappable; use JsonSerializable; @@ -10,7 +11,7 @@ class Stringable implements JsonSerializable { - use Macroable, Tappable; + use Conditionable, Macroable, Tappable; /** * The underlying string value. @@ -708,38 +709,6 @@ 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) - { - return $this->when(! $value, $callback, $default); - } - - /** - * Apply the callback's string changes if the given "value" is true. - * - * @param mixed $value - * @param callable $callback - * @param callable|null $default - * @return mixed|$this - */ - public function when($value, $callback, $default = null) - { - if ($value) { - return $callback($this, $value) ?: $this; - } elseif ($default) { - return $default($this, $value) ?: $this; - } - - return $this; - } - /** * Execute the given callback if the string is empty. * diff --git a/src/Illuminate/Support/Traits/Conditionable.php b/src/Illuminate/Support/Traits/Conditionable.php index d1cd23d9b2a7..183d63c66f02 100644 --- a/src/Illuminate/Support/Traits/Conditionable.php +++ b/src/Illuminate/Support/Traits/Conditionable.php @@ -5,7 +5,7 @@ trait Conditionable { /** - * Apply the callback if the given "value" is true. + * Apply the callback if the given "value" is truthy. * * @param mixed $value * @param callable $callback @@ -25,7 +25,7 @@ public function when($value, $callback, $default = null) } /** - * Apply the callback if the given "value" is false. + * Apply the callback if the given "value" is falsy. * * @param mixed $value * @param callable $callback @@ -35,6 +35,12 @@ public function when($value, $callback, $default = null) */ public function unless($value, $callback, $default = null) { - return $this->when(! $value, $callback, $default); + if (! $value) { + return $callback($this, $value) ?: $this; + } elseif ($default) { + return $default($this, $value) ?: $this; + } + + return $this; } } diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index 9158d88e3fc6..9059b7dc4356 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -165,6 +165,34 @@ public function testWhenTrue() })); } + public function testUnlessTruthy() + { + $this->assertSame('unless', (string) $this->stringable('unless')->unless(1, function ($stringable, $value) { + return $stringable->append($value)->append('true'); + })); + + $this->assertSame('unless true fallbacks to default with value 1', + (string) $this->stringable('unless true ')->unless(1, function ($stringable, $value) { + return $stringable->append($value); + }, function ($stringable, $value) { + return $stringable->append('fallbacks to default with value ')->append($value); + })); + } + + public function testUnlessFalsy() + { + $this->assertSame('unless 0', (string) $this->stringable('unless ')->unless(0, function ($stringable, $value) { + return $stringable->append($value); + })); + + $this->assertSame('gets the value 0', + (string) $this->stringable('gets the value ')->unless(0, function ($stringable, $value) { + return $stringable->append($value); + }, function ($stringable) { + return $stringable->append('fallbacks to default'); + })); + } + public function testTrimmedOnlyWhereNecessary() { $this->assertSame(' Taylor Otwell ', (string) $this->stringable(' Taylor Otwell ')->words(3));