2014-04-18 14:20:28 +02:00
|
|
|
<?php
|
2020-03-10 11:13:10 +01:00
|
|
|
|
2014-04-18 14:20:28 +02:00
|
|
|
namespace DigiComp\SettingValidator\Validation\Validator;
|
|
|
|
|
2017-06-03 12:56:14 +02:00
|
|
|
/*
|
|
|
|
* This file is part of the DigiComp.SettingValidator package.
|
|
|
|
*
|
|
|
|
* (c) digital competence
|
|
|
|
*
|
|
|
|
* This package is Open Source Software. For the full copyright and license
|
|
|
|
* information, please view the LICENSE file which was distributed with this
|
|
|
|
* source code.
|
|
|
|
*/
|
|
|
|
|
2020-05-04 22:01:48 +02:00
|
|
|
use DigiComp\SettingValidator\Package;
|
2017-03-13 17:00:13 +01:00
|
|
|
use Neos\Flow\Annotations as Flow;
|
|
|
|
use Neos\Flow\Configuration\ConfigurationManager;
|
|
|
|
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
|
|
|
*/
|
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;
|
|
|
|
|
2016-08-01 01:00:29 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-03-13 17:00:13 +01:00
|
|
|
protected $supportedOptions = [
|
2017-06-21 11:26:13 +02:00
|
|
|
'name' => ['', 'Set the name of the setting-array to use', 'string', false],
|
2017-07-19 12:55:31 +02:00
|
|
|
'validationGroups' => [
|
|
|
|
['Default'],
|
|
|
|
'Same as "Validation Groups" of Flow Framework. Defines the groups to execute.',
|
|
|
|
'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;
|
2020-05-04 22:01:48 +02:00
|
|
|
$this->validations = $this->configurationManager->getConfiguration(Package::CONFIGURATION_TYPE_VALIDATION);
|
2016-08-01 01:00:29 +02:00
|
|
|
}
|
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
|
|
|
|
2017-06-03 14:11:13 +02:00
|
|
|
$config = $this->getConfigForName($name);
|
|
|
|
|
2016-08-01 01:00:29 +02:00
|
|
|
foreach ($config as $validatorConfig) {
|
2017-06-21 11:26:13 +02:00
|
|
|
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-06-03 14:11:13 +02:00
|
|
|
sprintf(
|
|
|
|
'Validator could not be resolved: "%s" Check your Validation.yaml',
|
|
|
|
$validatorConfig['validator']
|
|
|
|
),
|
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'])) {
|
2020-05-04 21:57:03 +02:00
|
|
|
$this->getResult()->forProperty($validatorConfig['property'])->merge(
|
2016-08-01 01:00:29 +02:00
|
|
|
$validator->validate(ObjectAccess::getPropertyPath($value, $validatorConfig['property']))
|
|
|
|
);
|
2017-06-02 21:18:27 +02:00
|
|
|
} else {
|
2020-05-04 21:57:03 +02:00
|
|
|
$this->getResult()->merge($validator->validate($value));
|
2016-08-01 01:00:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-21 11:26:13 +02:00
|
|
|
|
2017-06-03 14:11:13 +02:00
|
|
|
/**
|
2018-11-08 15:41:53 +01:00
|
|
|
* @param string $name
|
2017-06-03 14:11:13 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getConfigForName($name): array
|
|
|
|
{
|
2017-06-02 23:14:05 +02:00
|
|
|
$config = [];
|
|
|
|
if (isset($this->validations[$name]['self'])) {
|
|
|
|
foreach ($this->validations[$name]['self'] as $validator => &$validation) {
|
|
|
|
if (is_null($validation)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$newValidation['options'] = $validation;
|
|
|
|
$newValidation['validator'] = $validator;
|
|
|
|
$config[] = $newValidation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isset($this->validations[$name]['properties'])) {
|
|
|
|
foreach ($this->validations[$name]['properties'] as $propertyName => &$validation) {
|
|
|
|
foreach ($validation as $validator => &$options) {
|
|
|
|
if (is_null($options)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$newValidation['property'] = $propertyName;
|
|
|
|
$newValidation['validator'] = $validator;
|
|
|
|
$newValidation['options'] = $options;
|
|
|
|
$config[] = $newValidation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-03 14:11:13 +02:00
|
|
|
return $config;
|
2016-08-01 01:00:29 +02:00
|
|
|
}
|
2017-06-29 21:33:19 +02:00
|
|
|
|
2017-06-21 11:26:13 +02:00
|
|
|
/**
|
|
|
|
* Check whether at least one configured group does match, if any is configured.
|
|
|
|
*
|
|
|
|
* @param array $validatorConfig
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function doesValidationGroupsMatch(array &$validatorConfig)
|
|
|
|
{
|
2020-03-10 14:35:24 +01:00
|
|
|
if (isset($validatorConfig['options']['validationGroups']) && empty(array_intersect($validatorConfig['options']['validationGroups'], $this->options['validationGroups']))) {
|
2017-06-21 11:26:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add validation groups for recursion if necessary.
|
|
|
|
*
|
|
|
|
* @param array $validatorConfig
|
|
|
|
*/
|
|
|
|
protected function handleValidationGroups(array &$validatorConfig)
|
|
|
|
{
|
2020-05-04 22:02:12 +02:00
|
|
|
if ($validatorConfig['validator'] === 'DigiComp.SettingValidator:Settings') {
|
2017-07-19 12:54:29 +02:00
|
|
|
$validatorConfig['options']['validationGroups'] = $this->options['validationGroups'];
|
2020-05-04 22:02:12 +02:00
|
|
|
} elseif (isset($validatorConfig['options']['validationGroups'])) {
|
|
|
|
unset($validatorConfig['options']['validationGroups']);
|
2017-06-29 21:34:20 +02:00
|
|
|
}
|
|
|
|
}
|
2014-04-18 14:20:28 +02:00
|
|
|
}
|