class ValidatorResolver (View source)

Validator resolver to automatically find a appropriate validator for a given subject

Constants

PATTERN_MATCH_VALIDATORS

Match validator names and options

PATTERN_MATCH_VALIDATOROPTIONS

Match validator options (to parse actual options)

Properties

protected ObjectManagerInterface $objectManager
protected ReflectionService $reflectionService
protected array $baseValidatorConjunctions

Methods

createValidator(string $validatorType, array $validatorOptions = [])

Get a validator for a given data type. Returns a validator implementing the ValidatorInterface or NULL if no validator could be resolved.

getBaseValidatorConjunction(string $targetClassName, array $validationGroups = ['Default'])

Resolves and returns the base validator conjunction for the given data type.

buildMethodArgumentsValidatorConjunctions(string $className, string $methodName, array $methodParameters = null, array $methodValidateAnnotations = null)

Detects and registers any validators for arguments:

  • by the data type specified in the param annotations
  • additional validators specified in the validate annotations of a method

void
reset()

Resets the baseValidatorConjunctions It is usually not required to reset the ValidatorResolver during one request. This method is mainly useful for functional tests

buildSubObjectValidator(array $objectPath, ValidatorInterface $propertyValidator)

Builds a chain of nested object validators by specification of the given object path.

void
buildBaseValidatorConjunction(string $indexKey, string $targetClassName, array $validationGroups)

Builds a base validator conjunction for the given data type.

void
addCustomValidators(string $targetClassName, ConjunctionValidator $conjunctionValidator)

This adds custom validators to the passed $conjunctionValidator.

static array
getPolyTypeObjectValidatorImplementationClassNames(ObjectManagerInterface $objectManager)

Returns a map of object validator class names.

string|false
resolveValidatorObjectName(string $validatorType)

Returns the class name of an appropriate validator for the given type. If no validator is available false is returned

static array
getValidatorImplementationClassNames(ObjectManagerInterface $objectManager)

Returns all class names implementing the ValidatorInterface.

string
getValidatorType(string $type)

Used to map PHP types to validator types.

Details

ValidatorInterface|null createValidator(string $validatorType, array $validatorOptions = [])

Get a validator for a given data type. Returns a validator implementing the ValidatorInterface or NULL if no validator could be resolved.

Parameters

string $validatorType

Either one of the built-in data types or fully qualified validator class name

array $validatorOptions

Options to be passed to the validator

Return Value

ValidatorInterface|null

Exceptions

NoSuchValidatorException
InvalidValidationConfigurationException

ConjunctionValidator getBaseValidatorConjunction(string $targetClassName, array $validationGroups = ['Default'])

Resolves and returns the base validator conjunction for the given data type.

If no validation is necessary, the returned validator is empty.

Parameters

string $targetClassName

Fully qualified class name of the target class, ie. the class which should be validated

array $validationGroups

The validation groups to build the validator for

Return Value

ConjunctionValidator

The validator conjunction

Exceptions

InvalidValidationConfigurationException
InvalidValidationOptionsException
NoSuchValidatorException

ConjunctionValidator[] buildMethodArgumentsValidatorConjunctions(string $className, string $methodName, array $methodParameters = null, array $methodValidateAnnotations = null)

Detects and registers any validators for arguments:

  • by the data type specified in the param annotations
  • additional validators specified in the validate annotations of a method

Parameters

string $className
string $methodName
array $methodParameters

Optional pre-compiled array of method parameters

array $methodValidateAnnotations

Optional pre-compiled array of validate annotations (as array)

Return Value

ConjunctionValidator[]

An Array of ValidatorConjunctions for each method parameters.

Exceptions

InvalidValidationConfigurationException
NoSuchValidatorException
InvalidTypeHintException
InvalidValidationOptionsException

void reset()

Resets the baseValidatorConjunctions It is usually not required to reset the ValidatorResolver during one request. This method is mainly useful for functional tests

Return Value

void

protected GenericObjectValidator buildSubObjectValidator(array $objectPath, ValidatorInterface $propertyValidator)

Builds a chain of nested object validators by specification of the given object path.

Parameters

array $objectPath

The object path

ValidatorInterface $propertyValidator

The validator which should be added to the property specified by objectPath

Return Value

GenericObjectValidator

Exceptions

InvalidValidationOptionsException

protected void buildBaseValidatorConjunction(string $indexKey, string $targetClassName, array $validationGroups)

Builds a base validator conjunction for the given data type.

The base validation rules are those which were declared directly in a class (typically a model) through some validate annotations on properties.

If a property holds a class for which a base validator exists, that property will be checked as well, regardless of a validate annotation

Additionally, if a custom validator was defined for the class in question, it will be added to the end of the conjunction. A custom validator is found if it follows the naming convention "Replace '\Model\' by '\Validator\' and append 'Validator'".

Example: $targetClassName is Neos\Foo\Domain\Model\Quux, then the validator will be found if it has the name Neos\Foo\Domain\Validator\QuuxValidator

Parameters

string $indexKey

The key to use as index in $this->baseValidatorConjunctions; calculated from target class name and validation groups

string $targetClassName

The data type to build the validation conjunction for. Needs to be the fully qualified class name.

array $validationGroups

The validation groups to build the validator for

Return Value

void

Exceptions

NoSuchValidatorException
InvalidArgumentException
InvalidValidationOptionsException
InvalidValidationConfigurationException

protected void addCustomValidators(string $targetClassName, ConjunctionValidator $conjunctionValidator)

This adds custom validators to the passed $conjunctionValidator.

A custom validator is found if it follows the naming convention "Replace '\Model\' by '\Validator\' and append 'Validator'". If found, it will be added to the $conjunctionValidator.

In addition canValidate() will be called on all implementations of the ObjectValidatorInterface to find all validators that could validate the target. The one with the highest priority will be added as well. If multiple validators have the same priority, which one will be added is not deterministic.

Parameters

string $targetClassName
ConjunctionValidator $conjunctionValidator

Return Value

void

Exceptions

InvalidValidationConfigurationException
NoSuchValidatorException

static array getPolyTypeObjectValidatorImplementationClassNames(ObjectManagerInterface $objectManager)

Returns a map of object validator class names.

Parameters

ObjectManagerInterface $objectManager

Return Value

array

Array of object validator class names

protected string|false resolveValidatorObjectName(string $validatorType)

Returns the class name of an appropriate validator for the given type. If no validator is available false is returned

Parameters

string $validatorType

Either the fully qualified class name of the validator or the short name of a built-in validator

Return Value

string|false

Class name of the validator or false if not available

static array getValidatorImplementationClassNames(ObjectManagerInterface $objectManager)

Returns all class names implementing the ValidatorInterface.

Parameters

ObjectManagerInterface $objectManager

Return Value

array

Array of class names implementing ValidatorInterface indexed by class name

protected string getValidatorType(string $type)

Used to map PHP types to validator types.

Parameters

string $type

Data type to unify

Return Value

string

unified data type