You are here

public function AllValidator::validate in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/Constraints/AllValidator.php \Symfony\Component\Validator\Constraints\AllValidator::validate()

Checks if the passed value is valid.

Parameters

mixed $value The value that should be validated:

Constraint $constraint The constraint for the validation:

Overrides ConstraintValidatorInterface::validate

File

vendor/symfony/validator/Constraints/AllValidator.php, line 27

Class

AllValidator
@author Bernhard Schussek <bschussek@gmail.com>

Namespace

Symfony\Component\Validator\Constraints

Code

public function validate($value, Constraint $constraint) {
  if (!$constraint instanceof All) {
    throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\All');
  }
  if (null === $value) {
    return;
  }
  if (!is_array($value) && !$value instanceof \Traversable) {
    throw new UnexpectedTypeException($value, 'array or Traversable');
  }
  $context = $this->context;
  if ($context instanceof ExecutionContextInterface) {
    $validator = $context
      ->getValidator()
      ->inContext($context);
    foreach ($value as $key => $element) {
      $validator
        ->atPath('[' . $key . ']')
        ->validate($element, $constraint->constraints);
    }
  }
  else {

    // 2.4 API
    foreach ($value as $key => $element) {
      $context
        ->validateValue($element, $constraint->constraints, '[' . $key . ']');
    }
  }
}