AllValidator.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\ConstraintValidator;
  13. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  14. use Symfony\Component\Validator\Exception\UnexpectedValueException;
  15. /**
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. */
  18. class AllValidator extends ConstraintValidator
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function validate($value, Constraint $constraint)
  24. {
  25. if (!$constraint instanceof All) {
  26. throw new UnexpectedTypeException($constraint, All::class);
  27. }
  28. if (null === $value) {
  29. return;
  30. }
  31. if (!\is_array($value) && !$value instanceof \Traversable) {
  32. throw new UnexpectedValueException($value, 'iterable');
  33. }
  34. $context = $this->context;
  35. $validator = $context->getValidator()->inContext($context);
  36. foreach ($value as $key => $element) {
  37. $validator->atPath('['.$key.']')->validate($element, $constraint->constraints);
  38. }
  39. }
  40. }