PortableInfobox/includes/services/Helpers/InfoboxParamsValidator.php

59 lines
1.3 KiB
PHP
Raw Normal View History

2015-06-11 09:47:22 +00:00
<?php
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 = [
'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
];
private $supportedLayouts = [
'default',
'stacked'
];
2015-06-11 09:47:22 +00:00
/**
* validates infobox tags attribute names
* @param array $params
* @throws InvalidInfoboxParamsException
* @todo: consider using hashmap instead of array ones validator grows
2015-06-11 10:54:10 +00:00
* @returns boolean
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
}
/**
* validates if argument is valid color value. Currently only hex values are supported
* @param $color
* @return bool
* @throws InvalidColorValueException
*/
public function validateColorValue( $color ) {
return !empty( preg_match('/^(#[a-f0-9]{3}([a-f0-9]{3})?)$/i', $color) );
}
/**
* checks if given layout name is supported
* @param $layoutName
* @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 {
}