Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/validator/Constraint.php @ 18:af1871eacc83
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:33:08 +0100 |
parents | 129ea1e6d783 |
children |
comparison
equal
deleted
inserted
replaced
17:129ea1e6d783 | 18:af1871eacc83 |
---|---|
103 * array, but getDefaultOption() returns | 103 * array, but getDefaultOption() returns |
104 * null | 104 * null |
105 */ | 105 */ |
106 public function __construct($options = null) | 106 public function __construct($options = null) |
107 { | 107 { |
108 $defaultOption = $this->getDefaultOption(); | |
108 $invalidOptions = []; | 109 $invalidOptions = []; |
109 $missingOptions = array_flip((array) $this->getRequiredOptions()); | 110 $missingOptions = array_flip((array) $this->getRequiredOptions()); |
110 $knownOptions = get_object_vars($this); | 111 $knownOptions = get_object_vars($this); |
111 | 112 |
112 // The "groups" option is added to the object lazily | 113 // The "groups" option is added to the object lazily |
113 $knownOptions['groups'] = true; | 114 $knownOptions['groups'] = true; |
114 | 115 |
115 if (\is_array($options) && \count($options) >= 1 && isset($options['value']) && !property_exists($this, 'value')) { | 116 if (\is_array($options) && isset($options['value']) && !property_exists($this, 'value')) { |
116 $options[$this->getDefaultOption()] = $options['value']; | 117 if (null === $defaultOption) { |
118 throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint "%s".', \get_class($this))); | |
119 } | |
120 | |
121 $options[$defaultOption] = $options['value']; | |
117 unset($options['value']); | 122 unset($options['value']); |
118 } | 123 } |
119 | 124 |
120 if (\is_array($options)) { | 125 if (\is_array($options)) { |
121 reset($options); | 126 reset($options); |
122 } | 127 } |
123 if ($options && \is_array($options) && \is_string(key($options))) { | 128 if ($options && \is_array($options) && \is_string(key($options))) { |
124 foreach ($options as $option => $value) { | 129 foreach ($options as $option => $value) { |
125 if (array_key_exists($option, $knownOptions)) { | 130 if (\array_key_exists($option, $knownOptions)) { |
126 $this->$option = $value; | 131 $this->$option = $value; |
127 unset($missingOptions[$option]); | 132 unset($missingOptions[$option]); |
128 } else { | 133 } else { |
129 $invalidOptions[] = $option; | 134 $invalidOptions[] = $option; |
130 } | 135 } |
131 } | 136 } |
132 } elseif (null !== $options && !(\is_array($options) && 0 === \count($options))) { | 137 } elseif (null !== $options && !(\is_array($options) && 0 === \count($options))) { |
133 $option = $this->getDefaultOption(); | 138 if (null === $defaultOption) { |
134 | 139 throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint "%s".', \get_class($this))); |
135 if (null === $option) { | |
136 throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint %s', \get_class($this))); | |
137 } | 140 } |
138 | 141 |
139 if (array_key_exists($option, $knownOptions)) { | 142 if (\array_key_exists($defaultOption, $knownOptions)) { |
140 $this->$option = $options; | 143 $this->$defaultOption = $options; |
141 unset($missingOptions[$option]); | 144 unset($missingOptions[$defaultOption]); |
142 } else { | 145 } else { |
143 $invalidOptions[] = $option; | 146 $invalidOptions[] = $defaultOption; |
144 } | 147 } |
145 } | 148 } |
146 | 149 |
147 if (\count($invalidOptions) > 0) { | 150 if (\count($invalidOptions) > 0) { |
148 throw new InvalidOptionsException(sprintf('The options "%s" do not exist in constraint %s', implode('", "', $invalidOptions), \get_class($this)), $invalidOptions); | 151 throw new InvalidOptionsException(sprintf('The options "%s" do not exist in constraint "%s".', implode('", "', $invalidOptions), \get_class($this)), $invalidOptions); |
149 } | 152 } |
150 | 153 |
151 if (\count($missingOptions) > 0) { | 154 if (\count($missingOptions) > 0) { |
152 throw new MissingOptionsException(sprintf('The options "%s" must be set for constraint %s', implode('", "', array_keys($missingOptions)), \get_class($this)), array_keys($missingOptions)); | 155 throw new MissingOptionsException(sprintf('The options "%s" must be set for constraint "%s".', implode('", "', array_keys($missingOptions)), \get_class($this)), array_keys($missingOptions)); |
153 } | 156 } |
154 } | 157 } |
155 | 158 |
156 /** | 159 /** |
157 * Sets the value of a lazily initialized option. | 160 * Sets the value of a lazily initialized option. |
171 $this->groups = (array) $value; | 174 $this->groups = (array) $value; |
172 | 175 |
173 return; | 176 return; |
174 } | 177 } |
175 | 178 |
176 throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint %s', $option, \get_class($this)), [$option]); | 179 throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint "%s".', $option, \get_class($this)), [$option]); |
177 } | 180 } |
178 | 181 |
179 /** | 182 /** |
180 * Returns the value of a lazily initialized option. | 183 * Returns the value of a lazily initialized option. |
181 * | 184 * |
197 $this->groups = [self::DEFAULT_GROUP]; | 200 $this->groups = [self::DEFAULT_GROUP]; |
198 | 201 |
199 return $this->groups; | 202 return $this->groups; |
200 } | 203 } |
201 | 204 |
202 throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint %s', $option, \get_class($this)), [$option]); | 205 throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint "%s".', $option, \get_class($this)), [$option]); |
203 } | 206 } |
204 | 207 |
205 /** | 208 /** |
206 * @param string $option The option name | 209 * @param string $option The option name |
207 * | 210 * |
227 /** | 230 /** |
228 * Returns the name of the default option. | 231 * Returns the name of the default option. |
229 * | 232 * |
230 * Override this method to define a default option. | 233 * Override this method to define a default option. |
231 * | 234 * |
232 * @return string | 235 * @return string|null |
233 * | 236 * |
234 * @see __construct() | 237 * @see __construct() |
235 */ | 238 */ |
236 public function getDefaultOption() | 239 public function getDefaultOption() |
237 { | 240 { |
241 return null; | |
238 } | 242 } |
239 | 243 |
240 /** | 244 /** |
241 * Returns the name of the required options. | 245 * Returns the name of the required options. |
242 * | 246 * |
254 /** | 258 /** |
255 * Returns the name of the class that validates this constraint. | 259 * Returns the name of the class that validates this constraint. |
256 * | 260 * |
257 * By default, this is the fully qualified name of the constraint class | 261 * By default, this is the fully qualified name of the constraint class |
258 * suffixed with "Validator". You can override this method to change that | 262 * suffixed with "Validator". You can override this method to change that |
259 * behaviour. | 263 * behavior. |
260 * | 264 * |
261 * @return string | 265 * @return string |
262 */ | 266 */ |
263 public function validatedBy() | 267 public function validatedBy() |
264 { | 268 { |