Minor refactoring

This commit is contained in:
Meritoo
2019-08-25 20:22:42 +02:00
parent 030e33064d
commit cc06cdde6f

View File

@@ -776,10 +776,7 @@ class Regex
*/ */
public static function getValidColorHexValue($color, $throwException = true) public static function getValidColorHexValue($color, $throwException = true)
{ {
/* // Not a scalar value?
* Not a scalar value?
* Nothing to do
*/
if (!is_scalar($color)) { if (!is_scalar($color)) {
return false; return false;
} }
@@ -787,10 +784,7 @@ class Regex
$color = Miscellaneous::replace($color, '/#/', ''); $color = Miscellaneous::replace($color, '/#/', '');
$length = strlen($color); $length = strlen($color);
/* // Color hasn't 3 or 6 characters length?
* Color is not 3 or 6 characters long?
* Nothing to do
*/
if (3 !== $length && 6 !== $length) { if (3 !== $length && 6 !== $length) {
if ($throwException) { if ($throwException) {
throw new IncorrectColorHexLengthException($color); throw new IncorrectColorHexLengthException($color);
@@ -799,10 +793,7 @@ class Regex
return false; return false;
} }
/* // Make the color 6 characters length, if has 3
* Color is 3 characters long?
* Let's make it 6 characters long
*/
if (3 === $length) { if (3 === $length) {
$color = Miscellaneous::replace($color, '/(.)(.)(.)/', '$1$1$2$2$3$3'); $color = Miscellaneous::replace($color, '/(.)(.)(.)/', '$1$1$2$2$3$3');
} }
@@ -810,19 +801,16 @@ class Regex
$pattern = self::$patterns['color']; $pattern = self::$patterns['color'];
$match = (bool)preg_match($pattern, $color); $match = (bool)preg_match($pattern, $color);
/* // It's not a valid color
* It's valid color if (!$match) {
* Nothing to do more if ($throwException) {
*/ throw new InvalidColorHexValueException($color);
if ($match) { }
return strtolower($color);
return false;
} }
if ($throwException) { return strtolower($color);
throw new InvalidColorHexValueException($color);
}
return false;
} }
/** /**