Skip to content

Commit

Permalink
add isHexColor() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Jan 1, 2022
1 parent 5abf54c commit e059372
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,16 @@ public function format(...$args): self
return $this;
}

/**
* Returns true if the string is hex color, false otherwise.
*
* @return bool Returns TRUE on success or FALSE otherwise.
*/
public function isHexColor(): bool
{
return (bool) mb_ereg_match('^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$', $this->string);
}

/**
* Returns true if the string is affirmative, false otherwise.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/StringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,14 @@
$this->assertFalse(Strings::create('90/11/2022')->isDate());
});

test('test isAffirmative() method', function (): void {
$this->assertTrue(Strings::create('true')->isAffirmative());
$this->assertTrue(Strings::create('yes')->isAffirmative());
$this->assertTrue(Strings::create('t')->isAffirmative());
$this->assertTrue(Strings::create('y')->isAffirmative());
$this->assertTrue(Strings::create('ok')->isAffirmative());
$this->assertTrue(Strings::create('okay')->isAffirmative());
});

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

test('test isHexColor() method', function (): void {
$this->assertTrue(Strings::create('#333')->isHexColor());
$this->assertFalse(Strings::create('#3333')->isHexColor());
$this->assertTrue(Strings::create('fff')->isHexColor());
$this->assertFalse(Strings::create('fffff')->isHexColor());
});

test('test isPrintable() method', function (): void {
$this->assertTrue(Strings::create('fòôbàřs')->isPrintable());
$this->assertTrue(Strings::create('19FDE')->isPrintable());
Expand Down

0 comments on commit e059372

Please sign in to comment.