Skip to content

Commit

Permalink
Added whenContains and whenContainsAll to Stringable. (#40285)
Browse files Browse the repository at this point in the history
  • Loading branch information
telkins authored Jan 7, 2022
1 parent 331c3f3 commit 310c270
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,52 @@ public function ucfirst()
return new static(Str::ucfirst($this->value));
}

/**
* Execute the given callback if the string contains a given substring.
*
* @param string|array $needles
* @param callable $callback
* @param callable|null $default
* @return static
*/
public function whenContains($needles, $callback, $default = null)
{
if ($this->contains($needles)) {
$result = $callback($this);

return $result ?? $this;
} elseif ($default) {
$result = $default($this);

return $result ?? $this;
}

return $this;
}

/**
* Execute the given callback if the string contains all array values.
*
* @param array $needles
* @param callable $callback
* @param callable|null $default
* @return static
*/
public function whenContainsAll(array $needles, $callback, $default = null)
{
if ($this->containsAll($needles)) {
$result = $callback($this);

return $result ?? $this;
} elseif ($default) {
$result = $default($this);

return $result ?? $this;
}

return $this;
}

/**
* Execute the given callback if the string is empty.
*
Expand Down
38 changes: 38 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,44 @@ public function testUnless()
}));
}

public function testWhenContains()
{
$this->assertSame('Tony Stark', (string) $this->stringable('stark')->whenContains('tar', function ($stringable) {
return $stringable->prepend('Tony ')->title();
}, function ($stringable) {
return $stringable->prepend('Arno ')->title();
}));

$this->assertSame('stark', (string) $this->stringable('stark')->whenContains('xxx', function ($stringable) {
return $stringable->prepend('Tony ')->title();
}));

$this->assertSame('Arno Stark', (string) $this->stringable('stark')->whenContains('xxx', function ($stringable) {
return $stringable->prepend('Tony ')->title();
}, function ($stringable) {
return $stringable->prepend('Arno ')->title();
}));
}

public function testWhenContainsAll()
{
$this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenContainsAll(['tony', 'stark'], function ($stringable) {
return $stringable->title();
}, function ($stringable) {
return $stringable->studly();
}));

$this->assertSame('tony stark', (string) $this->stringable('tony stark')->whenContainsAll(['xxx'], function ($stringable) {
return $stringable->title();
}));

$this->assertSame('TonyStark', (string) $this->stringable('tony stark')->whenContainsAll(['tony', 'xxx'], function ($stringable) {
return $stringable->title();
}, function ($stringable) {
return $stringable->studly();
}));
}

public function testWhenEmpty()
{
tap($this->stringable(), function ($stringable) {
Expand Down

0 comments on commit 310c270

Please sign in to comment.