Skip to content

Commit e7f8b3b

Browse files
[9.x] Added whenIsUlid to Stringable. (#45183)
* Added `whenIsUlid` to Stringable. * Add test
1 parent 37bbb8c commit e7f8b3b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/Illuminate/Support/Stringable.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,18 @@ public function whenIsUuid($callback, $default = null)
10171017
return $this->when($this->isUuid(), $callback, $default);
10181018
}
10191019

1020+
/**
1021+
* Execute the given callback if the string is a valid ULID.
1022+
*
1023+
* @param callable $callback
1024+
* @param callable|null $default
1025+
* @return static
1026+
*/
1027+
public function whenIsUlid($callback, $default = null)
1028+
{
1029+
return $this->when($this->isUlid(), $callback, $default);
1030+
}
1031+
10201032
/**
10211033
* Execute the given callback if the string starts with a given substring.
10221034
*

tests/Support/SupportStringableTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,25 @@ public function testWhenIsUuid()
321321
}));
322322
}
323323

324+
public function testWhenIsUlid()
325+
{
326+
$this->assertSame('Ulid: 01GJSNW9MAF792C0XYY8RX6QFT', (string) $this->stringable('01GJSNW9MAF792C0XYY8RX6QFT')->whenIsUlid(function ($stringable) {
327+
return $stringable->prepend('Ulid: ');
328+
}, function ($stringable) {
329+
return $stringable->prepend('Not Ulid: ');
330+
}));
331+
332+
$this->assertSame('2cdc7039-65a6-4ac7-8e5d-d554a98', (string) $this->stringable('2cdc7039-65a6-4ac7-8e5d-d554a98')->whenIsUlid(function ($stringable) {
333+
return $stringable->prepend('Ulid: ');
334+
}));
335+
336+
$this->assertSame('Not Ulid: ss-01GJSNW9MAF792C0XYY8RX6QFT', (string) $this->stringable('ss-01GJSNW9MAF792C0XYY8RX6QFT')->whenIsUlid(function ($stringable) {
337+
return $stringable->prepend('Ulid: ');
338+
}, function ($stringable) {
339+
return $stringable->prepend('Not Ulid: ');
340+
}));
341+
}
342+
324343
public function testWhenTest()
325344
{
326345
$this->assertSame('Winner: foo bar', (string) $this->stringable('foo bar')->whenTest('/bar/', function ($stringable) {

0 commit comments

Comments
 (0)