annotate vendor/symfony/validator/Constraint.php @ 14:1fec387a4317

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