annotate vendor/symfony/validator/Constraint.php @ 17:129ea1e6d783

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