Skip to content

Commit

Permalink
[8.x] Add unless method on stringable (#37326)
Browse files Browse the repository at this point in the history
* Add unless method to stringable

* Update src/Illuminate/Support/Stringable.php

Co-authored-by: Dinh Quoc Han <[email protected]>

Co-authored-by: Dinh Quoc Han <[email protected]>
  • Loading branch information
ph7jack and dinhquochan authored May 10, 2021
1 parent 32e4a6a commit 235db73
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,19 @@ 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.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 235db73

Please sign in to comment.