CountValidator.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 CountValidator extends ConstraintValidator
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function validate($value, Constraint $constraint)
  24. {
  25. if (!$constraint instanceof Count) {
  26. throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Count');
  27. }
  28. if (null === $value) {
  29. return;
  30. }
  31. if (!\is_array($value) && !$value instanceof \Countable) {
  32. throw new UnexpectedTypeException($value, 'array or \Countable');
  33. }
  34. $count = \count($value);
  35. if (null !== $constraint->max && $count > $constraint->max) {
  36. if ($this->context instanceof ExecutionContextInterface) {
  37. $this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
  38. ->setParameter('{{ count }}', $count)
  39. ->setParameter('{{ limit }}', $constraint->max)
  40. ->setInvalidValue($value)
  41. ->setPlural((int) $constraint->max)
  42. ->setCode(Count::TOO_MANY_ERROR)
  43. ->addViolation();
  44. } else {
  45. $this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
  46. ->setParameter('{{ count }}', $count)
  47. ->setParameter('{{ limit }}', $constraint->max)
  48. ->setInvalidValue($value)
  49. ->setPlural((int) $constraint->max)
  50. ->setCode(Count::TOO_MANY_ERROR)
  51. ->addViolation();
  52. }
  53. return;
  54. }
  55. if (null !== $constraint->min && $count < $constraint->min) {
  56. if ($this->context instanceof ExecutionContextInterface) {
  57. $this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)
  58. ->setParameter('{{ count }}', $count)
  59. ->setParameter('{{ limit }}', $constraint->min)
  60. ->setInvalidValue($value)
  61. ->setPlural((int) $constraint->min)
  62. ->setCode(Count::TOO_FEW_ERROR)
  63. ->addViolation();
  64. } else {
  65. $this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)
  66. ->setParameter('{{ count }}', $count)
  67. ->setParameter('{{ limit }}', $constraint->min)
  68. ->setInvalidValue($value)
  69. ->setPlural((int) $constraint->min)
  70. ->setCode(Count::TOO_FEW_ERROR)
  71. ->addViolation();
  72. }
  73. }
  74. }
  75. }