comparison vendor/symfony/validator/Constraint.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children 12f9dff5fda9
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
47 const PROPERTY_CONSTRAINT = 'property'; 47 const PROPERTY_CONSTRAINT = 'property';
48 48
49 /** 49 /**
50 * Maps error codes to the names of their constants. 50 * Maps error codes to the names of their constants.
51 */ 51 */
52 protected static $errorNames = array(); 52 protected static $errorNames = [];
53 53
54 /** 54 /**
55 * Domain-specific data attached to a constraint. 55 * Domain-specific data attached to a constraint.
56 * 56 *
57 * @var mixed 57 * @var mixed
68 * @throws InvalidArgumentException If the error code does not exist 68 * @throws InvalidArgumentException If the error code does not exist
69 */ 69 */
70 public static function getErrorName($errorCode) 70 public static function getErrorName($errorCode)
71 { 71 {
72 if (!isset(static::$errorNames[$errorCode])) { 72 if (!isset(static::$errorNames[$errorCode])) {
73 throw new InvalidArgumentException(sprintf( 73 throw new InvalidArgumentException(sprintf('The error code "%s" does not exist for constraint of type "%s".', $errorCode, \get_called_class()));
74 'The error code "%s" does not exist for constraint of type "%s".',
75 $errorCode,
76 get_called_class()
77 ));
78 } 74 }
79 75
80 return static::$errorNames[$errorCode]; 76 return static::$errorNames[$errorCode];
81 } 77 }
82 78
107 * array, but getDefaultOption() returns 103 * array, but getDefaultOption() returns
108 * null 104 * null
109 */ 105 */
110 public function __construct($options = null) 106 public function __construct($options = null)
111 { 107 {
112 $invalidOptions = array(); 108 $invalidOptions = [];
113 $missingOptions = array_flip((array) $this->getRequiredOptions()); 109 $missingOptions = array_flip((array) $this->getRequiredOptions());
114 $knownOptions = get_object_vars($this); 110 $knownOptions = get_object_vars($this);
115 111
116 // The "groups" option is added to the object lazily 112 // The "groups" option is added to the object lazily
117 $knownOptions['groups'] = true; 113 $knownOptions['groups'] = true;
118 114
119 if (is_array($options) && count($options) >= 1 && isset($options['value']) && !property_exists($this, 'value')) { 115 if (\is_array($options) && \count($options) >= 1 && isset($options['value']) && !property_exists($this, 'value')) {
120 $options[$this->getDefaultOption()] = $options['value']; 116 $options[$this->getDefaultOption()] = $options['value'];
121 unset($options['value']); 117 unset($options['value']);
122 } 118 }
123 119
124 if (is_array($options)) { 120 if (\is_array($options)) {
125 reset($options); 121 reset($options);
126 } 122 }
127 if (is_array($options) && count($options) > 0 && is_string(key($options))) { 123 if ($options && \is_array($options) && \is_string(key($options))) {
128 foreach ($options as $option => $value) { 124 foreach ($options as $option => $value) {
129 if (array_key_exists($option, $knownOptions)) { 125 if (array_key_exists($option, $knownOptions)) {
130 $this->$option = $value; 126 $this->$option = $value;
131 unset($missingOptions[$option]); 127 unset($missingOptions[$option]);
132 } else { 128 } else {
133 $invalidOptions[] = $option; 129 $invalidOptions[] = $option;
134 } 130 }
135 } 131 }
136 } elseif (null !== $options && !(is_array($options) && 0 === count($options))) { 132 } elseif (null !== $options && !(\is_array($options) && 0 === \count($options))) {
137 $option = $this->getDefaultOption(); 133 $option = $this->getDefaultOption();
138 134
139 if (null === $option) { 135 if (null === $option) {
140 throw new ConstraintDefinitionException( 136 throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint %s', \get_class($this)));
141 sprintf('No default option is configured for constraint %s', get_class($this))
142 );
143 } 137 }
144 138
145 if (array_key_exists($option, $knownOptions)) { 139 if (array_key_exists($option, $knownOptions)) {
146 $this->$option = $options; 140 $this->$option = $options;
147 unset($missingOptions[$option]); 141 unset($missingOptions[$option]);
148 } else { 142 } else {
149 $invalidOptions[] = $option; 143 $invalidOptions[] = $option;
150 } 144 }
151 } 145 }
152 146
153 if (count($invalidOptions) > 0) { 147 if (\count($invalidOptions) > 0) {
154 throw new InvalidOptionsException( 148 throw new InvalidOptionsException(sprintf('The options "%s" do not exist in constraint %s', implode('", "', $invalidOptions), \get_class($this)), $invalidOptions);
155 sprintf('The options "%s" do not exist in constraint %s', implode('", "', $invalidOptions), get_class($this)), 149 }
156 $invalidOptions 150
157 ); 151 if (\count($missingOptions) > 0) {
158 } 152 throw new MissingOptionsException(sprintf('The options "%s" must be set for constraint %s', implode('", "', array_keys($missingOptions)), \get_class($this)), array_keys($missingOptions));
159
160 if (count($missingOptions) > 0) {
161 throw new MissingOptionsException(
162 sprintf('The options "%s" must be set for constraint %s', implode('", "', array_keys($missingOptions)), get_class($this)),
163 array_keys($missingOptions)
164 );
165 } 153 }
166 } 154 }
167 155
168 /** 156 /**
169 * Sets the value of a lazily initialized option. 157 * Sets the value of a lazily initialized option.
183 $this->groups = (array) $value; 171 $this->groups = (array) $value;
184 172
185 return; 173 return;
186 } 174 }
187 175
188 throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint %s', $option, get_class($this)), array($option)); 176 throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint %s', $option, \get_class($this)), [$option]);
189 } 177 }
190 178
191 /** 179 /**
192 * Returns the value of a lazily initialized option. 180 * Returns the value of a lazily initialized option.
193 * 181 *
204 * @internal this method should not be used or overwritten in userland code 192 * @internal this method should not be used or overwritten in userland code
205 */ 193 */
206 public function __get($option) 194 public function __get($option)
207 { 195 {
208 if ('groups' === $option) { 196 if ('groups' === $option) {
209 $this->groups = array(self::DEFAULT_GROUP); 197 $this->groups = [self::DEFAULT_GROUP];
210 198
211 return $this->groups; 199 return $this->groups;
212 } 200 }
213 201
214 throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint %s', $option, get_class($this)), array($option)); 202 throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint %s', $option, \get_class($this)), [$option]);
215 } 203 }
216 204
217 /** 205 /**
218 * @param string $option The option name 206 * @param string $option The option name
219 * 207 *
229 * 217 *
230 * @param string $group 218 * @param string $group
231 */ 219 */
232 public function addImplicitGroupName($group) 220 public function addImplicitGroupName($group)
233 { 221 {
234 if (in_array(self::DEFAULT_GROUP, $this->groups) && !in_array($group, $this->groups)) { 222 if (\in_array(self::DEFAULT_GROUP, $this->groups) && !\in_array($group, $this->groups)) {
235 $this->groups[] = $group; 223 $this->groups[] = $group;
236 } 224 }
237 } 225 }
238 226
239 /** 227 /**
258 * 246 *
259 * @see __construct() 247 * @see __construct()
260 */ 248 */
261 public function getRequiredOptions() 249 public function getRequiredOptions()
262 { 250 {
263 return array(); 251 return [];
264 } 252 }
265 253
266 /** 254 /**
267 * Returns the name of the class that validates this constraint. 255 * Returns the name of the class that validates this constraint.
268 * 256 *
272 * 260 *
273 * @return string 261 * @return string
274 */ 262 */
275 public function validatedBy() 263 public function validatedBy()
276 { 264 {
277 return get_class($this).'Validator'; 265 return \get_class($this).'Validator';
278 } 266 }
279 267
280 /** 268 /**
281 * Returns whether the constraint can be put onto classes, properties or 269 * Returns whether the constraint can be put onto classes, properties or
282 * both. 270 * both.
294 /** 282 /**
295 * Optimizes the serialized value to minimize storage space. 283 * Optimizes the serialized value to minimize storage space.
296 * 284 *
297 * @return array The properties to serialize 285 * @return array The properties to serialize
298 * 286 *
299 * @internal This method may be replaced by an implementation of 287 * @internal
300 * {@link \Serializable} in the future. Please don't use or
301 * overwrite it.
302 */ 288 */
303 public function __sleep() 289 public function __sleep()
304 { 290 {
305 // Initialize "groups" option if it is not set 291 // Initialize "groups" option if it is not set
306 $this->groups; 292 $this->groups;