Skip to content

Commit 93019f1

Browse files
authored
[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
1 parent 71a83fb commit 93019f1

File tree

6 files changed

+47
-132
lines changed

6 files changed

+47
-132
lines changed

src/Illuminate/Database/Concerns/BuildsQueries.php

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
use Illuminate\Pagination\Paginator;
1111
use Illuminate\Support\Collection;
1212
use Illuminate\Support\LazyCollection;
13+
use Illuminate\Support\Traits\Conditionable;
1314
use InvalidArgumentException;
1415
use RuntimeException;
1516

1617
trait BuildsQueries
1718
{
19+
use Conditionable;
20+
1821
/**
1922
* Chunk the results of the query.
2023
*
@@ -278,25 +281,6 @@ public function sole($columns = ['*'])
278281
return $result->first();
279282
}
280283

281-
/**
282-
* Apply the callback's query changes if the given "value" is true.
283-
*
284-
* @param mixed $value
285-
* @param callable $callback
286-
* @param callable|null $default
287-
* @return mixed|$this
288-
*/
289-
public function when($value, $callback, $default = null)
290-
{
291-
if ($value) {
292-
return $callback($this, $value) ?: $this;
293-
} elseif ($default) {
294-
return $default($this, $value) ?: $this;
295-
}
296-
297-
return $this;
298-
}
299-
300284
/**
301285
* Pass the query to a given callback.
302286
*
@@ -308,25 +292,6 @@ public function tap($callback)
308292
return $this->when(true, $callback);
309293
}
310294

311-
/**
312-
* Apply the callback's query changes if the given "value" is false.
313-
*
314-
* @param mixed $value
315-
* @param callable $callback
316-
* @param callable|null $default
317-
* @return mixed|$this
318-
*/
319-
public function unless($value, $callback, $default = null)
320-
{
321-
if (! $value) {
322-
return $callback($this, $value) ?: $this;
323-
} elseif ($default) {
324-
return $default($this, $value) ?: $this;
325-
}
326-
327-
return $this;
328-
}
329-
330295
/**
331296
* Create a new length-aware paginator instance.
332297
*

src/Illuminate/Mail/Mailable.php

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Illuminate\Support\Collection;
1313
use Illuminate\Support\HtmlString;
1414
use Illuminate\Support\Str;
15+
use Illuminate\Support\Traits\Conditionable;
1516
use Illuminate\Support\Traits\ForwardsCalls;
1617
use Illuminate\Support\Traits\Localizable;
1718
use PHPUnit\Framework\Assert as PHPUnit;
@@ -20,7 +21,7 @@
2021

2122
class Mailable implements MailableContract, Renderable
2223
{
23-
use ForwardsCalls, Localizable;
24+
use Conditionable, ForwardsCalls, Localizable;
2425

2526
/**
2627
* The locale of the message.
@@ -990,25 +991,6 @@ public static function buildViewDataUsing(callable $callback)
990991
static::$viewDataCallback = $callback;
991992
}
992993

993-
/**
994-
* Apply the callback's message changes if the given "value" is true.
995-
*
996-
* @param mixed $value
997-
* @param callable $callback
998-
* @param mixed $default
999-
* @return mixed|$this
1000-
*/
1001-
public function when($value, $callback, $default = null)
1002-
{
1003-
if ($value) {
1004-
return $callback($this, $value) ?: $this;
1005-
} elseif ($default) {
1006-
return $default($this, $value) ?: $this;
1007-
}
1008-
1009-
return $this;
1010-
}
1011-
1012994
/**
1013995
* Dynamically bind parameters to the message.
1014996
*

src/Illuminate/Notifications/Messages/MailMessage.php

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
use Illuminate\Contracts\Support\Arrayable;
77
use Illuminate\Contracts\Support\Renderable;
88
use Illuminate\Mail\Markdown;
9+
use Illuminate\Support\Traits\Conditionable;
910

1011
class MailMessage extends SimpleMessage implements Renderable
1112
{
13+
use Conditionable;
14+
1215
/**
1316
* The view to be rendered.
1417
*
@@ -330,42 +333,4 @@ public function withSwiftMessage($callback)
330333

331334
return $this;
332335
}
333-
334-
/**
335-
* Apply the callback's message changes if the given "value" is true.
336-
*
337-
* @param mixed $value
338-
* @param callable $callback
339-
* @param callable|null $default
340-
* @return mixed|$this
341-
*/
342-
public function when($value, $callback, $default = null)
343-
{
344-
if ($value) {
345-
return $callback($this, $value) ?: $this;
346-
} elseif ($default) {
347-
return $default($this, $value) ?: $this;
348-
}
349-
350-
return $this;
351-
}
352-
353-
/**
354-
* Apply the callback's message changes if the given "value" is false.
355-
*
356-
* @param mixed $value
357-
* @param callable $callback
358-
* @param callable|null $default
359-
* @return mixed|$this
360-
*/
361-
public function unless($value, $callback, $default = null)
362-
{
363-
if (! $value) {
364-
return $callback($this, $value) ?: $this;
365-
} elseif ($default) {
366-
return $default($this, $value) ?: $this;
367-
}
368-
369-
return $this;
370-
}
371336
}

src/Illuminate/Support/Stringable.php

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
namespace Illuminate\Support;
44

55
use Closure;
6+
use Illuminate\Support\Traits\Conditionable;
67
use Illuminate\Support\Traits\Macroable;
78
use Illuminate\Support\Traits\Tappable;
89
use JsonSerializable;
910
use Symfony\Component\VarDumper\VarDumper;
1011

1112
class Stringable implements JsonSerializable
1213
{
13-
use Macroable, Tappable;
14+
use Conditionable, Macroable, Tappable;
1415

1516
/**
1617
* The underlying string value.
@@ -708,38 +709,6 @@ public function ucfirst()
708709
return new static(Str::ucfirst($this->value));
709710
}
710711

711-
/**
712-
* Apply the callback's string changes if the given "value" is false.
713-
*
714-
* @param mixed $value
715-
* @param callable $callback
716-
* @param callable|null $default
717-
* @return mixed|$this
718-
*/
719-
public function unless($value, $callback, $default = null)
720-
{
721-
return $this->when(! $value, $callback, $default);
722-
}
723-
724-
/**
725-
* Apply the callback's string changes if the given "value" is true.
726-
*
727-
* @param mixed $value
728-
* @param callable $callback
729-
* @param callable|null $default
730-
* @return mixed|$this
731-
*/
732-
public function when($value, $callback, $default = null)
733-
{
734-
if ($value) {
735-
return $callback($this, $value) ?: $this;
736-
} elseif ($default) {
737-
return $default($this, $value) ?: $this;
738-
}
739-
740-
return $this;
741-
}
742-
743712
/**
744713
* Execute the given callback if the string is empty.
745714
*

src/Illuminate/Support/Traits/Conditionable.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
trait Conditionable
66
{
77
/**
8-
* Apply the callback if the given "value" is true.
8+
* Apply the callback if the given "value" is truthy.
99
*
1010
* @param mixed $value
1111
* @param callable $callback
@@ -25,7 +25,7 @@ public function when($value, $callback, $default = null)
2525
}
2626

2727
/**
28-
* Apply the callback if the given "value" is false.
28+
* Apply the callback if the given "value" is falsy.
2929
*
3030
* @param mixed $value
3131
* @param callable $callback
@@ -35,6 +35,12 @@ public function when($value, $callback, $default = null)
3535
*/
3636
public function unless($value, $callback, $default = null)
3737
{
38-
return $this->when(! $value, $callback, $default);
38+
if (! $value) {
39+
return $callback($this, $value) ?: $this;
40+
} elseif ($default) {
41+
return $default($this, $value) ?: $this;
42+
}
43+
44+
return $this;
3945
}
4046
}

tests/Support/SupportStringableTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,34 @@ public function testWhenTrue()
165165
}));
166166
}
167167

