2014-04-18 14:20:28 +02:00
|
|
|
<?php
|
2020-03-10 11:13:10 +01:00
|
|
|
|
2022-05-02 09:56:09 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2017-03-13 17:00:13 +01:00
|
|
|
use Neos\Flow\Annotations as Flow;
|
|
|
|
use Neos\Flow\Validation\Exception\InvalidValidationConfigurationException;
|
|
|
|
use Neos\Flow\Validation\Exception\InvalidValidationOptionsException;
|
2020-05-14 14:43:48 +02:00
|
|
|
use Neos\Flow\Validation\Exception\NoSuchValidatorException;
|
2017-03-13 17:00:13 +01:00
|
|
|
use Neos\Flow\Validation\Validator\AbstractValidator;
|
|
|
|
use Neos\Flow\Validation\ValidatorResolver;
|
|
|
|
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
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @Flow\Inject
|
2020-05-04 23:05:55 +02:00
|
|
|
* @var ValidatorResolver
|
2016-08-01 01:00:29 +02:00
|
|
|
*/
|
|
|
|
protected $validatorResolver;
|
2014-04-18 14:20:28 +02:00
|
|
|
|
2017-03-13 17:00:13 +01:00
|
|
|
/**
|
2020-07-22 17:22:46 +02:00
|
|
|
* @Flow\InjectConfiguration(type="Validation")
|
|
|
|
* @var array
|
2017-03-13 17:00:13 +01:00
|
|
|
*/
|
2020-07-22 17:22:46 +02:00
|
|
|
protected array $validations;
|
2017-03-13 17:00:13 +01:00
|
|
|
|
2016-08-01 01:00:29 +02:00
|
|
|
/**
|
2020-07-22 17:22:46 +02:00
|
|
|
* @inheritDoc
|
2016-08-01 01:00:29 +02:00
|
|
|
*/
|
2020-07-22 17:22:46 +02:00
|
|
|
protected $supportedOptions = [
|
|
|
|
'name' => ['', 'Name of the setting array to use', 'string'],
|
|
|
|
'validationGroups' => [['Default'], 'Same as "Validation Groups" of Flow Framework', 'array'],
|
2017-03-13 17:00:13 +01:00
|
|
|
];
|
2014-04-18 14:20:28 +02:00
|
|
|
|
2016-08-01 01:00:29 +02:00
|
|
|
/**
|
2020-05-04 23:05:55 +02:00
|
|
|
* @inheritDoc
|
2016-08-01 01:00:29 +02:00
|
|
|
* @throws InvalidValidationOptionsException
|
|
|
|
* @throws InvalidValidationConfigurationException
|
2020-05-14 14:43:48 +02:00
|
|
|
* @throws NoSuchValidatorException
|
2016-08-01 01:00:29 +02:00
|
|
|
*/
|
2020-07-22 17:22:46 +02:00
|
|
|
protected function isValid($value): void
|
2016-08-01 01:00:29 +02:00
|
|
|
{
|
2020-07-22 17:22:46 +02:00
|
|
|
$validations = $this->validations;
|
|
|
|
|
|
|
|
$name = $this->options['name'] !== '' ? $this->options['name'] : TypeHandling::getTypeForValue($value);
|
2020-05-04 23:05:55 +02:00
|
|
|
if (!isset($validations[$name])) {
|
2016-08-01 01:00:29 +02:00
|
|
|
throw new InvalidValidationOptionsException(
|
2020-05-04 23:05:55 +02:00
|
|
|
'The name "' . $name . '" has not been defined in Validation.yaml!',
|
2016-08-01 01:00:29 +02:00
|
|
|
1397821438
|
|
|
|
);
|
|
|
|
}
|
2017-03-13 17:00:13 +01:00
|
|
|
|
2024-08-30 14:46:49 +02:00
|
|
|
// @deprecated - converts old "self" to new structure
|
|
|
|
if (isset($validations[$name]['self'])) {
|
|
|
|
foreach ($validations[$name]['self'] as $validator => $options) {
|
|
|
|
if (isset($validations[$name][$validator])) {
|
|
|
|
throw new \RuntimeException('The validator "' . $validator . '" is already defined on parent level.', 1725000364);
|
|
|
|
}
|
|
|
|
$validations[$name][$validator] = $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($validations[$name]['self']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// @deprecated - converts old "properties" to new structure
|
|
|
|
if (isset($validations[$name]['properties'])) {
|
|
|
|
if (isset($validations[$name]['DigiComp.SettingValidator:Properties'])) {
|
|
|
|
throw new \RuntimeException('The validator "DigiComp.SettingValidator:Properties" is already defined on parent level.', 1725000396);
|
|
|
|
}
|
|
|
|
$validations[$name]['DigiComp.SettingValidator:Properties'] = [
|
|
|
|
'validatorsForProperties' => $validations[$name]['properties'],
|
|
|
|
];
|
|
|
|
|
|
|
|
unset($validations[$name]['properties']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$validatorConfigs = [];
|
|
|
|
|
|
|
|
foreach ($validations[$name] as $validator => $options) {
|
|
|
|
if ($options === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$validatorConfigs[] = [
|
|
|
|
'validator' => $validator,
|
|
|
|
'options' => $options,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($validatorConfigs as $validatorConfig) {
|
2020-05-04 22:23:54 +02:00
|
|
|
if (!$this->doesValidationGroupsMatch($validatorConfig)) {
|
2017-06-21 11:26:13 +02:00
|
|
|
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
|
|
|
|
2020-05-13 09:47:41 +02:00
|
|
|
if ($validator === null) {
|
2016-08-01 01:00:29 +02:00
|
|
|
throw new InvalidValidationConfigurationException(
|
2020-05-04 23:07:52 +02:00
|
|
|
\sprintf(
|
2020-05-13 09:48:43 +02:00
|
|
|
'Validator "%s" could not be resolved. Check your Validation.yaml',
|
2017-06-03 14:11:13 +02:00
|
|
|
$validatorConfig['validator']
|
|
|
|
),
|
2016-08-01 01:00:29 +02:00
|
|
|
1402326139
|
|
|
|
);
|
|
|
|
}
|
2017-03-13 17:00:13 +01:00
|
|
|
|
2024-08-30 14:46:49 +02:00
|
|
|
$this->getResult()->merge($validator->validate($value));
|
2016-08-01 01:00:29 +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
|
|
|
|
*/
|
2020-05-04 23:05:55 +02:00
|
|
|
protected function doesValidationGroupsMatch(array $validatorConfig): bool
|
2017-06-21 11:26:13 +02:00
|
|
|
{
|
2020-08-19 13:56:55 +02:00
|
|
|
return !isset($validatorConfig['options']['validationGroups'])
|
2020-07-22 17:22:46 +02:00
|
|
|
|| \array_intersect(
|
|
|
|
$validatorConfig['options']['validationGroups'],
|
|
|
|
$this->options['validationGroups']
|
2020-08-19 13:56:55 +02:00
|
|
|
) !== [];
|
2017-06-21 11:26:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add validation groups for recursion if necessary.
|
|
|
|
*
|
|
|
|
* @param array $validatorConfig
|
|
|
|
*/
|
2020-07-22 17:22:46 +02:00
|
|
|
protected function handleValidationGroups(array &$validatorConfig): void
|
2017-06-21 11:26:13 +02:00
|
|
|
{
|
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
|
|
|
}
|