From 75da21cb0d22d96f8b1d04d94b45cab27b307353 Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Tue, 1 Jun 2021 11:05:55 -0400 Subject: [PATCH 1/4] [8.x] Use "Conditionable" in existing classes that implement when() --- .../Database/Concerns/BuildsQueries.php | 41 ++----------------- src/Illuminate/Mail/Mailable.php | 22 +--------- .../Notifications/Messages/MailMessage.php | 41 ++----------------- src/Illuminate/Support/Stringable.php | 35 +--------------- 4 files changed, 10 insertions(+), 129 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..2caeec4fef40 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 ForwardsCalls, Localizable, Conditionable; /** * 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..849dc3c1bbec 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 Macroable, Tappable, Conditionable; /** * 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. * From a1834fe8b985a11c94cb19b1a6c8218e5cd2a6fb Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Tue, 1 Jun 2021 11:20:09 -0400 Subject: [PATCH 2/4] Add full implementation of unless() --- src/Illuminate/Support/Traits/Conditionable.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) 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; } } From cec754e1e4a8564a3178f0025aca2cbe88e46c26 Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Tue, 1 Jun 2021 12:19:12 -0400 Subject: [PATCH 3/4] Reorder traits and add tests --- src/Illuminate/Mail/Mailable.php | 2 +- src/Illuminate/Support/Stringable.php | 2 +- tests/Support/SupportStringableTest.php | 28 +++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Mail/Mailable.php b/src/Illuminate/Mail/Mailable.php index 2caeec4fef40..de319fa78dcd 100644 --- a/src/Illuminate/Mail/Mailable.php +++ b/src/Illuminate/Mail/Mailable.php @@ -21,7 +21,7 @@ class Mailable implements MailableContract, Renderable { - use ForwardsCalls, Localizable, Conditionable; + use Conditionable, ForwardsCalls, Localizable; /** * The locale of the message. diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 849dc3c1bbec..7198670ff066 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -11,7 +11,7 @@ class Stringable implements JsonSerializable { - use Macroable, Tappable, Conditionable; + use Conditionable, Macroable, Tappable; /** * The underlying string value. diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index 9158d88e3fc6..2a344670f2d2 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)); From 094e764a5d578f0ca60b2d219dbe23f679e32634 Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Tue, 1 Jun 2021 12:21:42 -0400 Subject: [PATCH 4/4] StyleCI --- tests/Support/SupportStringableTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index 2a344670f2d2..9059b7dc4356 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -167,28 +167,28 @@ public function testWhenTrue() public function testUnlessTruthy() { - $this->assertSame('unless', (string) $this->stringable('unless')->unless(1, function($stringable, $value) { + $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) { + (string) $this->stringable('unless true ')->unless(1, function ($stringable, $value) { return $stringable->append($value); - }, function($stringable, $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) { + $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) { + (string) $this->stringable('gets the value ')->unless(0, function ($stringable, $value) { return $stringable->append($value); - }, function($stringable) { + }, function ($stringable) { return $stringable->append('fallbacks to default'); })); }