Add class for validating color codes

This commit is contained in:
Joshua Ramon Enslin 2021-01-28 21:47:59 +01:00
parent 9d4d326d6a
commit 89e06769f1
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE

View File

@ -85,6 +85,27 @@ final class MD_STD_IN {
}
/**
* String sanitization for 3 o4 6 characters RGB color codes (sans the leading #).
*
* @param mixed $input Input string.
*
* @return string
*/
public static function sanitize_rgb_color($input):string {
$output = \filter_var($input,
FILTER_SANITIZE_STRING,
FILTER_FLAG_NO_ENCODE_QUOTES);
if (!(preg_match('/^[a-zA-Z0-9]{3}$/', $output)) && !(preg_match('/^[a-zA-Z0-9]{6}$/', $output))) {
throw new MDInvalidColorCode("Invalid color code provided: " . $output);
}
return $output;
}
/**
* Retrieves HTTP input texts from GET or POST variables, whatever is provided.
* If neither is given, returns a provided default.