class CollectionValidator in Plug 7
@author Bernhard Schussek <bschussek@gmail.com>
@api
Hierarchy
- class \Symfony\Component\Validator\ConstraintValidator implements ConstraintValidatorInterface
- class \Symfony\Component\Validator\Constraints\CollectionValidator
Expanded class hierarchy of CollectionValidator
1 file declares its use of CollectionValidator
- CollectionValidatorTest.php in lib/
Symfony/ validator/ Symfony/ Component/ Validator/ Tests/ Constraints/ CollectionValidatorTest.php
File
- lib/
Symfony/ validator/ Symfony/ Component/ Validator/ Constraints/ CollectionValidator.php, line 24
Namespace
Symfony\Component\Validator\ConstraintsView source
class CollectionValidator extends ConstraintValidator {
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint) {
if (!$constraint instanceof Collection) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\Collection');
}
if (null === $value) {
return;
}
if (!is_array($value) && !($value instanceof \Traversable && $value instanceof \ArrayAccess)) {
throw new UnexpectedTypeException($value, 'array or Traversable and ArrayAccess');
}
// We need to keep the initialized context when CollectionValidator
// calls itself recursively (Collection constraints can be nested).
// Since the context of the validator is overwritten when initialize()
// is called for the nested constraint, the outer validator is
// acting on the wrong context when the nested validation terminates.
//
// A better solution - which should be approached in Symfony 3.0 - is to
// remove the initialize() method and pass the context as last argument
// to validate() instead.
$context = $this->context;
foreach ($constraint->fields as $field => $fieldConstraint) {
// bug fix issue #2779
$existsInArray = is_array($value) && array_key_exists($field, $value);
$existsInArrayAccess = $value instanceof \ArrayAccess && $value
->offsetExists($field);
if ($existsInArray || $existsInArrayAccess) {
if (count($fieldConstraint->constraints) > 0) {
if ($context instanceof ExecutionContextInterface) {
$context
->getValidator()
->inContext($context)
->atPath('[' . $field . ']')
->validate($value[$field], $fieldConstraint->constraints);
}
else {
// 2.4 API
$context
->validateValue($value[$field], $fieldConstraint->constraints, '[' . $field . ']');
}
}
}
elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
$this
->buildViolationInContext($context, $constraint->missingFieldsMessage)
->atPath('[' . $field . ']')
->setParameter('{{ field }}', $this
->formatValue($field))
->setInvalidValue(null)
->setCode(Collection::MISSING_FIELD_ERROR)
->addViolation();
}
}
if (!$constraint->allowExtraFields) {
foreach ($value as $field => $fieldValue) {
if (!isset($constraint->fields[$field])) {
$this
->buildViolationInContext($context, $constraint->extraFieldsMessage)
->atPath('[' . $field . ']')
->setParameter('{{ field }}', $this
->formatValue($field))
->setInvalidValue($fieldValue)
->setCode(Collection::NO_SUCH_FIELD_ERROR)
->addViolation();
}
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CollectionValidator:: |
public | function |
Checks if the passed value is valid. Overrides ConstraintValidatorInterface:: |
|
ConstraintValidator:: |
protected | property | ||
ConstraintValidator:: |
protected | function | Wrapper for {@link ExecutionContextInterface::buildViolation} that supports the 2.4 context API. | |
ConstraintValidator:: |
protected | function | Wrapper for {@link ExecutionContextInterface::buildViolation} that supports the 2.4 context API. | |
ConstraintValidator:: |
protected | function | Returns a string representation of the type of the value. | |
ConstraintValidator:: |
protected | function | Returns a string representation of the value. | |
ConstraintValidator:: |
protected | function | Returns a string representation of a list of values. | |
ConstraintValidator:: |
public | function |
Initializes the constraint validator. Overrides ConstraintValidatorInterface:: |
1 |
ConstraintValidator:: |
constant | Whether to cast objects with a "__toString()" method to strings. | ||
ConstraintValidator:: |
constant | Whether to format {@link \DateTime} objects as RFC-3339 dates ("Y-m-d H:i:s"). |