Skip to content

Commit

Permalink
Add rules for HSL & HSV/HSB color notations
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed May 26, 2024
1 parent 0bbd5b2 commit 31b2e6d
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ The field under validation must be a valid [hexadecimal color code](https://en.w

Optional length as integer to check only for shorthand (3 or 4 characters) or full hexadecimal (6 or 8 characters) form.

### HSL Color

The field under validation must be a valid [HSL color code](https://en.wikipedia.org/wiki/HSL_and_HSV).

public Intervention\Validation\Rules\Hslcolor::__construct()

### HSV Color

The field under validation must be a valid [HSV/HSB color code](https://en.wikipedia.org/wiki/HSL_and_HSV).

public Intervention\Validation\Rules\Hsvcolor::__construct()

### Text without HTML

The field under validation must be free of any html code.
Expand Down
49 changes: 49 additions & 0 deletions src/Rules/Hslcolor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Intervention\Validation\Rules;

use Intervention\Validation\AbstractRegexRule;

class Hslcolor extends AbstractRegexRule
{
/**
* {@inheritdoc}
*
* @see AbstractRegexRule::pattern()
*/
protected function pattern(): string
{
return '/^hsl\((?P<h>[0-9\.]+), ?(?P<s>[0-9\.]+%?), ?(?P<l>[0-9\.]+%?)?\)$/i';
}

/**
* Determine if the validation rule passes.
*
* @param mixed $value
* @return bool
*/
public function isValid(mixed $value): bool
{
$result = preg_match($this->pattern(), strval($value), $matches);

if ($result !== 1) {
return false;
}

if (!in_array(intval($matches['h']), range(0, 360))) {
return false;
}

if (!in_array(intval($matches['s']), range(0, 100))) {
return false;
}

if (!in_array(intval($matches['l']), range(0, 100))) {
return false;
}

return true;
}
}
49 changes: 49 additions & 0 deletions src/Rules/Hsvcolor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Intervention\Validation\Rules;

use Intervention\Validation\AbstractRegexRule;

class Hsvcolor extends AbstractRegexRule
{
/**
* {@inheritdoc}
*
* @see AbstractRegexRule::pattern()
*/
protected function pattern(): string
{
return '/^hs(v|b)\((?P<h>[0-9\.]+), ?(?P<s>[0-9\.]+%?), ?(?P<v>[0-9\.]+%?)?\)$/i';
}

/**
* Determine if the validation rule passes.
*
* @param mixed $value
* @return bool
*/
public function isValid(mixed $value): bool
{
$result = preg_match($this->pattern(), strval($value), $matches);

if ($result !== 1) {
return false;
}

if (!in_array(intval($matches['h']), range(0, 360))) {
return false;
}

if (!in_array(intval($matches['s']), range(0, 100))) {
return false;
}

if (!in_array(intval($matches['v']), range(0, 100))) {
return false;
}

return true;
}
}
36 changes: 36 additions & 0 deletions tests/Rules/HslcolorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Intervention\Validation\Tests\Rules;

use PHPUnit\Framework\Attributes\DataProvider;
use Intervention\Validation\Rules\Hslcolor;
use PHPUnit\Framework\TestCase;

final class HslcolorTest extends TestCase
{
#[DataProvider('dataProvider')]
public function testValidation($result, $value): void
{
$valid = (new Hslcolor())->isValid($value);
$this->assertEquals($result, $valid);
}

public static function dataProvider(): array
{
return [
[true, 'hsl(20, 100, 50)'],
[true, 'hsl(20, 100%, 50%)'],
[true, 'hsl(0, 100%, 50%)'],
[true, 'hsl(0, 0%, 0%)'],
[true, 'hsl(0,0%,0%)'],
[true, 'hsl(0,0,0)'],
[true, 'HSL(0,0,0)'],
[true, 'hsl(360, 100%, 100%)'],
[false, 'hsl(361, 101%, 101%)'],
[false, 'hsl(-1, 0%, 0%)'],
[false, ''],
];
}
}
46 changes: 46 additions & 0 deletions tests/Rules/HsvcolorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Intervention\Validation\Tests\Rules;

use PHPUnit\Framework\Attributes\DataProvider;
use Intervention\Validation\Rules\Hsvcolor;
use PHPUnit\Framework\TestCase;

final class HsvcolorTest extends TestCase
{
#[DataProvider('dataProvider')]
public function testValidation($result, $value): void
{
$valid = (new Hsvcolor())->isValid($value);
$this->assertEquals($result, $valid);
}

public static function dataProvider(): array
{
return [
[true, 'hsv(20, 100, 50)'],
[true, 'hsv(20, 100%, 50%)'],
[true, 'hsv(0, 100%, 50%)'],
[true, 'hsv(0, 0%, 0%)'],
[true, 'hsv(0,0%,0%)'],
[true, 'hsv(0,0,0)'],
[true, 'HSV(0,0,0)'],
[true, 'hsv(360, 100%, 100%)'],
[true, 'hsb(20, 100, 50)'],
[true, 'hsb(20, 100%, 50%)'],
[true, 'hsb(0, 100%, 50%)'],
[true, 'hsb(0, 0%, 0%)'],
[true, 'hsb(0,0%,0%)'],
[true, 'hsb(0,0,0)'],
[true, 'HSB(0,0,0)'],
[true, 'hsb(360, 100%, 100%)'],
[false, 'hsv(361, 101%, 101%)'],
[false, 'hsv(-1, 0%, 0%)'],
[false, 'hsb(361, 101%, 101%)'],
[false, 'hsb(-1, 0%, 0%)'],
[false, ''],
];
}
}

0 comments on commit 31b2e6d

Please sign in to comment.