diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index e33f9b0b78a7..647e70a9dcfa 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -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. * diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index 43207a37f393..f547161f14d6 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -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) {