153 lines
4.7 KiB
PHP
153 lines
4.7 KiB
PHP
|
<?php
|
||
|
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
namespace DigiComp\SettingValidator\Validation\Validator;
|
||
|
|
||
|
/*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
|
use Neos\Eel\EelEvaluatorInterface;
|
||
|
use Neos\Eel\Exception as NeosEelException;
|
||
|
use Neos\Eel\Utility;
|
||
|
use Neos\Flow\Annotations as Flow;
|
||
|
use Neos\Flow\ObjectManagement\DependencyInjection\DependencyProxy;
|
||
|
use Neos\Flow\Validation\Exception\InvalidValidationConfigurationException;
|
||
|
use Neos\Flow\Validation\Exception\NoSuchValidatorException;
|
||
|
use Neos\Flow\Validation\Validator\AbstractValidator;
|
||
|
use Neos\Flow\Validation\ValidatorResolver;
|
||
|
|
||
|
class ConditionalValidator extends AbstractValidator
|
||
|
{
|
||
|
/**
|
||
|
* @Flow\Inject
|
||
|
* @var EelEvaluatorInterface
|
||
|
*/
|
||
|
protected $eelEvaluator;
|
||
|
|
||
|
/**
|
||
|
* @Flow\Inject
|
||
|
* @var ValidatorResolver
|
||
|
*/
|
||
|
protected $validatorResolver;
|
||
|
|
||
|
/**
|
||
|
* @inheritDoc
|
||
|
*/
|
||
|
protected $supportedOptions = [
|
||
|
'conditions' => [[], 'List of entries with "condition" (eel expression) and "validators" (list of validators).', 'array', true],
|
||
|
'fallbackValidators' => [[], 'List of validators that is used if no condition matched.', 'array'],
|
||
|
'validationGroups' => [['Default'], 'Same as "Validation Groups" of Flow Framework', 'array'],
|
||
|
];
|
||
|
|
||
|
/**
|
||
|
* @inheritDoc
|
||
|
* @throws InvalidValidationConfigurationException
|
||
|
* @throws NeosEelException
|
||
|
* @throws NoSuchValidatorException
|
||
|
*/
|
||
|
protected function isValid($value): void
|
||
|
{
|
||
|
$validatorConfigs = [];
|
||
|
|
||
|
$hasMatch = false;
|
||
|
|
||
|
foreach ($this->options['conditions'] as $condition) {
|
||
|
if ($this->eelEvaluator instanceof DependencyProxy) {
|
||
|
$this->eelEvaluator->_activateDependency();
|
||
|
}
|
||
|
|
||
|
if (!Utility::evaluateEelExpression($condition['condition'], $this->eelEvaluator, ['this' => $value])) {
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
$hasMatch = true;
|
||
|
|
||
|
foreach ($condition['validators'] as $validator => $options) {
|
||
|
if ($options === null) {
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
$validatorConfigs[] = [
|
||
|
'validator' => $validator,
|
||
|
'options' => $options,
|
||
|
];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!$hasMatch) {
|
||
|
foreach ($this->options['fallbackValidators'] as $validator => $options) {
|
||
|
if ($options === null) {
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
$validatorConfigs[] = [
|
||
|
'validator' => $validator,
|
||
|
'options' => $options,
|
||
|
];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
foreach ($validatorConfigs as $validatorConfig) {
|
||
|
if (!$this->doesValidationGroupsMatch($validatorConfig)) {
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
$this->handleValidationGroups($validatorConfig);
|
||
|
|
||
|
$validator = $this->validatorResolver->createValidator(
|
||
|
$validatorConfig['validator'],
|
||
|
$validatorConfig['options']
|
||
|
);
|
||
|
|
||
|
if ($validator === null) {
|
||
|
throw new InvalidValidationConfigurationException(
|
||
|
\sprintf(
|
||
|
'Validator "%s" could not be resolved. Check your Validation.yaml',
|
||
|
$validatorConfig['validator']
|
||
|
),
|
||
|
1402326139
|
||
|
);
|
||
|
}
|
||
|
|
||
|
$this->getResult()->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): bool
|
||
|
{
|
||
|
return !isset($validatorConfig['options']['validationGroups'])
|
||
|
|| \array_intersect(
|
||
|
$validatorConfig['options']['validationGroups'],
|
||
|
$this->options['validationGroups']
|
||
|
) !== [];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Add validation groups for recursion if necessary.
|
||
|
*
|
||
|
* @param array $validatorConfig
|
||
|
*/
|
||
|
protected function handleValidationGroups(array &$validatorConfig): void
|
||
|
{
|
||
|
if ($validatorConfig['validator'] === 'DigiComp.SettingValidator:Settings') {
|
||
|
$validatorConfig['options']['validationGroups'] = $this->options['validationGroups'];
|
||
|
} elseif (isset($validatorConfig['options']['validationGroups'])) {
|
||
|
unset($validatorConfig['options']['validationGroups']);
|
||
|
}
|
||
|
}
|
||
|
}
|