Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
Added hex-color rule (#19)
Browse files Browse the repository at this point in the history
* created rule

* added test

* added 3 and 8 digit hex codes

* updated read me
  • Loading branch information
innoflash authored Apr 29, 2020
1 parent c982d27 commit cabfd81
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ The following validation rules are currently available:
| CitizenIdentification | validation.citizen_identification | Requires that the given value be a citizen identification number of USA, UK, France or Brazil (see class for details) |
| WithoutWhitespace | validation.without_whitespace | Requires that the given value not include any whitespace characters |
| MaxWords | validation.max_words | Requires that the given value cannot contain more words than specified |
| HexColor | validation.hex_color | Requires that the given value is a valid hex color eg. #fff, #0f0f0f, #00ff0080 |

## Contributing

Expand All @@ -99,4 +100,4 @@ If you'd like to support the development of Axiom, then please consider [sponsor

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
51 changes: 51 additions & 0 deletions src/Rules/HexColor.php
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.'
);
}
}
24 changes: 24 additions & 0 deletions tests/HexColorTest.php
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());
}
}

0 comments on commit cabfd81

Please sign in to comment.