Count.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_DIVISIBLE_BY_ERROR = DivisibleBy::NOT_DIVISIBLE_BY;
  25. protected static $errorNames = [
  26. self::TOO_FEW_ERROR => 'TOO_FEW_ERROR',
  27. self::TOO_MANY_ERROR => 'TOO_MANY_ERROR',
  28. self::NOT_DIVISIBLE_BY_ERROR => 'NOT_DIVISIBLE_BY_ERROR',
  29. ];
  30. public $minMessage = 'This collection should contain {{ limit }} element or more.|This collection should contain {{ limit }} elements or more.';
  31. public $maxMessage = 'This collection should contain {{ limit }} element or less.|This collection should contain {{ limit }} elements or less.';
  32. public $exactMessage = 'This collection should contain exactly {{ limit }} element.|This collection should contain exactly {{ limit }} elements.';
  33. public $divisibleByMessage = 'The number of elements in this collection should be a multiple of {{ compared_value }}.';
  34. public $min;
  35. public $max;
  36. public $divisibleBy;
  37. /**
  38. * {@inheritdoc}
  39. *
  40. * @param int|array|null $exactly The expected exact count or a set of options
  41. */
  42. public function __construct(
  43. $exactly = null,
  44. int $min = null,
  45. int $max = null,
  46. int $divisibleBy = null,
  47. string $exactMessage = null,
  48. string $minMessage = null,
  49. string $maxMessage = null,
  50. string $divisibleByMessage = null,
  51. array $groups = null,
  52. $payload = null,
  53. array $options = []
  54. ) {
  55. if (\is_array($exactly)) {
  56. $options = array_merge($exactly, $options);
  57. $exactly = $options['value'] ?? null;
  58. }
  59. $min = $min ?? $options['min'] ?? null;
  60. $max = $max ?? $options['max'] ?? null;
  61. unset($options['value'], $options['min'], $options['max']);
  62. if (null !== $exactly && null === $min && null === $max) {
  63. $min = $max = $exactly;
  64. }
  65. parent::__construct($options, $groups, $payload);
  66. $this->min = $min;
  67. $this->max = $max;
  68. $this->divisibleBy = $divisibleBy ?? $this->divisibleBy;
  69. $this->exactMessage = $exactMessage ?? $this->exactMessage;
  70. $this->minMessage = $minMessage ?? $this->minMessage;
  71. $this->maxMessage = $maxMessage ?? $this->maxMessage;
  72. $this->divisibleByMessage = $divisibleByMessage ?? $this->divisibleByMessage;
  73. if (null === $this->min && null === $this->max && null === $this->divisibleBy) {
  74. throw new MissingOptionsException(sprintf('Either option "min", "max" or "divisibleBy" must be given for constraint "%s".', __CLASS__), ['min', 'max', 'divisibleBy']);
  75. }
  76. }
  77. }