Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Validator;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
|
Chris@0
|
15 use Symfony\Component\Validator\Exception\InvalidArgumentException;
|
Chris@0
|
16 use Symfony\Component\Validator\Exception\InvalidOptionsException;
|
Chris@0
|
17 use Symfony\Component\Validator\Exception\MissingOptionsException;
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Contains the properties of a constraint definition.
|
Chris@0
|
21 *
|
Chris@0
|
22 * A constraint can be defined on a class, a property or a getter method.
|
Chris@0
|
23 * The Constraint class encapsulates all the configuration required for
|
Chris@0
|
24 * validating this class, property or getter result successfully.
|
Chris@0
|
25 *
|
Chris@0
|
26 * Constraint instances are immutable and serializable.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @property array $groups The groups that the constraint belongs to
|
Chris@0
|
29 *
|
Chris@0
|
30 * @author Bernhard Schussek <bschussek@gmail.com>
|
Chris@0
|
31 */
|
Chris@0
|
32 abstract class Constraint
|
Chris@0
|
33 {
|
Chris@0
|
34 /**
|
Chris@0
|
35 * The name of the group given to all constraints with no explicit group.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @var string
|
Chris@0
|
38 */
|
Chris@0
|
39 const DEFAULT_GROUP = 'Default';
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * Marks a constraint that can be put onto classes.
|
Chris@0
|
43 *
|
Chris@0
|
44 * @var string
|
Chris@0
|
45 */
|
Chris@0
|
46 const CLASS_CONSTRAINT = 'class';
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * Marks a constraint that can be put onto properties.
|
Chris@0
|
50 *
|
Chris@0
|
51 * @var string
|
Chris@0
|
52 */
|
Chris@0
|
53 const PROPERTY_CONSTRAINT = 'property';
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * Maps error codes to the names of their constants.
|
Chris@0
|
57 *
|
Chris@0
|
58 * @var array
|
Chris@0
|
59 */
|
Chris@0
|
60 protected static $errorNames = array();
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * Domain-specific data attached to a constraint.
|
Chris@0
|
64 *
|
Chris@0
|
65 * @var mixed
|
Chris@0
|
66 */
|
Chris@0
|
67 public $payload;
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * Returns the name of the given error code.
|
Chris@0
|
71 *
|
Chris@0
|
72 * @param string $errorCode The error code
|
Chris@0
|
73 *
|
Chris@0
|
74 * @return string The name of the error code
|
Chris@0
|
75 *
|
Chris@0
|
76 * @throws InvalidArgumentException If the error code does not exist
|
Chris@0
|
77 */
|
Chris@0
|
78 public static function getErrorName($errorCode)
|
Chris@0
|
79 {
|
Chris@0
|
80 if (!isset(static::$errorNames[$errorCode])) {
|
Chris@0
|
81 throw new InvalidArgumentException(sprintf(
|
Chris@0
|
82 'The error code "%s" does not exist for constraint of type "%s".',
|
Chris@0
|
83 $errorCode,
|
Chris@0
|
84 get_called_class()
|
Chris@0
|
85 ));
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 return static::$errorNames[$errorCode];
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * Initializes the constraint with options.
|
Chris@0
|
93 *
|
Chris@0
|
94 * You should pass an associative array. The keys should be the names of
|
Chris@0
|
95 * existing properties in this class. The values should be the value for these
|
Chris@0
|
96 * properties.
|
Chris@0
|
97 *
|
Chris@0
|
98 * Alternatively you can override the method getDefaultOption() to return the
|
Chris@0
|
99 * name of an existing property. If no associative array is passed, this
|
Chris@0
|
100 * property is set instead.
|
Chris@0
|
101 *
|
Chris@0
|
102 * You can force that certain options are set by overriding
|
Chris@0
|
103 * getRequiredOptions() to return the names of these options. If any
|
Chris@0
|
104 * option is not set here, an exception is thrown.
|
Chris@0
|
105 *
|
Chris@0
|
106 * @param mixed $options The options (as associative array)
|
Chris@0
|
107 * or the value for the default
|
Chris@0
|
108 * option (any other type)
|
Chris@0
|
109 *
|
Chris@0
|
110 * @throws InvalidOptionsException When you pass the names of non-existing
|
Chris@0
|
111 * options
|
Chris@0
|
112 * @throws MissingOptionsException When you don't pass any of the options
|
Chris@0
|
113 * returned by getRequiredOptions()
|
Chris@0
|
114 * @throws ConstraintDefinitionException When you don't pass an associative
|
Chris@0
|
115 * array, but getDefaultOption() returns
|
Chris@0
|
116 * null
|
Chris@0
|
117 */
|
Chris@0
|
118 public function __construct($options = null)
|
Chris@0
|
119 {
|
Chris@0
|
120 $invalidOptions = array();
|
Chris@0
|
121 $missingOptions = array_flip((array) $this->getRequiredOptions());
|
Chris@0
|
122 $knownOptions = get_object_vars($this);
|
Chris@0
|
123
|
Chris@0
|
124 // The "groups" option is added to the object lazily
|
Chris@0
|
125 $knownOptions['groups'] = true;
|
Chris@0
|
126
|
Chris@0
|
127 if (is_array($options) && count($options) >= 1 && isset($options['value']) && !property_exists($this, 'value')) {
|
Chris@0
|
128 $options[$this->getDefaultOption()] = $options['value'];
|
Chris@0
|
129 unset($options['value']);
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 if (is_array($options)) {
|
Chris@0
|
133 reset($options);
|
Chris@0
|
134 }
|
Chris@0
|
135 if (is_array($options) && count($options) > 0 && is_string(key($options))) {
|
Chris@0
|
136 foreach ($options as $option => $value) {
|
Chris@0
|
137 if (array_key_exists($option, $knownOptions)) {
|
Chris@0
|
138 $this->$option = $value;
|
Chris@0
|
139 unset($missingOptions[$option]);
|
Chris@0
|
140 } else {
|
Chris@0
|
141 $invalidOptions[] = $option;
|
Chris@0
|
142 }
|
Chris@0
|
143 }
|
Chris@0
|
144 } elseif (null !== $options && !(is_array($options) && count($options) === 0)) {
|
Chris@0
|
145 $option = $this->getDefaultOption();
|
Chris@0
|
146
|
Chris@0
|
147 if (null === $option) {
|
Chris@0
|
148 throw new ConstraintDefinitionException(
|
Chris@0
|
149 sprintf('No default option is configured for constraint %s', get_class($this))
|
Chris@0
|
150 );
|
Chris@0
|
151 }
|
Chris@0
|
152
|
Chris@0
|
153 if (array_key_exists($option, $knownOptions)) {
|
Chris@0
|
154 $this->$option = $options;
|
Chris@0
|
155 unset($missingOptions[$option]);
|
Chris@0
|
156 } else {
|
Chris@0
|
157 $invalidOptions[] = $option;
|
Chris@0
|
158 }
|
Chris@0
|
159 }
|
Chris@0
|
160
|
Chris@0
|
161 if (count($invalidOptions) > 0) {
|
Chris@0
|
162 throw new InvalidOptionsException(
|
Chris@0
|
163 sprintf('The options "%s" do not exist in constraint %s', implode('", "', $invalidOptions), get_class($this)),
|
Chris@0
|
164 $invalidOptions
|
Chris@0
|
165 );
|
Chris@0
|
166 }
|
Chris@0
|
167
|
Chris@0
|
168 if (count($missingOptions) > 0) {
|
Chris@0
|
169 throw new MissingOptionsException(
|
Chris@0
|
170 sprintf('The options "%s" must be set for constraint %s', implode('", "', array_keys($missingOptions)), get_class($this)),
|
Chris@0
|
171 array_keys($missingOptions)
|
Chris@0
|
172 );
|
Chris@0
|
173 }
|
Chris@0
|
174 }
|
Chris@0
|
175
|
Chris@0
|
176 /**
|
Chris@0
|
177 * Sets the value of a lazily initialized option.
|
Chris@0
|
178 *
|
Chris@0
|
179 * Corresponding properties are added to the object on first access. Hence
|
Chris@0
|
180 * this method will be called at most once per constraint instance and
|
Chris@0
|
181 * option name.
|
Chris@0
|
182 *
|
Chris@0
|
183 * @param string $option The option name
|
Chris@0
|
184 * @param mixed $value The value to set
|
Chris@0
|
185 *
|
Chris@0
|
186 * @throws InvalidOptionsException If an invalid option name is given
|
Chris@0
|
187 */
|
Chris@0
|
188 public function __set($option, $value)
|
Chris@0
|
189 {
|
Chris@0
|
190 if ('groups' === $option) {
|
Chris@0
|
191 $this->groups = (array) $value;
|
Chris@0
|
192
|
Chris@0
|
193 return;
|
Chris@0
|
194 }
|
Chris@0
|
195
|
Chris@0
|
196 throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint %s', $option, get_class($this)), array($option));
|
Chris@0
|
197 }
|
Chris@0
|
198
|
Chris@0
|
199 /**
|
Chris@0
|
200 * Returns the value of a lazily initialized option.
|
Chris@0
|
201 *
|
Chris@0
|
202 * Corresponding properties are added to the object on first access. Hence
|
Chris@0
|
203 * this method will be called at most once per constraint instance and
|
Chris@0
|
204 * option name.
|
Chris@0
|
205 *
|
Chris@0
|
206 * @param string $option The option name
|
Chris@0
|
207 *
|
Chris@0
|
208 * @return mixed The value of the option
|
Chris@0
|
209 *
|
Chris@0
|
210 * @throws InvalidOptionsException If an invalid option name is given
|
Chris@0
|
211 *
|
Chris@0
|
212 * @internal This method should not be used or overwritten in userland code.
|
Chris@0
|
213 */
|
Chris@0
|
214 public function __get($option)
|
Chris@0
|
215 {
|
Chris@0
|
216 if ('groups' === $option) {
|
Chris@0
|
217 $this->groups = array(self::DEFAULT_GROUP);
|
Chris@0
|
218
|
Chris@0
|
219 return $this->groups;
|
Chris@0
|
220 }
|
Chris@0
|
221
|
Chris@0
|
222 throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint %s', $option, get_class($this)), array($option));
|
Chris@0
|
223 }
|
Chris@0
|
224
|
Chris@0
|
225 /**
|
Chris@0
|
226 * Adds the given group if this constraint is in the Default group.
|
Chris@0
|
227 *
|
Chris@0
|
228 * @param string $group
|
Chris@0
|
229 */
|
Chris@0
|
230 public function addImplicitGroupName($group)
|
Chris@0
|
231 {
|
Chris@0
|
232 if (in_array(self::DEFAULT_GROUP, $this->groups) && !in_array($group, $this->groups)) {
|
Chris@0
|
233 $this->groups[] = $group;
|
Chris@0
|
234 }
|
Chris@0
|
235 }
|
Chris@0
|
236
|
Chris@0
|
237 /**
|
Chris@0
|
238 * Returns the name of the default option.
|
Chris@0
|
239 *
|
Chris@0
|
240 * Override this method to define a default option.
|
Chris@0
|
241 *
|
Chris@0
|
242 * @return string
|
Chris@0
|
243 *
|
Chris@0
|
244 * @see __construct()
|
Chris@0
|
245 */
|
Chris@0
|
246 public function getDefaultOption()
|
Chris@0
|
247 {
|
Chris@0
|
248 }
|
Chris@0
|
249
|
Chris@0
|
250 /**
|
Chris@0
|
251 * Returns the name of the required options.
|
Chris@0
|
252 *
|
Chris@0
|
253 * Override this method if you want to define required options.
|
Chris@0
|
254 *
|
Chris@0
|
255 * @return array
|
Chris@0
|
256 *
|
Chris@0
|
257 * @see __construct()
|
Chris@0
|
258 */
|
Chris@0
|
259 public function getRequiredOptions()
|
Chris@0
|
260 {
|
Chris@0
|
261 return array();
|
Chris@0
|
262 }
|
Chris@0
|
263
|
Chris@0
|
264 /**
|
Chris@0
|
265 * Returns the name of the class that validates this constraint.
|
Chris@0
|
266 *
|
Chris@0
|
267 * By default, this is the fully qualified name of the constraint class
|
Chris@0
|
268 * suffixed with "Validator". You can override this method to change that
|
Chris@0
|
269 * behaviour.
|
Chris@0
|
270 *
|
Chris@0
|
271 * @return string
|
Chris@0
|
272 */
|
Chris@0
|
273 public function validatedBy()
|
Chris@0
|
274 {
|
Chris@0
|
275 return get_class($this).'Validator';
|
Chris@0
|
276 }
|
Chris@0
|
277
|
Chris@0
|
278 /**
|
Chris@0
|
279 * Returns whether the constraint can be put onto classes, properties or
|
Chris@0
|
280 * both.
|
Chris@0
|
281 *
|
Chris@0
|
282 * This method should return one or more of the constants
|
Chris@0
|
283 * Constraint::CLASS_CONSTRAINT and Constraint::PROPERTY_CONSTRAINT.
|
Chris@0
|
284 *
|
Chris@0
|
285 * @return string|array One or more constant values
|
Chris@0
|
286 */
|
Chris@0
|
287 public function getTargets()
|
Chris@0
|
288 {
|
Chris@0
|
289 return self::PROPERTY_CONSTRAINT;
|
Chris@0
|
290 }
|
Chris@0
|
291
|
Chris@0
|
292 /**
|
Chris@0
|
293 * Optimizes the serialized value to minimize storage space.
|
Chris@0
|
294 *
|
Chris@0
|
295 * @return array The properties to serialize
|
Chris@0
|
296 *
|
Chris@0
|
297 * @internal This method may be replaced by an implementation of
|
Chris@0
|
298 * {@link \Serializable} in the future. Please don't use or
|
Chris@0
|
299 * overwrite it.
|
Chris@0
|
300 */
|
Chris@0
|
301 public function __sleep()
|
Chris@0
|
302 {
|
Chris@0
|
303 // Initialize "groups" option if it is not set
|
Chris@0
|
304 $this->groups;
|
Chris@0
|
305
|
Chris@0
|
306 return array_keys(get_object_vars($this));
|
Chris@0
|
307 }
|
Chris@0
|
308 }
|