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

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\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. class Range extends Constraint
  23. {
  24.     public const INVALID_CHARACTERS_ERROR 'ad9a9798-7a99-4df7-8ce9-46e416a1e60b';
  25.     public const NOT_IN_RANGE_ERROR '04b91c99-a946-4221-afc5-e65ebac401eb';
  26.     public const TOO_HIGH_ERROR '2d28afcb-e32e-45fb-a815-01c431a86a69';
  27.     public const TOO_LOW_ERROR '76454e69-502c-46c5-9643-f447d837c4d5';
  28.     protected static $errorNames = [
  29.         self::INVALID_CHARACTERS_ERROR => 'INVALID_CHARACTERS_ERROR',
  30.         self::NOT_IN_RANGE_ERROR => 'NOT_IN_RANGE_ERROR',
  31.         self::TOO_HIGH_ERROR => 'TOO_HIGH_ERROR',
  32.         self::TOO_LOW_ERROR => 'TOO_LOW_ERROR',
  33.     ];
  34.     public $notInRangeMessage 'This value should be between {{ min }} and {{ max }}.';
  35.     public $minMessage 'This value should be {{ limit }} or more.';
  36.     public $maxMessage 'This value should be {{ limit }} or less.';
  37.     public $invalidMessage 'This value should be a valid number.';
  38.     public $min;
  39.     public $minPropertyPath;
  40.     public $max;
  41.     public $maxPropertyPath;
  42.     // BC layer, to be removed in 5.0
  43.     /**
  44.      * @internal
  45.      */
  46.     public $deprecatedMinMessageSet false;
  47.     /**
  48.      * @internal
  49.      */
  50.     public $deprecatedMaxMessageSet false;
  51.     public function __construct($options null)
  52.     {
  53.         if (\is_array($options)) {
  54.             if (isset($options['min']) && isset($options['minPropertyPath'])) {
  55.                 throw new ConstraintDefinitionException(sprintf('The "%s" constraint requires only one of the "min" or "minPropertyPath" options to be set, not both.', static::class));
  56.             }
  57.             if (isset($options['max']) && isset($options['maxPropertyPath'])) {
  58.                 throw new ConstraintDefinitionException(sprintf('The "%s" constraint requires only one of the "max" or "maxPropertyPath" options to be set, not both.', static::class));
  59.             }
  60.             if ((isset($options['minPropertyPath']) || isset($options['maxPropertyPath'])) && !class_exists(PropertyAccess::class)) {
  61.                 throw new LogicException(sprintf('The "%s" constraint requires the Symfony PropertyAccess component to use the "minPropertyPath" or "maxPropertyPath" option.', static::class));
  62.             }
  63.             if (isset($options['min']) && isset($options['max'])) {
  64.                 $this->deprecatedMinMessageSet = isset($options['minMessage']);
  65.                 $this->deprecatedMaxMessageSet = isset($options['maxMessage']);
  66.                 if ($this->deprecatedMinMessageSet || $this->deprecatedMaxMessageSet) {
  67.                     @trigger_error('Since symfony/validator 4.4: "minMessage" and "maxMessage" are deprecated when the "min" and "max" options are both set. Use "notInRangeMessage" instead.', \E_USER_DEPRECATED);
  68.                 }
  69.             }
  70.         }
  71.         parent::__construct($options);
  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.     }
  76. }