Drupal investigation

CountValidator.php 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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\Context\ExecutionContextInterface;
  12. use Symfony\Component\Validator\Constraint;
  13. use Symfony\Component\Validator\ConstraintValidator;
  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 (null === $value) {
  26. return;
  27. }
  28. if (!is_array($value) && !$value instanceof \Countable) {
  29. throw new UnexpectedTypeException($value, 'array or \Countable');
  30. }
  31. $count = count($value);
  32. if (null !== $constraint->max && $count > $constraint->max) {
  33. if ($this->context instanceof ExecutionContextInterface) {
  34. $this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
  35. ->setParameter('{{ count }}', $count)
  36. ->setParameter('{{ limit }}', $constraint->max)
  37. ->setInvalidValue($value)
  38. ->setPlural((int) $constraint->max)
  39. ->setCode(Count::TOO_MANY_ERROR)
  40. ->addViolation();
  41. } else {
  42. $this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
  43. ->setParameter('{{ count }}', $count)
  44. ->setParameter('{{ limit }}', $constraint->max)
  45. ->setInvalidValue($value)
  46. ->setPlural((int) $constraint->max)
  47. ->setCode(Count::TOO_MANY_ERROR)
  48. ->addViolation();
  49. }
  50. return;
  51. }
  52. if (null !== $constraint->min && $count < $constraint->min) {
  53. if ($this->context instanceof ExecutionContextInterface) {
  54. $this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)
  55. ->setParameter('{{ count }}', $count)
  56. ->setParameter('{{ limit }}', $constraint->min)
  57. ->setInvalidValue($value)
  58. ->setPlural((int) $constraint->min)
  59. ->setCode(Count::TOO_FEW_ERROR)
  60. ->addViolation();
  61. } else {
  62. $this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)
  63. ->setParameter('{{ count }}', $count)
  64. ->setParameter('{{ limit }}', $constraint->min)
  65. ->setInvalidValue($value)
  66. ->setPlural((int) $constraint->min)
  67. ->setCode(Count::TOO_FEW_ERROR)
  68. ->addViolation();
  69. }
  70. }
  71. }
  72. }