Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 853b243

Browse files
committed
[Validator] Constraints as php 8 Attributes.
1 parent 9e4f511 commit 853b243

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+870
-174
lines changed

.github/patch-types.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
case false !== strpos($file, '/src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures'):
3737
case false !== strpos($file, '/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectOuter.php'):
3838
case false !== strpos($file, '/src/Symfony/Component/VarDumper/Tests/Fixtures/LotsOfAttributes.php'):
39+
case false !== strpos($file, '/src/Symfony/Component/Validator/Tests/Fixtures/Attribute/'):
3940
case false !== strpos($file, '/src/Symfony/Component/VarDumper/Tests/Fixtures/MyAttribute.php'):
4041
case false !== strpos($file, '/src/Symfony/Component/VarDumper/Tests/Fixtures/NotLoadableClass.php'):
4142
case false !== strpos($file, '/src/Symfony/Component/VarDumper/Tests/Fixtures/Php74.php') && \PHP_VERSION_ID < 70400:

src/Symfony/Component/Validator/Constraint.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,11 @@ public static function getErrorName($errorCode)
9191
* getRequiredOptions() to return the names of these options. If any
9292
* option is not set here, an exception is thrown.
9393
*
94-
* @param mixed $options The options (as associative array)
95-
* or the value for the default
96-
* option (any other type)
94+
* @param mixed $options The options (as associative array)
95+
* or the value for the default
96+
* option (any other type)
97+
* @param string[] $groups An array of validation groups
98+
* @param mixed $payload Domain-specific data attached to a constraint
9799
*
98100
* @throws InvalidOptionsException When you pass the names of non-existing
99101
* options
@@ -103,9 +105,15 @@ public static function getErrorName($errorCode)
103105
* array, but getDefaultOption() returns
104106
* null
105107
*/
106-
public function __construct($options = null)
108+
public function __construct($options = null, array $groups = null, $payload = null)
107109
{
108-
foreach ($this->normalizeOptions($options) as $name => $value) {
110+
$options = $this->normalizeOptions($options);
111+
if (null !== $groups) {
112+
$options['groups'] = $groups;
113+
}
114+
$options['payload'] = $payload ?? $options['payload'] ?? null;
115+
116+
foreach ($options as $name => $value) {
109117
$this->$name = $value;
110118
}
111119
}

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Attribute;
1415
use Symfony\Component\Validator\Constraint;
1516

1617
/**
@@ -19,6 +20,7 @@
1920
*
2021
* @author Bernhard Schussek <[email protected]>
2122
*/
23+
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
2224
class Callback extends Constraint
2325
{
2426
/**
@@ -28,19 +30,25 @@ class Callback extends Constraint
2830

2931
/**
3032
* {@inheritdoc}
33+
*
34+
* @param array|string|callable $callback The callback or a set of options
3135
*/
32-
public function __construct($options = null)
36+
public function __construct($callback = null, array $groups = null, $payload = null, array $options = [])
3337
{
3438
// Invocation through annotations with an array parameter only
35-
if (\is_array($options) && 1 === \count($options) && isset($options['value'])) {
36-
$options = $options['value'];
39+
if (\is_array($callback) && 1 === \count($callback) && isset($callback['value'])) {
40+
$callback = $callback['value'];
3741
}
3842

39-
if (\is_array($options) && !isset($options['callback']) && !isset($options['groups']) && !isset($options['payload'])) {
40-
$options = ['callback' => $options];
43+
if (!\is_array($callback)
44+
|| (!isset($callback['callback']) && !isset($callback['groups']) && !isset($callback['payload']))
45+
) {
46+
$options['callback'] = $callback;
47+
} else {
48+
$options = array_merge($callback, $options);
4149
}
4250

43-
parent::__construct($options);
51+
parent::__construct($options, $groups, $payload);
4452
}
4553

4654
/**

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Attribute;
1415
use Symfony\Component\Validator\Constraint;
1516

1617
/**
@@ -19,6 +20,7 @@
1920
*
2021
* @author Bernhard Schussek <[email protected]>
2122
*/
23+
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
2224
class Choice extends Constraint
2325
{
2426
const NO_SUCH_CHOICE_ERROR = '8e179f1b-97aa-4560-a02f-2a8b42e49df7';
@@ -49,4 +51,38 @@ public function getDefaultOption()
4951
{
5052
return 'choices';
5153
}
54+
55+
public function __construct(
56+
$choices = null,
57+
$callback = null,
58+
bool $multiple = null,
59+
bool $strict = null,
60+
int $min = null,
61+
int $max = null,
62+
string $message = null,
63+
string $multipleMessage = null,
64+
string $minMessage = null,
65+
string $maxMessage = null,
66+
$groups = null,
67+
$payload = null,
68+
array $options = []
69+
) {
70+
if (\is_array($choices) && \is_string(key($choices))) {
71+
$options = array_merge($choices, $options);
72+
} elseif (null !== $choices) {
73+
$options['choices'] = $choices;
74+
}
75+
76+
parent::__construct($options, $groups, $payload);
77+
78+
$this->callback = $callback ?? $this->callback;
79+
$this->multiple = $multiple ?? $this->multiple;
80+
$this->strict = $strict ?? $this->strict;
81+
$this->min = $min ?? $this->min;
82+
$this->max = $max ?? $this->max;
83+
$this->message = $message ?? $this->message;
84+
$this->multipleMessage = $multipleMessage ?? $this->multipleMessage;
85+
$this->minMessage = $minMessage ?? $this->minMessage;
86+
$this->maxMessage = $maxMessage ?? $this->maxMessage;
87+
}
5288
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Attribute;
15+
1416
/**
1517
* A sequence of validation groups.
1618
*
@@ -51,6 +53,7 @@
5153
*
5254
* @author Bernhard Schussek <[email protected]>
5355
*/
56+
#[Attribute(Attribute::TARGET_CLASS)]
5457
class GroupSequence
5558
{
5659
/**

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Attribute;
15+
1416
/**
1517
* Annotation to define a group sequence provider.
1618
*
@@ -19,6 +21,7 @@
1921
*
2022
* @author Bernhard Schussek <[email protected]>
2123
*/
24+
#[Attribute(Attribute::TARGET_CLASS)]
2225
class GroupSequenceProvider
2326
{
2427
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Attribute;
1415
use Symfony\Component\Validator\Constraint;
1516

1617
/**
@@ -19,6 +20,7 @@
1920
*
2021
* @author Bernhard Schussek <[email protected]>
2122
*/
23+
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
2224
class IsTrue extends Constraint
2325
{
2426
const NOT_TRUE_ERROR = '2beabf1c-54c0-4882-a928-05249b26e23b';
@@ -28,4 +30,12 @@ class IsTrue extends Constraint
2830
];
2931

3032
public $message = 'This value should be true.';
33+
34+
public function __construct(array $options = null, string $message = null, array $groups = null, $payload = null)
35+
{
36+
$options = $options ?? [];
37+
$options['message'] = $message ?? $options['message'] ?? $this->message;
38+
39+
parent::__construct($options, $groups, $payload);
40+
}
3141
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Attribute;
1415
use Symfony\Component\Validator\Constraint;
1516

1617
/**
@@ -19,6 +20,7 @@
1920
*
2021
* @author Bernhard Schussek <[email protected]>
2122
*/
23+
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
2224
class NotNull extends Constraint
2325
{
2426
const IS_NULL_ERROR = 'ad32d13f-c3d4-423b-909a-857b961eb720';
@@ -28,4 +30,12 @@ class NotNull extends Constraint
2830
];
2931

3032
public $message = 'This value should not be null.';
33+
34+
public function __construct(array $options = null, string $message = null, array $groups = null, $payload = null)
35+
{
36+
$options = $options ?? [];
37+
$options['message'] = $message ?? $options['message'] ?? $this->message;
38+
39+
parent::__construct($options, $groups, $payload);
40+
}
3141
}

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

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Attribute;
1415
use Symfony\Component\PropertyAccess\PropertyAccess;
16+
use Symfony\Component\PropertyAccess\PropertyPathInterface;
1517
use Symfony\Component\Validator\Constraint;
1618
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1719
use Symfony\Component\Validator\Exception\LogicException;
@@ -23,6 +25,7 @@
2325
*
2426
* @author Bernhard Schussek <[email protected]>
2527
*/
28+
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
2629
class Range extends Constraint
2730
{
2831
const INVALID_CHARACTERS_ERROR = 'ad9a9798-7a99-4df7-8ce9-46e416a1e60b';
@@ -57,36 +60,62 @@ class Range extends Constraint
5760
*/
5861
public $deprecatedMaxMessageSet = false;
5962

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);
6684

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;
7094

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+
}
7498

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+
}
78102

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));
84105
}
85106

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+
}
87110

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+
}
90119
}
91120
}
92121
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Attribute;
1415
use Symfony\Component\Validator\Constraint;
1516

1617
/**
@@ -19,6 +20,7 @@
1920
*
2021
* @author Bernhard Schussek <[email protected]>
2122
*/
23+
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
2224
class Valid extends Constraint
2325
{
2426
public $traverse = true;

0 commit comments

Comments
 (0)