|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\Validator\Constraints; |
13 | 13 |
|
| 14 | +use Attribute; |
14 | 15 | use Symfony\Component\PropertyAccess\PropertyAccess; |
| 16 | +use Symfony\Component\PropertyAccess\PropertyPathInterface; |
15 | 17 | use Symfony\Component\Validator\Constraint; |
16 | 18 | use Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
17 | 19 | use Symfony\Component\Validator\Exception\LogicException; |
|
23 | 25 | * |
24 | 26 | * @author Bernhard Schussek <bschussek@gmail.com> |
25 | 27 | */ |
| 28 | +#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)] |
26 | 29 | class Range extends Constraint |
27 | 30 | { |
28 | 31 | const INVALID_CHARACTERS_ERROR = 'ad9a9798-7a99-4df7-8ce9-46e416a1e60b'; |
@@ -57,36 +60,62 @@ class Range extends Constraint |
57 | 60 | */ |
58 | 61 | public $deprecatedMaxMessageSet = false; |
59 | 62 |
|
60 | | - public function __construct($options = null) |
61 | | - { |
62 | | - if (\is_array($options)) { |
63 | | - if (isset($options['min']) && isset($options['minPropertyPath'])) { |
64 | | - throw new ConstraintDefinitionException(sprintf('The "%s" constraint requires only one of the "min" or "minPropertyPath" options to be set, not both.', static::class)); |
65 | | - } |
| 63 | + /** |
| 64 | + * {@inheritdoc} |
| 65 | + * |
| 66 | + * @param string|PropertyPathInterface|null $minPropertyPath |
| 67 | + * @param string|PropertyPathInterface|null $maxPropertyPath |
| 68 | + */ |
| 69 | + public function __construct( |
| 70 | + array $options = null, |
| 71 | + string $notInRangeMessage = null, |
| 72 | + string $minMessage = null, |
| 73 | + string $maxMessage = null, |
| 74 | + string $invalidMessage = null, |
| 75 | + string $invalidDateTimeMessage = null, |
| 76 | + $min = null, |
| 77 | + $minPropertyPath = null, |
| 78 | + $max = null, |
| 79 | + $maxPropertyPath = null, |
| 80 | + array $groups = null, |
| 81 | + $payload = null |
| 82 | + ) { |
| 83 | + parent::__construct($options, $groups, $payload); |
66 | 84 |
|
67 | | - if (isset($options['max']) && isset($options['maxPropertyPath'])) { |
68 | | - throw new ConstraintDefinitionException(sprintf('The "%s" constraint requires only one of the "max" or "maxPropertyPath" options to be set, not both.', static::class)); |
69 | | - } |
| 85 | + $this->notInRangeMessage = $notInRangeMessage ?? $this->notInRangeMessage; |
| 86 | + $this->minMessage = $minMessage ?? $this->minMessage; |
| 87 | + $this->maxMessage = $maxMessage ?? $this->maxMessage; |
| 88 | + $this->invalidMessage = $invalidMessage ?? $this->invalidMessage; |
| 89 | + $this->invalidDateTimeMessage = $invalidDateTimeMessage ?? $this->invalidDateTimeMessage; |
| 90 | + $this->min = $min ?? $this->min; |
| 91 | + $this->minPropertyPath = $minPropertyPath ?? $this->minPropertyPath; |
| 92 | + $this->max = $max ?? $this->max; |
| 93 | + $this->maxPropertyPath = $maxPropertyPath ?? $this->maxPropertyPath; |
70 | 94 |
|
71 | | - if ((isset($options['minPropertyPath']) || isset($options['maxPropertyPath'])) && !class_exists(PropertyAccess::class)) { |
72 | | - throw new LogicException(sprintf('The "%s" constraint requires the Symfony PropertyAccess component to use the "minPropertyPath" or "maxPropertyPath" option.', static::class)); |
73 | | - } |
| 95 | + if (null === $this->min && null === $this->minPropertyPath && null === $this->max && null === $this->maxPropertyPath) { |
| 96 | + throw new MissingOptionsException(sprintf('Either option "min", "minPropertyPath", "max" or "maxPropertyPath" must be given for constraint "%s".', __CLASS__), ['min', 'minPropertyPath', 'max', 'maxPropertyPath']); |
| 97 | + } |
74 | 98 |
|
75 | | - if (isset($options['min']) && isset($options['max'])) { |
76 | | - $this->deprecatedMinMessageSet = isset($options['minMessage']); |
77 | | - $this->deprecatedMaxMessageSet = isset($options['maxMessage']); |
| 99 | + if (null !== $this->min && null !== $this->minPropertyPath) { |
| 100 | + throw new ConstraintDefinitionException(sprintf('The "%s" constraint requires only one of the "min" or "minPropertyPath" options to be set, not both.', static::class)); |
| 101 | + } |
78 | 102 |
|
79 | | - // BC layer, should throw a ConstraintDefinitionException in 6.0 |
80 | | - if ($this->deprecatedMinMessageSet || $this->deprecatedMaxMessageSet) { |
81 | | - trigger_deprecation('symfony/validator', '4.4', '"minMessage" and "maxMessage" are deprecated when the "min" and "max" options are both set. Use "notInRangeMessage" instead.'); |
82 | | - } |
83 | | - } |
| 103 | + if (null !== $this->max && null !== $this->maxPropertyPath) { |
| 104 | + throw new ConstraintDefinitionException(sprintf('The "%s" constraint requires only one of the "max" or "maxPropertyPath" options to be set, not both.', static::class)); |
84 | 105 | } |
85 | 106 |
|
86 | | - parent::__construct($options); |
| 107 | + if ((null !== $this->minPropertyPath || null !== $this->maxPropertyPath) && !class_exists(PropertyAccess::class)) { |
| 108 | + throw new LogicException(sprintf('The "%s" constraint requires the Symfony PropertyAccess component to use the "minPropertyPath" or "maxPropertyPath" option.', static::class)); |
| 109 | + } |
87 | 110 |
|
88 | | - if (null === $this->min && null === $this->minPropertyPath && null === $this->max && null === $this->maxPropertyPath) { |
89 | | - throw new MissingOptionsException(sprintf('Either option "min", "minPropertyPath", "max" or "maxPropertyPath" must be given for constraint "%s".', __CLASS__), ['min', 'minPropertyPath', 'max', 'maxPropertyPath']); |
| 111 | + if (null !== $this->min && null !== $this->max) { |
| 112 | + $this->deprecatedMinMessageSet = isset($options['minMessage']) || null !== $minMessage; |
| 113 | + $this->deprecatedMaxMessageSet = isset($options['maxMessage']) || null !== $maxMessage; |
| 114 | + |
| 115 | + // BC layer, should throw a ConstraintDefinitionException in 6.0 |
| 116 | + if ($this->deprecatedMinMessageSet || $this->deprecatedMaxMessageSet) { |
| 117 | + trigger_deprecation('symfony/validator', '4.4', '"minMessage" and "maxMessage" are deprecated when the "min" and "max" options are both set. Use "notInRangeMessage" instead.'); |
| 118 | + } |
90 | 119 | } |
91 | 120 | } |
92 | 121 | } |
0 commit comments