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

174 lines
5.4 KiB
PHP
Raw Normal View History

2014-04-18 14:20:28 +02:00
<?php
2014-04-18 14:20:28 +02:00
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 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
{
/**
* @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-05-04 23:05:55 +02:00
* @Flow\Inject
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 = [
2020-05-04 23:05:55 +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',
2020-05-04 22:23:54 +02:00
false,
2017-07-19 12:55:31 +02:00
],
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
*/
protected function isValid($value)
{
2020-05-04 23:05:55 +02:00
$validations = $this->configurationManager->getConfiguration(Package::CONFIGURATION_TYPE_VALIDATION);
$name = $this->options['name'] ?: TypeHandling::getTypeForValue($value);
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)) {
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-04 22:23:54 +02:00
if (!$validator) {
2016-08-01 01:00:29 +02:00
throw new InvalidValidationConfigurationException(
2020-05-04 23:07:52 +02:00
\sprintf(
2017-06-03 14:11:13 +02:00
'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'])) {
$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 {
$this->getResult()->merge($validator->validate($value));
2016-08-01 01:00:29 +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
{
$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) {
continue;
}
2020-05-04 23:05:55 +02:00
$config[] = [
'validator' => $validator,
'options' => $options,
];
}
}
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) {
continue;
}
2020-05-04 23:05:55 +02:00
$config[] = [
'property' => $property,
'validator' => $validator,
'options' => $options,
];
}
}
}
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
}
/**
* 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
{
2020-05-04 23:05:55 +02:00
return
!isset($validatorConfig['options']['validationGroups'])
|| !empty(
2020-05-04 23:07:52 +02:00
\array_intersect(
2020-05-04 22:23:54 +02:00
$validatorConfig['options']['validationGroups'],
$this->options['validationGroups']
)
)
2020-05-04 23:05:55 +02:00
;
}
/**
* Add validation groups for recursion if necessary.
*
* @param array $validatorConfig
*/
protected function handleValidationGroups(array &$validatorConfig)
{
if ($validatorConfig['validator'] === 'DigiComp.SettingValidator:Settings') {
$validatorConfig['options']['validationGroups'] = $this->options['validationGroups'];
} elseif (isset($validatorConfig['options']['validationGroups'])) {
unset($validatorConfig['options']['validationGroups']);
}
}
2014-04-18 14:20:28 +02:00
}