DigiComp.SettingValidator/Classes/Validation/Validator/SettingsValidator.php

138 lines
4.2 KiB
PHP
Raw Normal View History

2014-04-18 14:20:28 +02:00
<?php
namespace DigiComp\SettingValidator\Validation\Validator;
2017-03-13 17:00:13 +01:00
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Configuration\ConfigurationManager;
use Neos\Flow\Reflection\ReflectionService;
use Neos\Flow\Validation\Exception\InvalidValidationConfigurationException;
use Neos\Flow\Validation\Exception\InvalidValidationOptionsException;
use Neos\Flow\Validation\Validator\AbstractValidator;
use Neos\Flow\Validation\ValidatorResolver;
use Neos\Utility\ObjectAccess;
use Neos\Utility\TypeHandling;
2014-04-18 14:20:28 +02:00
/**
2015-04-29 17:52:27 +02:00
* Validator resolving other Validators defined in Validation.yaml
*
2014-04-18 14:20:28 +02:00
* @Flow\Scope("prototype")
*/
2016-08-01 01:00:29 +02:00
class SettingsValidator extends AbstractValidator
{
/**
* @var ValidatorResolver
* @Flow\Inject
*/
protected $validatorResolver;
2014-04-18 14:20:28 +02:00
2017-03-13 17:00:13 +01:00
/**
* @var ConfigurationManager
*/
protected $configurationManager;
/**
* @var ReflectionService
* @Flow\Inject
*/
protected $reflectionService;
2016-08-01 01:00:29 +02:00
/**
* @var array
*/
2017-03-13 17:00:13 +01:00
protected $supportedOptions = [
'name' => ['', 'Set the name of the setting-array to use', 'string', false],
'validationGroups' => [['Default'], 'Same as "Validation Groups" of Flow Framework.', 'array', false],
2017-03-13 17:00:13 +01:00
];
2014-04-18 14:20:28 +02:00
2016-08-01 01:00:29 +02:00
/**
2017-03-13 17:00:13 +01:00
* @var array
2016-08-01 01:00:29 +02:00
*/
2017-03-13 17:00:13 +01:00
protected $validations;
2014-04-18 14:20:28 +02:00
2017-03-13 17:00:13 +01:00
/**
* @param ConfigurationManager $configurationManager
*/
2016-08-01 01:00:29 +02:00
public function injectConfigurationManager(ConfigurationManager $configurationManager)
{
$this->configurationManager = $configurationManager;
$this->validations = $this->configurationManager->getConfiguration('Validation');
}
2014-04-18 14:20:28 +02:00
2016-08-01 01:00:29 +02:00
/**
* Check if $value is valid. If it is not valid, needs to add an error
* to Result.
*
* @param mixed $value
*
* @throws InvalidValidationOptionsException
* @throws InvalidValidationConfigurationException
*/
protected function isValid($value)
{
2017-03-13 17:00:13 +01:00
$name = $this->options['name'] ? $this->options['name'] : TypeHandling::getTypeForValue($value);
if (! isset($this->validations[$name])) {
2016-08-01 01:00:29 +02:00
throw new InvalidValidationOptionsException(
'The name ' . $name . ' has not been defined in Validation.yaml!',
1397821438
);
}
2017-03-13 17:00:13 +01:00
2016-08-01 01:00:29 +02:00
$config = &$this->validations[$name];
foreach ($config as $validatorConfig) {
if (! $this->doesValidationGroupsMatch($validatorConfig)) {
continue;
}
$this->handleValidationGroups($validatorConfig);
2016-08-01 01:00:29 +02:00
$validator = $this->validatorResolver->createValidator(
$validatorConfig['validator'],
$validatorConfig['options']
);
2017-03-13 17:00:13 +01:00
if (! $validator) {
2016-08-01 01:00:29 +02:00
throw new InvalidValidationConfigurationException(
2017-03-13 17:00:13 +01:00
'Validator could not be resolved: ' . $validatorConfig['validator'] . '. Check your Validation.yaml',
2016-08-01 01:00:29 +02:00
1402326139
);
}
2017-03-13 17:00:13 +01:00
2016-08-01 01:00:29 +02:00
if (isset($validatorConfig['property'])) {
$this->result->forProperty($validatorConfig['property'])->merge(
$validator->validate(ObjectAccess::getPropertyPath($value, $validatorConfig['property']))
);
2017-06-02 21:18:27 +02:00
} else {
2016-08-01 01:00:29 +02:00
$this->result->merge($validator->validate($value));
}
}
}
/**
* Check whether at least one configured group does match, if any is configured.
*
* @param array $validatorConfig
* @return bool
*/
protected function doesValidationGroupsMatch(array &$validatorConfig)
{
if (isset($validatorConfig['validationGroups'])
&& count(array_intersect($validatorConfig['validationGroups'], $this->options['validationGroups'])) === 0
) {
return false;
}
return true;
}
/**
* Add validation groups for recursion if necessary.
*
* @param array $validatorConfig
*/
protected function handleValidationGroups(array &$validatorConfig)
{
if ($validatorConfig['validator'] === 'DigiComp.SettingValidator:Settings' && empty($validatorConfig['options']['validationGroups'])) {
$validatorConfig['options']['validationGroups'] = $this->options['validationGroups'];
}
}
2014-04-18 14:20:28 +02:00
}