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