Allow hex RGB colors with 3 integers

This commit will also apply the contribution guidelines to the rule and
improve its  documentation.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2019-04-05 19:05:42 +02:00
parent 6040ddee42
commit 18ce48371c
No known key found for this signature in database
GPG key ID: 221E9281655813A6
4 changed files with 81 additions and 80 deletions

View file

@ -2,7 +2,7 @@
- `HexRgbColor()`
Validates a hex RGB color
Validates weather the input is a hex RGB color or not.
```php
v::hexRgbColor()->validate('#FFFAAA'); // true
@ -14,6 +14,7 @@ v::hexRgbColor()->validate('FCD'); // true
Version | Description
--------|-------------
2.0.0 | Allow hex RGB colors with 3 integers
0.7.0 | Created
***

View file

@ -13,35 +13,16 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use function is_string;
use function mb_strlen;
use function mb_strpos;
use function mb_substr;
/**
* Validates weather the input is a hex RGB color or not.
*
* @author Davide Pastore <pasdavide@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class HexRgbColor extends Xdigit
final class HexRgbColor extends AbstractEnvelope
{
/**
* {@inheritDoc}
*/
public function validate($input): bool
public function __construct()
{
if (!is_string($input)) {
return false;
}
if (mb_strpos($input, '#') === 0) {
$input = mb_substr($input, 1);
}
$length = mb_strlen($input);
if ($length != 3 && $length != 6) {
return false;
}
return parent::validate($input);
parent::__construct(new Regex('/^#?([0-9A-F]{3}|[0-9A-F]{6})$/'));
}
}

View file

@ -0,0 +1,42 @@
--CREDITS--
Henrique Moody <henriquemoody@gmail.com>
--FILE--
<?php
declare(strict_types=1);
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\HexRgbColorException;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::hexRgbColor()->check('invalid');
} catch (HexRgbColorException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::hexRgbColor())->check('#808080');
} catch (HexRgbColorException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::hexRgbColor()->assert('invalid');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::hexRgbColor())->assert('#808080');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECT--
"invalid" must be a hex RGB color
"#808080" must not be a hex RGB color
- "invalid" must be a hex RGB color
- "#808080" must not be a hex RGB color

View file

@ -13,83 +13,60 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Test\TestCase;
use Respect\Validation\Test\RuleTestCase;
use stdClass;
/**
* @group rule
* @covers \Respect\Validation\Exceptions\HexRgbColorException
* @group rule
*
* @covers \Respect\Validation\Rules\HexRgbColor
*
* @author Davide Pastore <pasdavide@gmail.com>
* @author Gabriel Caruso <carusogabriel34@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class HexRgbColorTest extends TestCase
final class HexRgbColorTest extends RuleTestCase
{
/**
* @dataProvider providerForValidHexRgbColor
*
* @test
* {@inheritDoc}
*/
public function hexRgbColorValuesOnlyShouldReturnTrue(string $input): void
public function providerForValidInput(): array
{
$validator = new HexRgbColor();
$sut = new HexRgbColor();
self::assertTrue($validator->validate($input));
}
/**
* @dataProvider providerForInvalidHexRgbColor
*
* @test
*
* @param mixed $input
*/
public function invalidHexRgbColorValuesShouldReturnFalse($input): void
{
$validator = new HexRgbColor();
self::assertFalse($validator->validate($input));
}
/**
* @return string[][]
*/
public function providerForValidHexRgbColor(): array
{
return [
['#000'],
['#00000F'],
['#123'],
['#123456'],
['#FFFFFF'],
['123123'],
['FFFFFF'],
[$sut, '#000'],
[$sut, '#00000F'],
[$sut, '#123'],
[$sut, '#123456'],
[$sut, '#FFFFFF'],
[$sut, '123123'],
[$sut, 'FFFFFF'],
[$sut, 443],
];
}
/**
* @return mixed[][]
* {@inheritDoc}
*/
public function providerForInvalidHexRgbColor(): array
public function providerForInvalidInput(): array
{
$sut = new HexRgbColor();
return [
['#0'],
['#0000G0'],
['#0FG'],
['#1234'],
['#AAAAAA1'],
['#S'],
['1234'],
['foo'],
[0x39F],
[05],
[1],
[443],
[[]],
[new stdClass()],
[null],
[$sut, '#0'],
[$sut, '#0000G0'],
[$sut, '#0FG'],
[$sut, '#1234'],
[$sut, '#AAAAAA1'],
[$sut, '#S'],
[$sut, '1234'],
[$sut, 'foo'],
[$sut, 05],
[$sut, 1],
[$sut, []],
[$sut, new stdClass()],
[$sut, null],
];
}
}