optimize code
This commit is contained in:
parent
e4abe5a96c
commit
272f78f96e
1 changed files with 33 additions and 50 deletions
|
@ -28,12 +28,13 @@ use Neos\Utility\TypeHandling;
|
||||||
class SettingsValidator extends AbstractValidator
|
class SettingsValidator extends AbstractValidator
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var ValidatorResolver
|
|
||||||
* @Flow\Inject
|
* @Flow\Inject
|
||||||
|
* @var ValidatorResolver
|
||||||
*/
|
*/
|
||||||
protected $validatorResolver;
|
protected $validatorResolver;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @Flow\Inject
|
||||||
* @var ConfigurationManager
|
* @var ConfigurationManager
|
||||||
*/
|
*/
|
||||||
protected $configurationManager;
|
protected $configurationManager;
|
||||||
|
@ -42,7 +43,7 @@ class SettingsValidator extends AbstractValidator
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $supportedOptions = [
|
protected $supportedOptions = [
|
||||||
'name' => ['', 'Set the name of the setting-array to use', 'string', false],
|
'name' => ['', 'Set the name of the setting-array to use.', 'string', false],
|
||||||
'validationGroups' => [
|
'validationGroups' => [
|
||||||
['Default'],
|
['Default'],
|
||||||
'Same as "Validation Groups" of Flow Framework. Defines the groups to execute.',
|
'Same as "Validation Groups" of Flow Framework. Defines the groups to execute.',
|
||||||
|
@ -52,40 +53,22 @@ class SettingsValidator extends AbstractValidator
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @inheritDoc
|
||||||
*/
|
|
||||||
protected $validations;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param ConfigurationManager $configurationManager
|
|
||||||
*/
|
|
||||||
public function injectConfigurationManager(ConfigurationManager $configurationManager)
|
|
||||||
{
|
|
||||||
$this->configurationManager = $configurationManager;
|
|
||||||
$this->validations = $this->configurationManager->getConfiguration(Package::CONFIGURATION_TYPE_VALIDATION);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if $value is valid. If it is not valid, needs to add an error
|
|
||||||
* to Result.
|
|
||||||
*
|
|
||||||
* @param mixed $value
|
|
||||||
* @throws InvalidValidationOptionsException
|
* @throws InvalidValidationOptionsException
|
||||||
* @throws InvalidValidationConfigurationException
|
* @throws InvalidValidationConfigurationException
|
||||||
*/
|
*/
|
||||||
protected function isValid($value)
|
protected function isValid($value)
|
||||||
{
|
{
|
||||||
$name = $this->options['name'] ? $this->options['name'] : TypeHandling::getTypeForValue($value);
|
$validations = $this->configurationManager->getConfiguration(Package::CONFIGURATION_TYPE_VALIDATION);
|
||||||
if (!isset($this->validations[$name])) {
|
$name = $this->options['name'] ?: TypeHandling::getTypeForValue($value);
|
||||||
|
if (!isset($validations[$name])) {
|
||||||
throw new InvalidValidationOptionsException(
|
throw new InvalidValidationOptionsException(
|
||||||
'The name ' . $name . ' has not been defined in Validation.yaml!',
|
'The name "' . $name . '" has not been defined in Validation.yaml!',
|
||||||
1397821438
|
1397821438
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$config = $this->getConfigForName($name);
|
foreach ($this->getConfigForValidation($validations[$name]) as $validatorConfig) {
|
||||||
|
|
||||||
foreach ($config as $validatorConfig) {
|
|
||||||
if (!$this->doesValidationGroupsMatch($validatorConfig)) {
|
if (!$this->doesValidationGroupsMatch($validatorConfig)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -118,32 +101,36 @@ class SettingsValidator extends AbstractValidator
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param array $validation
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function getConfigForName($name): array
|
protected function getConfigForValidation(array $validation): array
|
||||||
{
|
{
|
||||||
$config = [];
|
$config = [];
|
||||||
if (isset($this->validations[$name]['self'])) {
|
|
||||||
foreach ($this->validations[$name]['self'] as $validator => &$validation) {
|
if (isset($validation['self'])) {
|
||||||
if (is_null($validation)) {
|
foreach ($validation['self'] as $validator => $options) {
|
||||||
|
if (is_null($options)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$newValidation['options'] = $validation;
|
$config[] = [
|
||||||
$newValidation['validator'] = $validator;
|
'validator' => $validator,
|
||||||
$config[] = $newValidation;
|
'options' => $options,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isset($this->validations[$name]['properties'])) {
|
|
||||||
foreach ($this->validations[$name]['properties'] as $propertyName => $validation) {
|
if (isset($validation['properties'])) {
|
||||||
foreach ($validation as $validator => $options) {
|
foreach ($validation['properties'] as $property => $propertyValidation) {
|
||||||
|
foreach ($propertyValidation as $validator => $options) {
|
||||||
if (is_null($options)) {
|
if (is_null($options)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$newValidation['property'] = $propertyName;
|
$config[] = [
|
||||||
$newValidation['validator'] = $validator;
|
'property' => $property,
|
||||||
$newValidation['options'] = $options;
|
'validator' => $validator,
|
||||||
$config[] = $newValidation;
|
'options' => $options,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -157,21 +144,17 @@ class SettingsValidator extends AbstractValidator
|
||||||
* @param array $validatorConfig
|
* @param array $validatorConfig
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function doesValidationGroupsMatch(array $validatorConfig)
|
protected function doesValidationGroupsMatch(array $validatorConfig): bool
|
||||||
{
|
{
|
||||||
if (
|
return
|
||||||
isset($validatorConfig['options']['validationGroups'])
|
!isset($validatorConfig['options']['validationGroups'])
|
||||||
&& empty(
|
|| !empty(
|
||||||
array_intersect(
|
array_intersect(
|
||||||
$validatorConfig['options']['validationGroups'],
|
$validatorConfig['options']['validationGroups'],
|
||||||
$this->options['validationGroups']
|
$this->options['validationGroups']
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
) {
|
;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue