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

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