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.
|
|
|
|
*/
|
|
|
|
|
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\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
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
2020-05-04 23:05:55 +02:00
|
|
|
foreach ($this->getConfigForValidation($validations[$name]) 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
|
|
|
|
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
|
|
|
/**
|
2020-05-04 23:05:55 +02:00
|
|
|
* @param array $validation
|
2017-06-03 14:11:13 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2020-05-04 23:05:55 +02:00
|
|
|
protected function getConfigForValidation(array $validation): array
|
2017-06-03 14:11:13 +02:00
|
|
|
{
|
2017-06-02 23:14:05 +02:00
|
|
|
$config = [];
|
2020-05-04 23:05:55 +02:00
|
|
|
|
|
|
|
if (isset($validation['self'])) {
|
|
|
|
foreach ($validation['self'] as $validator => $options) {
|
2020-05-04 23:07:52 +02:00
|
|
|
if ($options === null) {
|
2017-06-02 23:14:05 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-05-04 23:05:55 +02:00
|
|
|
$config[] = [
|
|
|
|
'validator' => $validator,
|
|
|
|
'options' => $options,
|
|
|
|
];
|
2017-06-02 23:14:05 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-04 23:05:55 +02:00
|
|
|
|
|
|
|
if (isset($validation['properties'])) {
|
|
|
|
foreach ($validation['properties'] as $property => $propertyValidation) {
|
|
|
|
foreach ($propertyValidation as $validator => $options) {
|
2020-05-04 23:07:52 +02:00
|
|
|
if ($options === null) {
|
2017-06-02 23:14:05 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-05-04 23:05:55 +02:00
|
|
|
$config[] = [
|
|
|
|
'property' => $property,
|
|
|
|
'validator' => $validator,
|
|
|
|
'options' => $options,
|
|
|
|
];
|
2017-06-02 23:14:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-04 22:23:54 +02:00
|
|
|
|
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
|
|
|
|
*/
|
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
|
|
|
}
|