2015-06-11 09:47:22 +00:00
|
|
|
<?php
|
2018-08-09 09:49:10 +00:00
|
|
|
namespace PortableInfobox\Helpers;
|
2015-06-11 09:47:22 +00:00
|
|
|
|
2016-02-03 11:06:44 +00:00
|
|
|
class InfoboxParamsValidator {
|
2015-06-11 09:47:22 +00:00
|
|
|
private $supportedParams = [
|
2016-12-27 12:50:53 +00:00
|
|
|
'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',
|
2015-06-11 09:47:22 +00:00
|
|
|
];
|
|
|
|
|
2016-12-28 08:27:25 +00:00
|
|
|
private $supportedLayouts = [
|
|
|
|
'default',
|
|
|
|
'stacked'
|
|
|
|
];
|
|
|
|
|
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 ) {
|
2015-06-11 10:54:10 +00:00
|
|
|
if ( !in_array( $param, $this->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
|
|
|
}
|
2016-12-28 08:27:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* validates if argument is valid color value. Currently only hex values are supported
|
2018-08-16 09:25:53 +00:00
|
|
|
* @param string $color
|
2016-12-28 08:27:25 +00:00
|
|
|
* @return bool
|
|
|
|
* @throws InvalidColorValueException
|
|
|
|
*/
|
|
|
|
public function validateColorValue( $color ) {
|
2018-08-16 09:25:53 +00:00
|
|
|
return !empty( preg_match( '/^(#[a-f0-9]{3}([a-f0-9]{3})?)$/i', $color ) );
|
2016-12-28 08:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* checks if given layout name is supported
|
2018-08-16 09:25:53 +00:00
|
|
|
* @param string $layoutName
|
2016-12-28 08:27:25 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function validateLayout( $layoutName ) {
|
|
|
|
return $layoutName && in_array( $layoutName, $this->supportedLayouts );
|
|
|
|
}
|
2015-06-11 09:47:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class InvalidInfoboxParamsException extends \Exception {
|
|
|
|
}
|