SequentiallyValidator.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. /**
  15. * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  16. */
  17. class SequentiallyValidator extends ConstraintValidator
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function validate($value, Constraint $constraint)
  23. {
  24. if (!$constraint instanceof Sequentially) {
  25. throw new UnexpectedTypeException($constraint, Sequentially::class);
  26. }
  27. $context = $this->context;
  28. $validator = $context->getValidator()->inContext($context);
  29. $originalCount = $validator->getViolations()->count();
  30. foreach ($constraint->constraints as $c) {
  31. if ($originalCount !== $validator->validate($value, $c)->getViolations()->count()) {
  32. break;
  33. }
  34. }
  35. }
  36. }