Drupal investigation

AllValidator.php 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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\Context\ExecutionContextInterface;
  14. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  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, __NAMESPACE__.'\All');
  27. }
  28. if (null === $value) {
  29. return;
  30. }
  31. if (!is_array($value) && !$value instanceof \Traversable) {
  32. throw new UnexpectedTypeException($value, 'array or Traversable');
  33. }
  34. $context = $this->context;
  35. if ($context instanceof ExecutionContextInterface) {
  36. $validator = $context->getValidator()->inContext($context);
  37. foreach ($value as $key => $element) {
  38. $validator->atPath('['.$key.']')->validate($element, $constraint->constraints);
  39. }
  40. } else {
  41. // 2.4 API
  42. foreach ($value as $key => $element) {
  43. $context->validateValue($element, $constraint->constraints, '['.$key.']');
  44. }
  45. }
  46. }
  47. }