168+
public function testUnlessTruthy()
169+
{
170+
$this->assertSame('unless', (string) $this->stringable('unless')->unless(1, function ($stringable, $value) {
171+
return $stringable->append($value)->append('true');
172+
}));
173+
174+
$this->assertSame('unless true fallbacks to default with value 1',
175+
(string) $this->stringable('unless true ')->unless(1, function ($stringable, $value) {
176+
return $stringable->append($value);
177+
}, function ($stringable, $value) {
178+
return $stringable->append('fallbacks to default with value ')->append($value);
179+
}));
180+
}
181+
182+
public function testUnlessFalsy()
183+
{
184+
$this->assertSame('unless 0', (string) $this->stringable('unless ')->unless(0, function ($stringable, $value) {
185+
return $stringable->append($value);
186+
}));
187+
188+
$this->assertSame('gets the value 0',
189+
(string) $this->stringable('gets the value ')->unless(0, function ($stringable, $value) {
190+
return $stringable->append($value);
191+
}, function ($stringable) {
192+
return $stringable->append('fallbacks to default');
193+
}));
194+
}
195+
168196
public function testTrimmedOnlyWhereNecessary()
169197
{
170198
$this->assertSame(' Taylor Otwell ', (string) $this->stringable(' Taylor Otwell ')->words(3));

0 commit comments

Comments
 (0)