Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Use "Conditionable" in existing classes that implement when() #37561

Merged
merged 4 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 3 additions & 38 deletions src/Illuminate/Database/Concerns/BuildsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down
22 changes: 2 additions & 20 deletions src/Illuminate/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,7 +21,7 @@

class Mailable implements MailableContract, Renderable
{
use ForwardsCalls, Localizable;
use Conditionable, ForwardsCalls, Localizable;

/**
* The locale of the message.
Expand Down Expand Up @@ -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.
*
Expand Down
41 changes: 3 additions & 38 deletions src/Illuminate/Notifications/Messages/MailMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
}
35 changes: 2 additions & 33 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
namespace Illuminate\Support;

use Closure;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\Traits\Tappable;
use JsonSerializable;
use Symfony\Component\VarDumper\VarDumper;

class Stringable implements JsonSerializable
{
use Macroable, Tappable;
use Conditionable, Macroable, Tappable;

/**
* The underlying string value.
Expand Down Expand Up @@ -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.
*
Expand Down
12 changes: 9 additions & 3 deletions src/Illuminate/Support/Traits/Conditionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
}
}
28 changes: 28 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down