-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathConstraint.php
More file actions
198 lines (170 loc) · 5.8 KB
/
Constraint.php
File metadata and controls
198 lines (170 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator;
use Symfony\Component\Validator\Exception\InvalidArgumentException;
use Symfony\Component\Validator\Exception\InvalidOptionsException;
/**
* Contains the properties of a constraint definition.
*
* A constraint can be defined on a class, a property or a getter method.
* The Constraint class encapsulates all the configuration required for
* validating this class, property or getter result successfully.
*
* Constraint instances are immutable and serializable.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
abstract class Constraint
{
/**
* The name of the group given to all constraints with no explicit group.
*/
public const DEFAULT_GROUP = 'Default';
/**
* Marks a constraint that can be put onto classes.
*/
public const CLASS_CONSTRAINT = 'class';
/**
* Marks a constraint that can be put onto properties.
*/
public const PROPERTY_CONSTRAINT = 'property';
/**
* Maps error codes to the names of their constants.
*
* @var array<string, string>
*/
protected const ERROR_NAMES = [];
/**
* Domain-specific data attached to a constraint.
*/
public mixed $payload;
/**
* The groups that the constraint belongs to.
*
* @var string[]
*/
public ?array $groups = null;
/**
* Returns the name of the given error code.
*
* @throws InvalidArgumentException If the error code does not exist
*/
public static function getErrorName(string $errorCode): string
{
if (isset(static::ERROR_NAMES[$errorCode])) {
return static::ERROR_NAMES[$errorCode];
}
throw new InvalidArgumentException(\sprintf('The error code "%s" does not exist for constraint of type "%s".', $errorCode, static::class));
}
/**
* Initializes the constraint with the groups and payload options.
*
* @param string[] $groups An array of validation groups
* @param mixed $payload Domain-specific data attached to a constraint
*/
public function __construct(mixed $options = null, ?array $groups = null, mixed $payload = null)
{
unset($this->groups); // enable lazy initialization
if (null !== $groups) {
$this->groups = $groups;
}
$this->payload = $payload;
}
/**
* Sets the value of a lazily initialized option.
*
* Corresponding properties are added to the object on first access. Hence
* this method will be called at most once per constraint instance and
* option name.
*
* @throws InvalidOptionsException If an invalid option name is given
*/
public function __set(string $option, mixed $value): void
{
if ('groups' === $option) {
$this->groups = (array) $value;
return;
}
throw new InvalidOptionsException(\sprintf('The option "%s" does not exist in constraint "%s".', $option, static::class), [$option]);
}
/**
* Returns the value of a lazily initialized option.
*
* Corresponding properties are added to the object on first access. Hence
* this method will be called at most once per constraint instance and
* option name.
*
* @throws InvalidOptionsException If an invalid option name is given
*/
public function __get(string $option): mixed
{
if ('groups' === $option) {
$this->groups = [self::DEFAULT_GROUP];
return $this->groups;
}
throw new InvalidOptionsException(\sprintf('The option "%s" does not exist in constraint "%s".', $option, static::class), [$option]);
}
public function __isset(string $option): bool
{
return 'groups' === $option;
}
/**
* Adds the given group if this constraint is in the Default group.
*/
public function addImplicitGroupName(string $group): void
{
if (null === $this->groups && \array_key_exists('groups', (array) $this)) {
throw new \LogicException(\sprintf('"%s::$groups" is set to null. Did you forget to call "%s::__construct()"?', static::class, self::class));
}
if (\in_array(self::DEFAULT_GROUP, $this->groups) && !\in_array($group, $this->groups, true)) {
$this->groups[] = $group;
}
}
/**
* Returns the name of the class that validates this constraint.
*
* By default, this is the fully qualified name of the constraint class
* suffixed with "Validator". You can override this method to change that
* behavior.
*/
public function validatedBy(): string
{
return static::class.'Validator';
}
/**
* Returns whether the constraint can be put onto classes, properties or
* both.
*
* @return self::CLASS_CONSTRAINT|self::PROPERTY_CONSTRAINT|array<self::CLASS_CONSTRAINT|self::PROPERTY_CONSTRAINT>
*/
public function getTargets(): string|array
{
return self::PROPERTY_CONSTRAINT;
}
/**
* Optimizes the serialized value to minimize storage space.
*/
public function __serialize(): array
{
// Initialize "groups" option if it is not set
$this->groups;
$data = [];
$class = $this::class;
foreach ((array) $this as $k => $v) {
$data[match (true) {
'' === $k || "\0" !== $k[0] => $k,
str_starts_with($k, "\0*\0") => substr($k, 3),
str_starts_with($k, "\0{$class}\0") => substr($k, 2 + \strlen($class)),
default => $k,
}] = $v;
}
return $data;
}
}