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

  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 const ERROR_NAMES = [
  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.     /**
  34.      * @deprecated since Symfony 6.1, use const ERROR_NAMES instead
  35.      */
  36.     protected static $errorNames = self::ERROR_NAMES;
  37.     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.';
  38.     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.';
  39.     public $exactMessage = 'This value should have exactly {{ limit }} character.|This value should have exactly {{ limit }} characters.';
  40.     public $charsetMessage = 'This value does not match the expected {{ charset }} charset.';
  41.     public $max;
  42.     public $min;
  43.     public $charset = 'UTF-8';
  44.     public $normalizer;
  45.     public function __construct(
  46.         int|array $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.         mixed $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 ??= $options['min'] ?? null;
  64.         $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.     }
  85. }