Skip to content

Commit 5a1669b

Browse files
committed
feat(strings): add new methods whenStartsWith and whenIsUuid
1 parent 2f77db7 commit 5a1669b

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

src/Strings.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,37 @@ public function whenIsAscii($callback, $default = null)
18011801
return $this->when($this->isAscii(), $callback, $default);
18021802
}
18031803

1804+
/**
1805+
* Execute the given callback if the string is a valid UUID.
1806+
*
1807+
* @param callable $callback Callback function.
1808+
* @param callable $default Callback function.
1809+
*
1810+
* @return self Returns instance of the Strings class.
1811+
*
1812+
* @access public
1813+
*/
1814+
public function whenIsUuid($callback, $default = null)
1815+
{
1816+
return $this->when($this->isUuid(), $callback, $default);
1817+
}
1818+
1819+
/**
1820+
* Execute the given callback if the string starts with a given substring.
1821+
*
1822+
* @param string|string[] $needles The string to find in haystack.
1823+
* @param callable $callback Callback function.
1824+
* @param callable $default Callback function.
1825+
*
1826+
* @return self Returns instance of the Strings class.
1827+
*
1828+
* @access public
1829+
*/
1830+
public function whenStartsWith($needles, $callback, $default = null)
1831+
{
1832+
return $this->when($this->startsWith($needles), $callback, $default);
1833+
}
1834+
18041835
/**
18051836
* Apply the callback if the given "value" is (or resolves to) falsy.
18061837
*

tests/StringsTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,22 @@
478478
$this->assertEquals('# EL', $strings->toString());
479479
});
480480

481+
test('test whenIsUuid() method', function (): void {
482+
$strings = new Strings('f47ac10b-58cc-4372-a567-0e02b2c3d479');
483+
$strings->whenIsUuid(function ($strings) {
484+
return $strings->prepend('uuid: ');
485+
});
486+
$this->assertEquals('uuid: f47ac10b-58cc-4372-a567-0e02b2c3d479', $strings->toString());
487+
});
488+
489+
test('test whenStartsWith() method', function (): void {
490+
$strings = new Strings('maxine mayfield');
491+
$strings->whenStartsWith('maxine', function ($strings) {
492+
return $strings->headline();
493+
});
494+
$this->assertEquals('Maxine Mayfield', $strings->toString());
495+
});
496+
481497
test('test unless() method', function (): void {
482498
$strings = new Strings('Fòô');
483499
$strings->unless(false, function ($strings) {

0 commit comments

Comments
 (0)