PortableInfobox/includes/services/Helpers/InfoboxParamsValidator.php

248 lines
4.9 KiB
PHP
Raw Normal View History

2015-06-11 09:47:22 +00:00
<?php
2022-03-11 20:35:51 +00:00
namespace PortableInfobox\Helpers;
2015-06-11 09:47:22 +00:00
2022-03-11 20:35:51 +00:00
use Exception;
2016-02-03 11:06:44 +00:00
class InfoboxParamsValidator {
private static $supportedParams = [
'accent-color-default',
'accent-color-source',
'accent-color-text-default',
2016-12-28 15:39:53 +00:00
'accent-color-text-source',
'layout',
'theme',
'theme-source',
2019-02-02 22:34:48 +00:00
'type',
'name'
2015-06-11 09:47:22 +00:00
];
private static $supportedLayouts = [
'default',
'stacked'
];
private static $colorNames = [
'aliceblue',
'antiquewhite',
'aqua',
'aquamarine',
'azure',
'beige',
'bisque',
'black',
'blanchedalmond',
'blue',
'blueviolet',
'brown',
'burlywood',
'cadetblue',
'chartreuse',
'chocolate',
'coral',
'cornflowerblue',
'cornsilk',
'crimson',
'cyan',
'darkblue',
'darkcyan',
'darkgoldenrod',
'darkgray',
'darkgrey',
'darkgreen',
'darkkhaki',
'darkmagenta',
'darkolivegreen',
'darkorange',
'darkorchid',
'darkred',
'darksalmon',
'darkseagreen',
'darkslateblue',
'darkslategray',
'darkslategrey',
'darkturquoise',
'darkviolet',
'deeppink',
'deepskyblue',
'dimgray',
'dimgrey',
'dodgerblue',
'firebrick',
'floralwhite',
'forestgreen',
'fuchsia',
'gainsboro',
'ghostwhite',
'gold',
'goldenrod',
'gray',
'grey',
'green',
'greenyellow',
'honeydew',
'hotpink',
'indianred',
'indigo',
'ivory',
'khaki',
'lavender',
'lavenderblush',
'lawngreen',
'lemonchiffon',
'lightblue',
'lightcoral',
'lightcyan',
'lightgoldenrodyellow',
'lightgray',
'lightgrey',
'lightgreen',
'lightpink',
'lightsalmon',
'lightseagreen',
'lightskyblue',
'lightslategray',
'lightslategrey',
'lightsteelblue',
'lightyellow',
'lime',
'limegreen',
'linen',
'magenta',
'maroon',
'mediumaquamarine',
'mediumblue',
'mediumorchid',
'mediumpurple',
'mediumseagreen',
'mediumslateblue',
'mediumspringgreen',
'mediumturquoise',
'mediumvioletred',
'midnightblue',
'mintcream',
'mistyrose',
'moccasin',
'navajowhite',
'navy',
'oldlace',
'olive',
'olivedrab',
'orange',
'orangered',
'orchid',
'palegoldenrod',
'palegreen',
'paleturquoise',
'palevioletred',
'papayawhip',
'peachpuff',
'peru',
'pink',
'plum',
'powderblue',
'purple',
'red',
'rosybrown',
'royalblue',
'saddlebrown',
'salmon',
'sandybrown',
'seagreen',
'seashell',
'sienna',
'silver',
'skyblue',
'slateblue',
'slategray',
'slategrey',
'snow',
'springgreen',
'steelblue',
'tan',
'teal',
'thistle',
'tomato',
'turquoise',
'violet',
'wheat',
'white',
'whitesmoke',
'yellow',
'yellowgreen'
];
private static $colorNamesFlipped;
2021-09-10 02:52:19 +00:00
private const REGEX_PERCENT = '(?:100|\d{1,2})%';
private const REGEX_HUE = '-?(?:3(?:60|[0-5]\d)|[12]?\d{1,2})';
private const REGEX_ALPHAVAL = '(?:' . self::REGEX_PERCENT . '|[01]?\.\d+|[01])';
private const REGEX_RGBVAL = '(?:' . self::REGEX_PERCENT . '|2(?:5[0-5]|[0-4]\d)|1?\d{1,2})';
private const REGEX_HEXRGB = '/^#?[a-f0-9]{3}(?:[a-f0-9]{3}(?:[a-f0-9]{2})?|[a-f0-9])?$/';
private const REGEX_RGB = '/^rgb\((?:' . self::REGEX_RGBVAL . ',){2}' . self::REGEX_RGBVAL . '\)$/';
private const REGEX_RGBA = '/^rgba\((?:' . self::REGEX_RGBVAL . ',){3}' . self::REGEX_ALPHAVAL . '\)$/';
private const REGEX_HSL = '/^hsl\(' . self::REGEX_HUE . ',' . self::REGEX_PERCENT . ',' .
2018-10-02 07:41:19 +00:00
self::REGEX_PERCENT . '\)$/';
2021-09-10 02:52:19 +00:00
private const REGEX_HSLA = '/^hsla\(' . self::REGEX_HUE . ',(?:' . self::REGEX_PERCENT . ',){2}' .
2018-10-02 07:41:19 +00:00
self::REGEX_ALPHAVAL . '\)$/';
public function __construct() {
2021-09-10 02:52:19 +00:00
if ( self::$colorNamesFlipped === null ) {
self::$colorNamesFlipped = array_flip( self::$colorNames );
}
}
2015-06-11 09:47:22 +00:00
/**
* validates infobox tags attribute names
* @param array $params
* @throws InvalidInfoboxParamsException
2018-08-16 09:25:53 +00:00
* @todo consider using hashmap instead of array ones validator grows
* @return bool
2015-06-11 09:47:22 +00:00
*/
public function validateParams( $params ) {
foreach ( array_keys( $params ) as $param ) {
if ( !in_array( $param, self::$supportedParams ) ) {
2015-06-11 09:47:22 +00:00
throw new InvalidInfoboxParamsException( $param );
}
}
2015-06-11 10:54:10 +00:00
return true;
2015-06-11 09:47:22 +00:00
}
/**
* validates if argument is valid color value.
2018-08-16 09:25:53 +00:00
* @param string $color
* @return bool
*/
public function validateColorValue( $color ) {
2022-04-15 15:56:38 +00:00
$color = strtolower( preg_replace( '/\s+/', '', $color ) );
if ( preg_match( self::REGEX_HEXRGB, $color ) ) {
return substr( $color, 0, 1 ) === '#' ? $color : '#' . $color;
}
if ( isset( self::$colorNamesFlipped[$color] ) ||
preg_match( self::REGEX_RGB, $color ) ||
preg_match( self::REGEX_RGBA, $color ) ||
preg_match( self::REGEX_HSL, $color ) ||
preg_match( self::REGEX_HSLA, $color )
) {
return $color;
}
return '';
}
/**
* checks if given layout name is supported
2018-08-16 09:25:53 +00:00
* @param string $layoutName
* @return bool
*/
public function validateLayout( $layoutName ) {
return $layoutName && in_array( $layoutName, self::$supportedLayouts );
}
2015-06-11 09:47:22 +00:00
}
2018-10-02 07:41:19 +00:00
// phpcs:ignore Generic.Files.OneObjectStructurePerFile.MultipleFound
2022-03-11 20:35:51 +00:00
class InvalidInfoboxParamsException extends Exception {
2015-06-11 09:47:22 +00:00
}