Skip to content

Commit c91ad12

Browse files
committed
feature symfony#38382 [Validator] Use comparison constraints as attributes (derrabus)
This PR was merged into the 5.2-dev branch. Discussion ---------- [Validator] Use comparison constraints as attributes | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | yes | Tickets | symfony#38096 | License | MIT | Doc PR | TODO, let's add it to symfony/symfony-docs#14305 This PR enables all child classes of `AbstractComparison` to be used as attributes. Some of those constraints used a trait called `NumberConstraintTrait` for a shared implementation. After my changes, that trait did not fit well anymore, so I've added a new `ZeroComparisonConstraintTrait` as a replacement. Although I don't expect `NumberConstraintTrait` to provide much value outside of the Symfony codebase, I think we cannot safely change it because it was not labelled as `@internal`. This is basically why I went for the deprecation. Commits ------- b5bdf82 [Validator] Use comparison constraints as attributes.
2 parents dfcde5b + b5bdf82 commit c91ad12

32 files changed

+795
-58
lines changed

UPGRADE-5.2.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ Validator
105105
*/
106106
```
107107

108+
* Deprecated the `NumberConstraintTrait` trait.
109+
108110
Security
109111
--------
110112

UPGRADE-6.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ Validator
184184
*/
185185
```
186186

187+
* Removed the `NumberConstraintTrait` trait.
188+
187189
Yaml
188190
----
189191

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ CHANGELOG
3333
* added the `ULID` constraint and validator
3434
* added support for UUIDv6 in `Uuid` constraint
3535
* enabled the validator to load constraints from PHP attributes
36+
* deprecated the `NumberConstraintTrait` trait
3637

3738
5.1.0
3839
-----

src/Symfony/Component/Validator/Constraints/AbstractComparison.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,33 @@ abstract class AbstractComparison extends Constraint
3030

3131
/**
3232
* {@inheritdoc}
33+
*
34+
* @param mixed $value the value to compare or a set of options
3335
*/
34-
public function __construct($options = null)
36+
public function __construct($value = null, $propertyPath = null, string $message = null, array $groups = null, $payload = null, array $options = [])
3537
{
36-
if (null === $options) {
37-
$options = [];
38+
if (\is_array($value)) {
39+
$options = array_merge($value, $options);
40+
} elseif (null !== $value) {
41+
$options['value'] = $value;
3842
}
3943

40-
if (\is_array($options)) {
41-
if (!isset($options['value']) && !isset($options['propertyPath'])) {
42-
throw new ConstraintDefinitionException(sprintf('The "%s" constraint requires either the "value" or "propertyPath" option to be set.', static::class));
43-
}
44+
parent::__construct($options, $groups, $payload);
4445

45-
if (isset($options['value']) && isset($options['propertyPath'])) {
46-
throw new ConstraintDefinitionException(sprintf('The "%s" constraint requires only one of the "value" or "propertyPath" options to be set, not both.', static::class));
47-
}
46+
$this->message = $message ?? $this->message;
47+
$this->propertyPath = $propertyPath ?? $this->propertyPath;
4848

49-
if (isset($options['propertyPath']) && !class_exists(PropertyAccess::class)) {
50-
throw new LogicException(sprintf('The "%s" constraint requires the Symfony PropertyAccess component to use the "propertyPath" option.', static::class));
51-
}
49+
if (null === $this->value && null === $this->propertyPath) {
50+
throw new ConstraintDefinitionException(sprintf('The "%s" constraint requires either the "value" or "propertyPath" option to be set.', static::class));
5251
}
5352

54-
parent::__construct($options);
53+
if (null !== $this->value && null !== $this->propertyPath) {
54+
throw new ConstraintDefinitionException(sprintf('The "%s" constraint requires only one of the "value" or "propertyPath" options to be set, not both.', static::class));
55+
}
56+
57+
if (null !== $this->propertyPath && !class_exists(PropertyAccess::class)) {
58+
throw new LogicException(sprintf('The "%s" constraint requires the Symfony PropertyAccess component to use the "propertyPath" option.', static::class));
59+
}
5560
}
5661

5762
/**

src/Symfony/Component/Validator/Constraints/DivisibleBy.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*
1818
* @author Colin O'Dell <[email protected]>
1919
*/
20+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
2021
class DivisibleBy extends AbstractComparison
2122
{
2223
const NOT_DIVISIBLE_BY = '6d99d6c3-1464-4ccf-bdc7-14d083cf455c';

src/Symfony/Component/Validator/Constraints/EqualTo.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* @author Daniel Holmes <[email protected]>
1919
* @author Bernhard Schussek <[email protected]>
2020
*/
21+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
2122
class EqualTo extends AbstractComparison
2223
{
2324
const NOT_EQUAL_ERROR = '478618a7-95ba-473d-9101-cabd45e49115';

src/Symfony/Component/Validator/Constraints/GreaterThan.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* @author Daniel Holmes <[email protected]>
1919
* @author Bernhard Schussek <[email protected]>
2020
*/
21+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
2122
class GreaterThan extends AbstractComparison
2223
{
2324
const TOO_LOW_ERROR = '778b7ae0-84d3-481a-9dec-35fdb64b1d78';

src/Symfony/Component/Validator/Constraints/GreaterThanOrEqual.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* @author Daniel Holmes <[email protected]>
1919
* @author Bernhard Schussek <[email protected]>
2020
*/
21+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
2122
class GreaterThanOrEqual extends AbstractComparison
2223
{
2324
const TOO_LOW_ERROR = 'ea4e51d1-3342-48bd-87f1-9e672cd90cad';

src/Symfony/Component/Validator/Constraints/IdenticalTo.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* @author Daniel Holmes <[email protected]>
1919
* @author Bernhard Schussek <[email protected]>
2020
*/
21+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
2122
class IdenticalTo extends AbstractComparison
2223
{
2324
const NOT_IDENTICAL_ERROR = '2a8cc50f-58a2-4536-875e-060a2ce69ed5';

src/Symfony/Component/Validator/Constraints/LessThan.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* @author Daniel Holmes <[email protected]>
1919
* @author Bernhard Schussek <[email protected]>
2020
*/
21+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
2122
class LessThan extends AbstractComparison
2223
{
2324
const TOO_HIGH_ERROR = '079d7420-2d13-460c-8756-de810eeb37d2';

0 commit comments

Comments
 (0)