This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* created rule * added test * added 3 and 8 digit hex codes * updated read me
- Loading branch information
Showing
3 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Axiom\Rules; | ||
|
||
use Axiom\Types\Rule; | ||
|
||
class HexColor extends Rule | ||
{ | ||
/** | ||
* Hex color regex formats. | ||
* | ||
* @var array | ||
*/ | ||
private array $formats = [ | ||
4 => '/^#([a-fA-F0-9]{3})$/i', | ||
7 => '/^#([a-fA-F0-9]{6})$/i', | ||
9 => '/^#([a-fA-F0-9]{6}[0-9]{2})$/i', | ||
]; | ||
|
||
/** | ||
* Determines if the rule passes. | ||
* | ||
* @param string $attribute | ||
* @param mixed $value | ||
* | ||
* @return bool | ||
*/ | ||
public function passes($attribute, $value) | ||
{ | ||
if (! is_string($value) || ! in_array(strlen($value), array_keys($this->formats))) { | ||
return false; | ||
} | ||
|
||
$match = preg_match($this->formats[strlen($value)], $value); | ||
|
||
return $match > 0; | ||
} | ||
|
||
/** | ||
* Message returned on validation failure. | ||
* | ||
* @return array|string | ||
*/ | ||
public function message() | ||
{ | ||
return $this->getLocalizedErrorMessage( | ||
'hex_color', | ||
'The :attribute has to be a valid hex color.' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Axiom\Rules\Tests; | ||
|
||
use Axiom\Rules\HexColor; | ||
use Orchestra\Testbench\TestCase; | ||
|
||
class HexColorTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_can_validate_hex_color() | ||
{ | ||
$rule = ['hex_color' => [new HexColor()]]; | ||
|
||
$this->assertTrue(validator(['hex_color' => '#f0f0f0'], $rule)->passes()); | ||
$this->assertTrue(validator(['hex_color' => '#fff'], $rule)->passes()); | ||
$this->assertTrue(validator(['hex_color' => '#00ff0080'], $rule)->passes()); | ||
$this->assertFalse(validator(['hex_color' => '#00ff00ff'], $rule)->passes()); | ||
$this->assertFalse(validator(['hex_color' => 'f0f0f0'], $rule)->passes()); | ||
$this->assertFalse(validator(['hex_color' => 'rgb(0,0,255)'], $rule)->passes()); | ||
$this->assertTrue(validator(['hex_color' => '#000000'], $rule)->passes()); | ||
$this->assertFalse(validator(['hex_color' => '#0f0f'], $rule)->passes()); | ||
} | ||
} |