improve color chooser and name validation (#2622)
This commit is contained in:
@@ -17,6 +17,10 @@ trait StringAccessibleConfigTrait
|
||||
* @var array
|
||||
*/
|
||||
protected $settings;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $original;
|
||||
/**
|
||||
* @var ConfigLoaderInterface
|
||||
*/
|
||||
@@ -29,7 +33,7 @@ trait StringAccessibleConfigTrait
|
||||
public function __construct(ConfigLoaderInterface $repository, array $settings)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
$this->settings = $settings;
|
||||
$this->original = $this->settings = $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,6 +84,17 @@ trait StringAccessibleConfigTrait
|
||||
*/
|
||||
abstract protected function getPrefix(): string;
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function default(string $key)
|
||||
{
|
||||
$key = $this->prepareSearchKey($key);
|
||||
|
||||
return $this->get($key, $this->original);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
|
||||
@@ -367,32 +367,13 @@ class SystemConfiguration implements SystemBundleConfiguration
|
||||
return (int) $this->find('theme.autocomplete_chars');
|
||||
}
|
||||
|
||||
public function getThemeColorChoices(): ?array
|
||||
public function getThemeColorChoices(): ?string
|
||||
{
|
||||
$config = $this->find('theme.color_choices');
|
||||
if (empty($config)) {
|
||||
return null;
|
||||
}
|
||||
$config = explode(',', $config);
|
||||
|
||||
$colors = [];
|
||||
foreach ($config as $item) {
|
||||
if (empty($item)) {
|
||||
continue;
|
||||
}
|
||||
$item = explode('|', $item);
|
||||
$key = $item[0];
|
||||
$value = $key;
|
||||
if (\count($item) > 1) {
|
||||
$value = $item[1];
|
||||
}
|
||||
|
||||
if (empty($key)) {
|
||||
$key = $value;
|
||||
}
|
||||
$colors[$key] = $value;
|
||||
if (!empty($config)) {
|
||||
return $config;
|
||||
}
|
||||
|
||||
return array_unique($colors);
|
||||
return $this->default('theme.color_choices');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -429,7 +429,7 @@ class Configuration implements ConfigurationInterface
|
||||
->end()
|
||||
->scalarNode('color_choices')
|
||||
->defaultValue(implode(',', [
|
||||
Constants::SOFTWARE . '|' . Constants::DEFAULT_COLOR, 'Silver|#c0c0c0', 'Gray|#808080', 'Black|#000000',
|
||||
'Silver|#c0c0c0', 'Gray|#808080', 'Black|#000000',
|
||||
'Maroon|#800000', 'Brown|#a52a2a', 'Red|#ff0000', 'Orange|#ffa500',
|
||||
'Gold|#ffd700', 'Yellow|#ffff00', 'Peach|#ffdab9', 'Khaki|#f0e68c',
|
||||
'Olive|#808000', 'Lime|#00ff00', 'Jelly|#9acd32', 'Green|#008000', 'Teal|#008080',
|
||||
|
||||
@@ -66,9 +66,12 @@ class ColorChoiceType extends AbstractType implements DataTransformerInterface
|
||||
|
||||
if ($this->isLimitedColors()) {
|
||||
$choices = [];
|
||||
foreach ($this->systemConfiguration->getThemeColorChoices() as $name => $color) {
|
||||
$colors = $this->convertStringToColorArray($this->systemConfiguration->getThemeColorChoices());
|
||||
|
||||
foreach ($colors as $name => $color) {
|
||||
$choices[$name] = $color;
|
||||
}
|
||||
|
||||
$options['choices'] = $choices;
|
||||
$options['search'] = false;
|
||||
$options['attr']['data-renderer'] = 'color';
|
||||
@@ -77,6 +80,37 @@ class ColorChoiceType extends AbstractType implements DataTransformerInterface
|
||||
$resolver->setDefaults($options);
|
||||
}
|
||||
|
||||
private function convertStringToColorArray(string $config): array
|
||||
{
|
||||
$config = explode(',', $config);
|
||||
|
||||
$colors = [];
|
||||
foreach ($config as $item) {
|
||||
if (empty($item)) {
|
||||
continue;
|
||||
}
|
||||
$item = explode('|', $item);
|
||||
$key = $item[0];
|
||||
$value = $key;
|
||||
|
||||
if (\count($item) > 1) {
|
||||
$value = $item[1];
|
||||
}
|
||||
|
||||
if (empty($key)) {
|
||||
$key = $value;
|
||||
}
|
||||
|
||||
if ($value === Constants::DEFAULT_COLOR) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$colors[$key] = $value;
|
||||
}
|
||||
|
||||
return array_unique($colors);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
||||
@@ -22,5 +22,6 @@ class ColorChoices extends Constraint
|
||||
];
|
||||
|
||||
public $message = 'The given value {{ value }} is not a valid hexadecimal color.';
|
||||
public $invalidNameMessage = 'The given value {{ name }} is not a valid color name for {{ color }}. Allowed are {{ max }} characters, given {{ count }}.';
|
||||
public $invalidNameMessage = 'The given value {{ name }} is not a valid color name for {{ color }}. Allowed are {{ max }} alpha-numerical characters, including minus and space.';
|
||||
public $maxLength = 20;
|
||||
}
|
||||
|
||||
@@ -57,12 +57,15 @@ class ColorChoicesValidator extends ConstraintValidator
|
||||
return;
|
||||
}
|
||||
|
||||
if (!\is_string($name) || 1 !== preg_match('/^[0-9a-zA-Z]{1,10}$/i', $name)) {
|
||||
$name = str_replace(['-', ' '], '', $name);
|
||||
$length = mb_strlen($name);
|
||||
|
||||
if (!\is_string($name) || $length > $constraint->maxLength || !ctype_alnum($name)) {
|
||||
$this->context->buildViolation($constraint->invalidNameMessage)
|
||||
->setParameter('{{ name }}', $this->formatValue($name))
|
||||
->setParameter('{{ color }}', $this->formatValue($code))
|
||||
->setParameter('{{ max }}', $this->formatValue(10))
|
||||
->setParameter('{{ count }}', $this->formatValue(\strlen($name)))
|
||||
->setParameter('{{ max }}', $this->formatValue($constraint->maxLength))
|
||||
->setParameter('{{ count }}', $this->formatValue($length))
|
||||
->setCode(ColorChoices::COLOR_CHOICES_NAME_ERROR)
|
||||
->addViolation();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user