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

Add a fluent Stringable Str::of($string)->if() method to allow longer chaining of a Stringable, based on the Stringable itself #40038

Closed
wants to merge 4 commits into from
Closed
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
17 changes: 17 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,23 @@ public function finish($cap)
return new static(Str::finish($this->value, $cap));
}

/**
* Conditionally apply a callback to the stringable, based on the stringable value itself.
*
* @param callable $condition
* @param callable|null $true
* @param callable|null $false
* @return static
*/
public function if($condition, $true, $false = null)
{
if ($condition($this)) {
return new static($true ? $true($this) : $this);
} else {
return new static($false ? $false($this) : $this);
}
}

/**
* Determine if a given string matches a given pattern.
*
Expand Down
53 changes: 53 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,59 @@ public function testFinish()
$this->assertSame('abcbbc', (string) $this->stringable('abcbbcbc')->finish('bc'));
}

public function testIf()
{
$this->assertSame('200 is numeric',
(string) $this->stringable(' 200')
->if(function ($stringable) {
return is_numeric((string) $stringable);
}, function ($stringable) {
return $stringable->append(' is numeric');
}, function ($stringable) {
return $stringable->append(' is not numeric');
})->trim());

$this->assertSame('Laravel is not numeric',
(string) $this->stringable(' Laravel')
->if(function ($stringable) {
return is_numeric((string) $stringable);
}, function ($stringable) {
return $stringable->append(' is numeric');
}, function ($stringable) {
return $stringable->append(' is not numeric');
})->trim());

$this->assertSame(
'It keeps a string the same if $true = null',
(string) $this->stringable('It keeps a string the same if $true = null ')
->if(function () {
return true;
}, null, function ($stringable) {
return $stringable->replace('keeps', 'doesn\'t keep');
})->trim()
);

$this->assertSame(
'It keeps a string the same if $false = null',
(string) $this->stringable('It keeps a string the same if $false = null ')
->if(function () {
return false;
}, function ($stringable) {
return $stringable->replace('keeps', 'doesn\'t keep');
}, null)->trim()
);

$this->assertSame(
'It keeps a string the same if $false = null and third parameter omitted',
(string) $this->stringable('It keeps a string the same if $false = null and third parameter omitted ')
->if(function () {
return false;
}, function ($stringable) {
return $stringable->replace('keeps', 'doesn\'t keep');
})->trim()
);
}

public function testIs()
{
$this->assertTrue($this->stringable('/')->is('/'));
Expand Down