Length.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\InvalidArgumentException;
  13. use Symfony\Component\Validator\Exception\MissingOptionsException;
  14. /**
  15. * @Annotation
  16. * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
  17. *
  18. * @author Bernhard Schussek <bschussek@gmail.com>
  19. */
  20. #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
  21. class Length extends Constraint
  22. {
  23. public const TOO_SHORT_ERROR = '9ff3fdc4-b214-49db-8718-39c315e33d45';
  24. public const TOO_LONG_ERROR = 'd94b19cc-114f-4f44-9cc4-4138e80a87b9';
  25. public const INVALID_CHARACTERS_ERROR = '35e6a710-aa2e-4719-b58e-24b35749b767';
  26. protected static $errorNames = [
  27. self::TOO_SHORT_ERROR => 'TOO_SHORT_ERROR',
  28. self::TOO_LONG_ERROR => 'TOO_LONG_ERROR',
  29. self::INVALID_CHARACTERS_ERROR => 'INVALID_CHARACTERS_ERROR',
  30. ];
  31. public $maxMessage = 'This value is too long. It should have {{ limit }} character or less.|This value is too long. It should have {{ limit }} characters or less.';
  32. public $minMessage = 'This value is too short. It should have {{ limit }} character or more.|This value is too short. It should have {{ limit }} characters or more.';
  33. public $exactMessage = 'This value should have exactly {{ limit }} character.|This value should have exactly {{ limit }} characters.';
  34. public $charsetMessage = 'This value does not match the expected {{ charset }} charset.';
  35. public $max;
  36. public $min;
  37. public $charset = 'UTF-8';
  38. public $normalizer;
  39. public $allowEmptyString = false;
  40. /**
  41. * {@inheritdoc}
  42. *
  43. * @param int|array|null $exactly The expected exact length or a set of options
  44. */
  45. public function __construct(
  46. $exactly = null,
  47. int $min = null,
  48. int $max = null,
  49. string $charset = null,
  50. callable $normalizer = null,
  51. string $exactMessage = null,
  52. string $minMessage = null,
  53. string $maxMessage = null,
  54. string $charsetMessage = null,
  55. array $groups = null,
  56. $payload = null,
  57. array $options = []
  58. ) {
  59. if (\is_array($exactly)) {
  60. $options = array_merge($exactly, $options);
  61. $exactly = $options['value'] ?? null;
  62. }
  63. $min = $min ?? $options['min'] ?? null;
  64. $max = $max ?? $options['max'] ?? null;
  65. unset($options['value'], $options['min'], $options['max']);
  66. if (null !== $exactly && null === $min && null === $max) {
  67. $min = $max = $exactly;
  68. }
  69. parent::__construct($options, $groups, $payload);
  70. $this->min = $min;
  71. $this->max = $max;
  72. $this->charset = $charset ?? $this->charset;
  73. $this->normalizer = $normalizer ?? $this->normalizer;
  74. $this->exactMessage = $exactMessage ?? $this->exactMessage;
  75. $this->minMessage = $minMessage ?? $this->minMessage;
  76. $this->maxMessage = $maxMessage ?? $this->maxMessage;
  77. $this->charsetMessage = $charsetMessage ?? $this->charsetMessage;
  78. if (null === $this->min && null === $this->max) {
  79. throw new MissingOptionsException(sprintf('Either option "min" or "max" must be given for constraint "%s".', __CLASS__), ['min', 'max']);
  80. }
  81. if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
  82. throw new InvalidArgumentException(sprintf('The "normalizer" option must be a valid callable ("%s" given).', get_debug_type($this->normalizer)));
  83. }
  84. if (isset($options['allowEmptyString'])) {
  85. trigger_deprecation('symfony/validator', '5.2', sprintf('The "allowEmptyString" option of the "%s" constraint is deprecated.', self::class));
  86. }
  87. }
  88. }