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] Added whenEndsWith(), whenExactly(), whenStartsWith(), etc to Stringable #40320

Merged
merged 5 commits into from
Jan 11, 2022
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
107 changes: 88 additions & 19 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -775,17 +775,7 @@ public function ucfirst()
*/
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;
return $this->when($this->contains($needles), $callback, $default);
}

/**
Expand All @@ -798,17 +788,96 @@ public function whenContains($needles, $callback, $default = null)
*/
public function whenContainsAll(array $needles, $callback, $default = null)
{
if ($this->containsAll($needles)) {
$result = $callback($this);
return $this->when($this->containsAll($needles), $callback, $default);
}

return $result ?? $this;
} elseif ($default) {
$result = $default($this);
/**
* Execute the given callback if the string ends with a given substring.
*
* @param string|array $needles
* @param callable $callback
* @param callable|null $default
* @return static
*/
public function whenEndsWith($needles, $callback, $default = null)
{
return $this->when($this->endsWith($needles), $callback, $default);
}

return $result ?? $this;
}
/**
* Execute the given callback if the string is an exact match with the given value.
*
* @param string $value
* @param callable $callback
* @param callable|null $default
* @return static
*/
public function whenExactly($value, $callback, $default = null)
{
return $this->when($this->exactly($value), $callback, $default);
}

return $this;
/**
* Execute the given callback if the string matches a given pattern.
*
* @param string|array $pattern
* @param callable $callback
* @param callable|null $default
* @return static
*/
public function whenIs($pattern, $callback, $default = null)
{
return $this->when($this->is($pattern), $callback, $default);
}

/**
* Execute the given callback if the string is 7 bit ASCII.
*
* @param callable $callback
* @param callable|null $default
* @return static
*/
public function whenIsAscii($callback, $default = null)
{
return $this->when($this->isAscii(), $callback, $default);
}

/**
* Execute the given callback if the string is a valid UUID.
*
* @param callable $callback
* @param callable|null $default
* @return static
*/
public function whenIsUuid($callback, $default = null)
{
return $this->when($this->isUuid(), $callback, $default);
}

/**
* Execute the given callback if the string matches the given pattern.
*
* @param string $pattern
* @param callable $callback
* @param callable|null $default
* @return static
*/
public function whenTest($pattern, $callback, $default = null)
{
return $this->when($this->test($pattern), $callback, $default);
}

/**
* Execute the given callback if the string starts with a given substring.
*
* @param string|array $needles
* @param callable $callback
* @param callable|null $default
* @return static
*/
public function whenStartsWith($needles, $callback, $default = null)
{
return $this->when($this->startsWith($needles), $callback, $default);
}

/**
Expand Down
149 changes: 149 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,155 @@ public function testWhenContainsAll()
}));
}

public function testWhenEndsWith()
{
$this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenEndsWith('ark', function ($stringable) {
return $stringable->title();
}, function ($stringable) {
return $stringable->studly();
}));

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

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

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

public function testWhenExactly()
{
$this->assertSame('Nailed it...!', (string) $this->stringable('Tony Stark')->whenExactly('Tony Stark', function ($stringable) {
return 'Nailed it...!';
}, function ($stringable) {
return 'Swing and a miss...!';
}));

$this->assertSame('Swing and a miss...!', (string) $this->stringable('Tony Stark')->whenExactly('Iron Man', function ($stringable) {
return 'Nailed it...!';
}, function ($stringable) {
return 'Swing and a miss...!';
}));

$this->assertSame('Tony Stark', (string) $this->stringable('Tony Stark')->whenExactly('Iron Man', function ($stringable) {
return 'Nailed it...!';
}));
}

public function testWhenIs()
{
$this->assertSame('Winner: /', (string) $this->stringable('/')->whenIs('/', function ($stringable) {
return $stringable->prepend('Winner: ');
}, function ($stringable) {
return 'Try again';
}));

$this->assertSame('/', (string) $this->stringable('/')->whenIs(' /', function ($stringable) {
return $stringable->prepend('Winner: ');
}));

$this->assertSame('Try again', (string) $this->stringable('/')->whenIs(' /', function ($stringable) {
return $stringable->prepend('Winner: ');
}, function ($stringable) {
return 'Try again';
}));

$this->assertSame('Winner: foo/bar/baz', (string) $this->stringable('foo/bar/baz')->whenIs('foo/*', function ($stringable) {
return $stringable->prepend('Winner: ');
}));
}

public function testWhenIsAscii()
{
$this->assertSame('Ascii: A', (string) $this->stringable('A')->whenIsAscii(function ($stringable) {
return $stringable->prepend('Ascii: ');
}, function ($stringable) {
return $stringable->prepend('Not Ascii: ');
}));

$this->assertSame('ù', (string) $this->stringable('ù')->whenIsAscii(function ($stringable) {
return $stringable->prepend('Ascii: ');
}));

$this->assertSame('Not Ascii: ù', (string) $this->stringable('ù')->whenIsAscii(function ($stringable) {
return $stringable->prepend('Ascii: ');
}, function ($stringable) {
return $stringable->prepend('Not Ascii: ');
}));
}

public function testWhenIsUuid()
{
$this->assertSame('Uuid: 2cdc7039-65a6-4ac7-8e5d-d554a98e7b15', (string) $this->stringable('2cdc7039-65a6-4ac7-8e5d-d554a98e7b15')->whenIsUuid(function ($stringable) {
return $stringable->prepend('Uuid: ');
}, function ($stringable) {
return $stringable->prepend('Not Uuid: ');
}));

$this->assertSame('2cdc7039-65a6-4ac7-8e5d-d554a98', (string) $this->stringable('2cdc7039-65a6-4ac7-8e5d-d554a98')->whenIsUuid(function ($stringable) {
return $stringable->prepend('Uuid: ');
}));

$this->assertSame('Not Uuid: 2cdc7039-65a6-4ac7-8e5d-d554a98', (string) $this->stringable('2cdc7039-65a6-4ac7-8e5d-d554a98')->whenIsUuid(function ($stringable) {
return $stringable->prepend('Uuid: ');
}, function ($stringable) {
return $stringable->prepend('Not Uuid: ');
}));
}

public function testWhenTest()
{
$this->assertSame('Winner: foo bar', (string) $this->stringable('foo bar')->whenTest('/bar/', function ($stringable) {
return $stringable->prepend('Winner: ');
}, function ($stringable) {
return 'Try again';
}));

$this->assertSame('Try again', (string) $this->stringable('foo bar')->whenTest('/link/', function ($stringable) {
return $stringable->prepend('Winner: ');
}, function ($stringable) {
return 'Try again';
}));

$this->assertSame('foo bar', (string) $this->stringable('foo bar')->whenTest('/link/', function ($stringable) {
return $stringable->prepend('Winner: ');
}));
}

public function testWhenStartsWith()
{
$this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenStartsWith('ton', function ($stringable) {
return $stringable->title();
}, function ($stringable) {
return $stringable->studly();
}));

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

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

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

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