vendor/symfony/validator/Constraints/Length.php line 25

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\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 NOT_EQUAL_LENGTH_ERROR = '4b6f5c76-22b4-409d-af16-fbe823ba9332';
  26. public const INVALID_CHARACTERS_ERROR = '35e6a710-aa2e-4719-b58e-24b35749b767';
  27. protected static $errorNames = [
  28. self::TOO_SHORT_ERROR => 'TOO_SHORT_ERROR',
  29. self::TOO_LONG_ERROR => 'TOO_LONG_ERROR',
  30. self::NOT_EQUAL_LENGTH_ERROR => 'NOT_EQUAL_LENGTH_ERROR',
  31. self::INVALID_CHARACTERS_ERROR => 'INVALID_CHARACTERS_ERROR',
  32. ];
  33. 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.';
  34. 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.';
  35. public $exactMessage = 'This value should have exactly {{ limit }} character.|This value should have exactly {{ limit }} characters.';
  36. public $charsetMessage = 'This value does not match the expected {{ charset }} charset.';
  37. public $max;
  38. public $min;
  39. public $charset = 'UTF-8';
  40. public $normalizer;
  41. public $allowEmptyString = false;
  42. /**
  43. * {@inheritdoc}
  44. *
  45. * @param int|array|null $exactly The expected exact length or a set of options
  46. */
  47. public function __construct(
  48. $exactly = null,
  49. ?int $min = null,
  50. ?int $max = null,
  51. ?string $charset = null,
  52. ?callable $normalizer = null,
  53. ?string $exactMessage = null,
  54. ?string $minMessage = null,
  55. ?string $maxMessage = null,
  56. ?string $charsetMessage = null,
  57. ?array $groups = null,
  58. $payload = null,
  59. array $options = []
  60. ) {
  61. if (\is_array($exactly)) {
  62. $options = array_merge($exactly, $options);
  63. $exactly = $options['value'] ?? null;
  64. }
  65. $min = $min ?? $options['min'] ?? null;
  66. $max = $max ?? $options['max'] ?? null;
  67. unset($options['value'], $options['min'], $options['max']);
  68. if (null !== $exactly && null === $min && null === $max) {
  69. $min = $max = $exactly;
  70. }
  71. parent::__construct($options, $groups, $payload);
  72. $this->min = $min;
  73. $this->max = $max;
  74. $this->charset = $charset ?? $this->charset;
  75. $this->normalizer = $normalizer ?? $this->normalizer;
  76. $this->exactMessage = $exactMessage ?? $this->exactMessage;
  77. $this->minMessage = $minMessage ?? $this->minMessage;
  78. $this->maxMessage = $maxMessage ?? $this->maxMessage;
  79. $this->charsetMessage = $charsetMessage ?? $this->charsetMessage;
  80. if (null === $this->min && null === $this->max) {
  81. throw new MissingOptionsException(sprintf('Either option "min" or "max" must be given for constraint "%s".', __CLASS__), ['min', 'max']);
  82. }
  83. if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
  84. throw new InvalidArgumentException(sprintf('The "normalizer" option must be a valid callable ("%s" given).', get_debug_type($this->normalizer)));
  85. }
  86. if (isset($options['allowEmptyString'])) {
  87. trigger_deprecation('symfony/validator', '5.2', sprintf('The "allowEmptyString" option of the "%s" constraint is deprecated.', self::class));
  88. }
  89. }
  90. }