vendor/symfony/validator/Constraints/Range.php line 27

  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\PropertyAccess\PropertyAccess;
  12. use Symfony\Component\Validator\Constraint;
  13. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  14. use Symfony\Component\Validator\Exception\LogicException;
  15. use Symfony\Component\Validator\Exception\MissingOptionsException;
  16. /**
  17.  * @Annotation
  18.  * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
  19.  *
  20.  * @author Bernhard Schussek <bschussek@gmail.com>
  21.  */
  22. #[\Attribute(\Attribute::TARGET_PROPERTY \Attribute::TARGET_METHOD \Attribute::IS_REPEATABLE)]
  23. class Range extends Constraint
  24. {
  25.     public const INVALID_CHARACTERS_ERROR 'ad9a9798-7a99-4df7-8ce9-46e416a1e60b';
  26.     public const NOT_IN_RANGE_ERROR '04b91c99-a946-4221-afc5-e65ebac401eb';
  27.     public const TOO_HIGH_ERROR '2d28afcb-e32e-45fb-a815-01c431a86a69';
  28.     public const TOO_LOW_ERROR '76454e69-502c-46c5-9643-f447d837c4d5';
  29.     protected const ERROR_NAMES = [
  30.         self::INVALID_CHARACTERS_ERROR => 'INVALID_CHARACTERS_ERROR',
  31.         self::NOT_IN_RANGE_ERROR => 'NOT_IN_RANGE_ERROR',
  32.         self::TOO_HIGH_ERROR => 'TOO_HIGH_ERROR',
  33.         self::TOO_LOW_ERROR => 'TOO_LOW_ERROR',
  34.     ];
  35.     /**
  36.      * @deprecated since Symfony 6.1, use const ERROR_NAMES instead
  37.      */
  38.     protected static $errorNames self::ERROR_NAMES;
  39.     public $notInRangeMessage 'This value should be between {{ min }} and {{ max }}.';
  40.     public $minMessage 'This value should be {{ limit }} or more.';
  41.     public $maxMessage 'This value should be {{ limit }} or less.';
  42.     public $invalidMessage 'This value should be a valid number.';
  43.     public $invalidDateTimeMessage 'This value should be a valid datetime.';
  44.     public $min;
  45.     public $minPropertyPath;
  46.     public $max;
  47.     public $maxPropertyPath;
  48.     public function __construct(
  49.         array $options null,
  50.         string $notInRangeMessage null,
  51.         string $minMessage null,
  52.         string $maxMessage null,
  53.         string $invalidMessage null,
  54.         string $invalidDateTimeMessage null,
  55.         mixed $min null,
  56.         string $minPropertyPath null,
  57.         mixed $max null,
  58.         string $maxPropertyPath null,
  59.         array $groups null,
  60.         mixed $payload null
  61.     ) {
  62.         parent::__construct($options$groups$payload);
  63.         $this->notInRangeMessage $notInRangeMessage ?? $this->notInRangeMessage;
  64.         $this->minMessage $minMessage ?? $this->minMessage;
  65.         $this->maxMessage $maxMessage ?? $this->maxMessage;
  66.         $this->invalidMessage $invalidMessage ?? $this->invalidMessage;
  67.         $this->invalidDateTimeMessage $invalidDateTimeMessage ?? $this->invalidDateTimeMessage;
  68.         $this->min $min ?? $this->min;
  69.         $this->minPropertyPath $minPropertyPath ?? $this->minPropertyPath;
  70.         $this->max $max ?? $this->max;
  71.         $this->maxPropertyPath $maxPropertyPath ?? $this->maxPropertyPath;
  72.         if (null === $this->min && null === $this->minPropertyPath && null === $this->max && null === $this->maxPropertyPath) {
  73.             throw new MissingOptionsException(sprintf('Either option "min", "minPropertyPath", "max" or "maxPropertyPath" must be given for constraint "%s".'__CLASS__), ['min''minPropertyPath''max''maxPropertyPath']);
  74.         }
  75.         if (null !== $this->min && null !== $this->minPropertyPath) {
  76.             throw new ConstraintDefinitionException(sprintf('The "%s" constraint requires only one of the "min" or "minPropertyPath" options to be set, not both.', static::class));
  77.         }
  78.         if (null !== $this->max && null !== $this->maxPropertyPath) {
  79.             throw new ConstraintDefinitionException(sprintf('The "%s" constraint requires only one of the "max" or "maxPropertyPath" options to be set, not both.', static::class));
  80.         }
  81.         if ((null !== $this->minPropertyPath || null !== $this->maxPropertyPath) && !class_exists(PropertyAccess::class)) {
  82.             throw new LogicException(sprintf('The "%s" constraint requires the Symfony PropertyAccess component to use the "minPropertyPath" or "maxPropertyPath" option.', static::class));
  83.         }
  84.         if (null !== $this->min && null !== $this->max && ($minMessage || $maxMessage)) {
  85.             throw new ConstraintDefinitionException(sprintf('The "%s" constraint can not use "minMessage" and "maxMessage" when the "min" and "max" options are both set. Use "notInRangeMessage" instead.', static::class));
  86.         }
  87.     }
  88. }