Skip to content

Commit

Permalink
[9.x] Added whenIsUlid to Stringable. (#45183)
Browse files Browse the repository at this point in the history
* Added `whenIsUlid` to Stringable.

* Add test
  • Loading branch information
michaelnabil230 authored Dec 5, 2022
1 parent 37bbb8c commit e7f8b3b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,18 @@ public function whenIsUuid($callback, $default = null)
return $this->when($this->isUuid(), $callback, $default);
}

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

/**
* Execute the given callback if the string starts with a given substring.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,25 @@ public function testWhenIsUuid()
}));
}

public function testWhenIsUlid()
{
$this->assertSame('Ulid: 01GJSNW9MAF792C0XYY8RX6QFT', (string) $this->stringable('01GJSNW9MAF792C0XYY8RX6QFT')->whenIsUlid(function ($stringable) {
return $stringable->prepend('Ulid: ');
}, function ($stringable) {
return $stringable->prepend('Not Ulid: ');
}));

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

$this->assertSame('Not Ulid: ss-01GJSNW9MAF792C0XYY8RX6QFT', (string) $this->stringable('ss-01GJSNW9MAF792C0XYY8RX6QFT')->whenIsUlid(function ($stringable) {
return $stringable->prepend('Ulid: ');
}, function ($stringable) {
return $stringable->prepend('Not Ulid: ');
}));
}

public function testWhenTest()
{
$this->assertSame('Winner: foo bar', (string) $this->stringable('foo bar')->whenTest('/bar/', function ($stringable) {
Expand Down

0 comments on commit e7f8b3b

Please sign in to comment.