vendor/symfony/validator/Constraints/Count.php line 24

Open in your IDE?
  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\Exception\MissingOptionsException;
  13. /**
  14. * @Annotation
  15. * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
  16. *
  17. * @author Bernhard Schussek <bschussek@gmail.com>
  18. */
  19. #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
  20. class Count extends Constraint
  21. {
  22. public const TOO_FEW_ERROR = 'bef8e338-6ae5-4caf-b8e2-50e7b0579e69';
  23. public const TOO_MANY_ERROR = '756b1212-697c-468d-a9ad-50dd783bb169';
  24. public const NOT_EQUAL_COUNT_ERROR = '9fe5d43f-3784-4ece-a0e1-473fc02dadbc';
  25. public const NOT_DIVISIBLE_BY_ERROR = DivisibleBy::NOT_DIVISIBLE_BY;
  26. protected static $errorNames = [
  27. self::TOO_FEW_ERROR => 'TOO_FEW_ERROR',
  28. self::TOO_MANY_ERROR => 'TOO_MANY_ERROR',
  29. self::NOT_EQUAL_COUNT_ERROR => 'NOT_EQUAL_COUNT_ERROR',
  30. self::NOT_DIVISIBLE_BY_ERROR => 'NOT_DIVISIBLE_BY_ERROR',
  31. ];
  32. public $minMessage = 'This collection should contain {{ limit }} element or more.|This collection should contain {{ limit }} elements or more.';
  33. public $maxMessage = 'This collection should contain {{ limit }} element or less.|This collection should contain {{ limit }} elements or less.';
  34. public $exactMessage = 'This collection should contain exactly {{ limit }} element.|This collection should contain exactly {{ limit }} elements.';
  35. public $divisibleByMessage = 'The number of elements in this collection should be a multiple of {{ compared_value }}.';
  36. public $min;
  37. public $max;
  38. public $divisibleBy;
  39. /**
  40. * {@inheritdoc}
  41. *
  42. * @param int|array|null $exactly The expected exact count or a set of options
  43. */
  44. public function __construct(
  45. $exactly = null,
  46. ?int $min = null,
  47. ?int $max = null,
  48. ?int $divisibleBy = null,
  49. ?string $exactMessage = null,
  50. ?string $minMessage = null,
  51. ?string $maxMessage = null,
  52. ?string $divisibleByMessage = null,
  53. ?array $groups = null,
  54. $payload = null,
  55. array $options = []
  56. ) {
  57. if (\is_array($exactly)) {
  58. $options = array_merge($exactly, $options);
  59. $exactly = $options['value'] ?? null;
  60. }
  61. $min = $min ?? $options['min'] ?? null;
  62. $max = $max ?? $options['max'] ?? null;
  63. unset($options['value'], $options['min'], $options['max']);
  64. if (null !== $exactly && null === $min && null === $max) {
  65. $min = $max = $exactly;
  66. }
  67. parent::__construct($options, $groups, $payload);
  68. $this->min = $min;
  69. $this->max = $max;
  70. $this->divisibleBy = $divisibleBy ?? $this->divisibleBy;
  71. $this->exactMessage = $exactMessage ?? $this->exactMessage;
  72. $this->minMessage = $minMessage ?? $this->minMessage;
  73. $this->maxMessage = $maxMessage ?? $this->maxMessage;
  74. $this->divisibleByMessage = $divisibleByMessage ?? $this->divisibleByMessage;
  75. if (null === $this->min && null === $this->max && null === $this->divisibleBy) {
  76. throw new MissingOptionsException(sprintf('Either option "min", "max" or "divisibleBy" must be given for constraint "%s".', __CLASS__), ['min', 'max', 'divisibleBy']);
  77. }
  78. }
  79. }