| 1 |
<?php |
| 2 |
/** |
| 3 |
* CssColorModuleValueTrait.php |
| 4 |
* |
| 5 |
* @created 04.05.2024 |
| 6 |
* @author smiley <[email protected]> |
| 7 |
* @copyright 2024 smiley |
| 8 |
* @license MIT |
| 9 |
* |
| 10 |
* @phan-file-suppress PhanTypeMismatchDeclaredParamNullable |
| 11 |
*/ |
| 12 |
declare(strict_types=1); |
| 13 |
|
| 14 |
namespace chillerlan\QRCode\Output; |
| 15 |
|
| 16 |
use function is_string, preg_match, strip_tags, trim; |
| 17 |
|
| 18 |
/** |
| 19 |
* Module value checks for output classes that use CSS colors |
| 20 |
*/ |
| 21 |
trait CssColorModuleValueTrait{ |
| 22 |
|
| 23 |
/** |
| 24 |
* note: we're not necessarily validating the several values, just checking the general syntax |
| 25 |
* note: css4 colors are not included |
| 26 |
* |
| 27 |
* implements \chillerlan\QRCode\Output\QROutputInterface::moduleValueIsValid() |
| 28 |
* |
| 29 |
* @todo: XSS proof |
| 30 |
* |
| 31 |
* @see https://developer.mozilla.org/en-US/docs/Web/CSS/color_value |
| 32 |
* |
| 33 |
* @param string $value |
| 34 |
*/ |
| 35 |
public static function moduleValueIsValid(mixed $value):bool{ |
| 36 |
|
| 37 |
if(!is_string($value)){ |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
$value = trim(strip_tags($value), " '\"\r\n\t"); |
| 42 |
|
| 43 |
// hex notation |
| 44 |
// #rgb(a) |
| 45 |
// #rrggbb(aa) |
| 46 |
if(preg_match('/^#([\da-f]{3}){1,2}$|^#([\da-f]{4}){1,2}$/i', $value)){ |
| 47 |
return true; |
| 48 |
} |
| 49 |
|
| 50 |
// css: hsla/rgba(...values) |
| 51 |
if(preg_match('#^(hsla?|rgba?)\([\d .,%/]+\)$#i', $value)){ |
| 52 |
return true; |
| 53 |
} |
| 54 |
|
| 55 |
// predefined css color |
| 56 |
if(preg_match('/^[a-z]+$/i', $value)){ |
| 57 |
return true; |
| 58 |
} |
| 59 |
|
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* implements \chillerlan\QRCode\Output\QROutputAbstract::prepareModuleValue() |
| 65 |
* |
| 66 |
* @param string $value |
| 67 |
*/ |
| 68 |
protected function prepareModuleValue(mixed $value):string{ |
| 69 |
return trim(strip_tags($value), " '\"\r\n\t"); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* implements \chillerlan\QRCode\Output\QROutputAbstract::getDefaultModuleValue() |
| 74 |
*/ |
| 75 |
protected function getDefaultModuleValue(bool $isDark):string{ |
| 76 |
return ($isDark) ? '#000' : '#fff'; |
| 77 |
} |
| 78 |
|
| 79 |
} |