-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rules for HSL & HSV/HSB color notations
- Loading branch information
1 parent
0bbd5b2
commit 31b2e6d
Showing
5 changed files
with
192 additions
and
0 deletions.
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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, ''], | ||
]; | ||
} | ||
} |
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,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, ''], | ||
]; | ||
} | ||
} |