Skip to content

Commit e059372

Browse files
committed
add isHexColor() method
1 parent 5abf54c commit e059372

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/Strings.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,16 @@ public function format(...$args): self
17081708
return $this;
17091709
}
17101710

1711+
/**
1712+
* Returns true if the string is hex color, false otherwise.
1713+
*
1714+
* @return bool Returns TRUE on success or FALSE otherwise.
1715+
*/
1716+
public function isHexColor(): bool
1717+
{
1718+
return (bool) mb_ereg_match('^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$', $this->string);
1719+
}
1720+
17111721
/**
17121722
* Returns true if the string is affirmative, false otherwise.
17131723
*

tests/StringsTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,14 @@
752752
$this->assertFalse(Strings::create('90/11/2022')->isDate());
753753
});
754754

755+
test('test isAffirmative() method', function (): void {
756+
$this->assertTrue(Strings::create('true')->isAffirmative());
757+
$this->assertTrue(Strings::create('yes')->isAffirmative());
758+
$this->assertTrue(Strings::create('t')->isAffirmative());
759+
$this->assertTrue(Strings::create('y')->isAffirmative());
760+
$this->assertTrue(Strings::create('ok')->isAffirmative());
761+
$this->assertTrue(Strings::create('okay')->isAffirmative());
762+
});
755763

756764
test('test isLower() method', function (): void {
757765
$this->assertTrue(Strings::create('fòôbàřs')->isLower());
@@ -768,6 +776,13 @@
768776
$this->assertTrue(Strings::create('19FDE')->isHexadecimal());
769777
});
770778

779+
test('test isHexColor() method', function (): void {
780+
$this->assertTrue(Strings::create('#333')->isHexColor());
781+
$this->assertFalse(Strings::create('#3333')->isHexColor());
782+
$this->assertTrue(Strings::create('fff')->isHexColor());
783+
$this->assertFalse(Strings::create('fffff')->isHexColor());
784+
});
785+
771786
test('test isPrintable() method', function (): void {
772787
$this->assertTrue(Strings::create('fòôbàřs')->isPrintable());
773788
$this->assertTrue(Strings::create('19FDE')->isPrintable());

0 commit comments

Comments
 (0)