PortableInfobox/services/Helpers/InfoboxParamsValidator.php

66 lines
1.4 KiB
PHP
Raw Normal View History

2015-06-11 09:47:22 +00:00
<?php
namespace Wikia\PortableInfobox\Helpers;
2016-02-03 11:06:44 +00:00
class InfoboxParamsValidator {
2015-06-11 09:47:22 +00:00
private $supportedParams = [
'theme',
'theme-source',
'layout',
'accent-color-default',
'accent-color-source',
'accent-color-text-default',
'accent-color-text-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 ) {
if ( preg_match('/^(#?[a-f0-9]{3}([a-f0-9]{3})?)$/i', $color) ) {
return true;
}
throw new InvalidColorValueException();
}
/**
* 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 {
}
class InvalidColorValueException extends \Exception {
}