Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colour Validation for RGB and RGBa #18

Merged
merged 2 commits into from
Mar 19, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added rgb(a) colour validation
  • Loading branch information
darrynten committed Mar 19, 2017
commit 54df0ed917091476ee1b7250a9de5933b8239bcd
32 changes: 30 additions & 2 deletions src/Validators/ColourValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,37 @@ class ColourValidator
/**
* Valid short hex (#FFF)
*
* @var array $validShortHex
* @var string $validShortHex
*/
private static $validShortHex = '/^\#[0-9A-Fa-f]{3}$/';

/**
* Valid long hex (#FFFFFF)
*
* @var array $validLongHex
* @var string $validLongHex
*/
private static $validLongHex = '/^\#[0-9A-Fa-f]{6}$/';

/**
* Valid rgba
*
* rgba(1,1,1,1)
* rgba(0,10,0,0)
* rgba(0, 0 ,0, 0.0)
* rgba( 255,255,255,1)
* rgba(255, 111 ,0 , 0.5 )
*
* @var string $validRgba
*/
private static $validRgba = '/^rgba\(\s?[0-2]{0,1}?[0-9]{0,2}?\s?,\s?[0-2]{0,1}?[0-9]{0,2}?\s?,\s?[0-2]{0,1}[0-9]{0,2}?\s?(,\s?[0-1]{1}\.*?[0-9]?\s?)?\)$/';

/**
* Valid rgb
*
* @var string $validRgb
*/
private static $validRgb = '/^rgb\(\s?[0-2]{0,1}?[0-9]{0,2}?\s?,\s?[0-2]{0,1}?[0-9]{0,2}?\s?,\s?[0-2]{0,1}[0-9]{0,2}?\s?\)$/';

/**
* Valid word
*
Expand Down Expand Up @@ -56,6 +76,14 @@ public static function isValidColour(string $colour = null)
return true;
}

if (preg_match(self::$validRgb, $colour)) {
return true;
}

if (preg_match(self::$validRgba, $colour)) {
return true;
}

if (preg_match(self::$validWord, $colour)) {
return true;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Pslayers/Validators/ColourValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public function testValidation()
$this->assertTrue(ColourValidator::isValidColour('#000000'));
$this->assertTrue(ColourValidator::isValidColour('#FFF'));
$this->assertTrue(ColourValidator::isValidColour('#b4d455'));
$this->assertTrue(ColourValidator::isValidColour('rgb(255,128,1)'));
$this->assertTrue(ColourValidator::isValidColour('rgba(255,128,0, 1)'));
$this->assertTrue(ColourValidator::isValidColour(null));

$this->assertTrue(ColourValidator::isValidOpacity(1));
Expand Down