annotate vendor/symfony/validator/Mapping/ClassMetadata.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
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\Mapping;
Chris@0 13
Chris@0 14 use Symfony\Component\Validator\Constraint;
Chris@0 15 use Symfony\Component\Validator\Constraints\GroupSequence;
Chris@0 16 use Symfony\Component\Validator\Constraints\Traverse;
Chris@0 17 use Symfony\Component\Validator\Constraints\Valid;
Chris@0 18 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
Chris@0 19 use Symfony\Component\Validator\Exception\GroupDefinitionException;
Chris@0 20
Chris@0 21 /**
Chris@0 22 * Default implementation of {@link ClassMetadataInterface}.
Chris@0 23 *
Chris@0 24 * This class supports serialization and cloning.
Chris@0 25 *
Chris@0 26 * @author Bernhard Schussek <bschussek@gmail.com>
Chris@0 27 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 28 */
Chris@0 29 class ClassMetadata extends GenericMetadata implements ClassMetadataInterface
Chris@0 30 {
Chris@0 31 /**
Chris@0 32 * @var string
Chris@0 33 *
Chris@0 34 * @internal This property is public in order to reduce the size of the
Chris@0 35 * class' serialized representation. Do not access it. Use
Chris@0 36 * {@link getClassName()} instead.
Chris@0 37 */
Chris@0 38 public $name;
Chris@0 39
Chris@0 40 /**
Chris@0 41 * @var string
Chris@0 42 *
Chris@0 43 * @internal This property is public in order to reduce the size of the
Chris@0 44 * class' serialized representation. Do not access it. Use
Chris@0 45 * {@link getDefaultGroup()} instead.
Chris@0 46 */
Chris@0 47 public $defaultGroup;
Chris@0 48
Chris@0 49 /**
Chris@14 50 * @var MemberMetadata[][]
Chris@0 51 *
Chris@0 52 * @internal This property is public in order to reduce the size of the
Chris@0 53 * class' serialized representation. Do not access it. Use
Chris@0 54 * {@link getPropertyMetadata()} instead.
Chris@0 55 */
Chris@17 56 public $members = [];
Chris@0 57
Chris@0 58 /**
Chris@0 59 * @var PropertyMetadata[]
Chris@0 60 *
Chris@0 61 * @internal This property is public in order to reduce the size of the
Chris@0 62 * class' serialized representation. Do not access it. Use
Chris@0 63 * {@link getPropertyMetadata()} instead.
Chris@0 64 */
Chris@17 65 public $properties = [];
Chris@0 66
Chris@0 67 /**
Chris@0 68 * @var GetterMetadata[]
Chris@0 69 *
Chris@0 70 * @internal This property is public in order to reduce the size of the
Chris@0 71 * class' serialized representation. Do not access it. Use
Chris@0 72 * {@link getPropertyMetadata()} instead.
Chris@0 73 */
Chris@17 74 public $getters = [];
Chris@0 75
Chris@0 76 /**
Chris@0 77 * @var array
Chris@0 78 *
Chris@0 79 * @internal This property is public in order to reduce the size of the
Chris@0 80 * class' serialized representation. Do not access it. Use
Chris@0 81 * {@link getGroupSequence()} instead.
Chris@0 82 */
Chris@17 83 public $groupSequence = [];
Chris@0 84
Chris@0 85 /**
Chris@0 86 * @var bool
Chris@0 87 *
Chris@0 88 * @internal This property is public in order to reduce the size of the
Chris@0 89 * class' serialized representation. Do not access it. Use
Chris@0 90 * {@link isGroupSequenceProvider()} instead.
Chris@0 91 */
Chris@0 92 public $groupSequenceProvider = false;
Chris@0 93
Chris@0 94 /**
Chris@0 95 * The strategy for traversing traversable objects.
Chris@0 96 *
Chris@0 97 * By default, only instances of {@link \Traversable} are traversed.
Chris@0 98 *
Chris@0 99 * @var int
Chris@0 100 *
Chris@0 101 * @internal This property is public in order to reduce the size of the
Chris@0 102 * class' serialized representation. Do not access it. Use
Chris@0 103 * {@link getTraversalStrategy()} instead.
Chris@0 104 */
Chris@0 105 public $traversalStrategy = TraversalStrategy::IMPLICIT;
Chris@0 106
Chris@0 107 /**
Chris@0 108 * @var \ReflectionClass
Chris@0 109 */
Chris@0 110 private $reflClass;
Chris@0 111
Chris@0 112 /**
Chris@0 113 * Constructs a metadata for the given class.
Chris@0 114 *
Chris@0 115 * @param string $class
Chris@0 116 */
Chris@0 117 public function __construct($class)
Chris@0 118 {
Chris@0 119 $this->name = $class;
Chris@0 120 // class name without namespace
Chris@0 121 if (false !== $nsSep = strrpos($class, '\\')) {
Chris@0 122 $this->defaultGroup = substr($class, $nsSep + 1);
Chris@0 123 } else {
Chris@0 124 $this->defaultGroup = $class;
Chris@0 125 }
Chris@0 126 }
Chris@0 127
Chris@0 128 /**
Chris@0 129 * {@inheritdoc}
Chris@0 130 */
Chris@0 131 public function __sleep()
Chris@0 132 {
Chris@0 133 $parentProperties = parent::__sleep();
Chris@0 134
Chris@0 135 // Don't store the cascading strategy. Classes never cascade.
Chris@0 136 unset($parentProperties[array_search('cascadingStrategy', $parentProperties)]);
Chris@0 137
Chris@17 138 return array_merge($parentProperties, [
Chris@0 139 'getters',
Chris@0 140 'groupSequence',
Chris@0 141 'groupSequenceProvider',
Chris@0 142 'members',
Chris@0 143 'name',
Chris@0 144 'properties',
Chris@0 145 'defaultGroup',
Chris@17 146 ]);
Chris@0 147 }
Chris@0 148
Chris@0 149 /**
Chris@0 150 * {@inheritdoc}
Chris@0 151 */
Chris@0 152 public function getClassName()
Chris@0 153 {
Chris@0 154 return $this->name;
Chris@0 155 }
Chris@0 156
Chris@0 157 /**
Chris@0 158 * Returns the name of the default group for this class.
Chris@0 159 *
Chris@0 160 * For each class, the group "Default" is an alias for the group
Chris@0 161 * "<ClassName>", where <ClassName> is the non-namespaced name of the
Chris@0 162 * class. All constraints implicitly or explicitly assigned to group
Chris@0 163 * "Default" belong to both of these groups, unless the class defines
Chris@0 164 * a group sequence.
Chris@0 165 *
Chris@0 166 * If a class defines a group sequence, validating the class in "Default"
Chris@0 167 * will validate the group sequence. The constraints assigned to "Default"
Chris@0 168 * can still be validated by validating the class in "<ClassName>".
Chris@0 169 *
Chris@0 170 * @return string The name of the default group
Chris@0 171 */
Chris@0 172 public function getDefaultGroup()
Chris@0 173 {
Chris@0 174 return $this->defaultGroup;
Chris@0 175 }
Chris@0 176
Chris@0 177 /**
Chris@0 178 * {@inheritdoc}
Chris@0 179 */
Chris@0 180 public function addConstraint(Constraint $constraint)
Chris@0 181 {
Chris@17 182 if (!\in_array(Constraint::CLASS_CONSTRAINT, (array) $constraint->getTargets())) {
Chris@17 183 throw new ConstraintDefinitionException(sprintf('The constraint "%s" cannot be put on classes.', \get_class($constraint)));
Chris@0 184 }
Chris@0 185
Chris@0 186 if ($constraint instanceof Valid) {
Chris@17 187 throw new ConstraintDefinitionException(sprintf('The constraint "%s" cannot be put on classes.', \get_class($constraint)));
Chris@0 188 }
Chris@0 189
Chris@0 190 if ($constraint instanceof Traverse) {
Chris@0 191 if ($constraint->traverse) {
Chris@0 192 // If traverse is true, traversal should be explicitly enabled
Chris@0 193 $this->traversalStrategy = TraversalStrategy::TRAVERSE;
Chris@0 194 } else {
Chris@0 195 // If traverse is false, traversal should be explicitly disabled
Chris@0 196 $this->traversalStrategy = TraversalStrategy::NONE;
Chris@0 197 }
Chris@0 198
Chris@0 199 // The constraint is not added
Chris@0 200 return $this;
Chris@0 201 }
Chris@0 202
Chris@0 203 $constraint->addImplicitGroupName($this->getDefaultGroup());
Chris@0 204
Chris@0 205 parent::addConstraint($constraint);
Chris@0 206
Chris@0 207 return $this;
Chris@0 208 }
Chris@0 209
Chris@0 210 /**
Chris@0 211 * Adds a constraint to the given property.
Chris@0 212 *
Chris@0 213 * @param string $property The name of the property
Chris@0 214 * @param Constraint $constraint The constraint
Chris@0 215 *
Chris@0 216 * @return $this
Chris@0 217 */
Chris@0 218 public function addPropertyConstraint($property, Constraint $constraint)
Chris@0 219 {
Chris@0 220 if (!isset($this->properties[$property])) {
Chris@0 221 $this->properties[$property] = new PropertyMetadata($this->getClassName(), $property);
Chris@0 222
Chris@0 223 $this->addPropertyMetadata($this->properties[$property]);
Chris@0 224 }
Chris@0 225
Chris@0 226 $constraint->addImplicitGroupName($this->getDefaultGroup());
Chris@0 227
Chris@0 228 $this->properties[$property]->addConstraint($constraint);
Chris@0 229
Chris@0 230 return $this;
Chris@0 231 }
Chris@0 232
Chris@0 233 /**
Chris@0 234 * @param string $property
Chris@0 235 * @param Constraint[] $constraints
Chris@0 236 *
Chris@0 237 * @return $this
Chris@0 238 */
Chris@0 239 public function addPropertyConstraints($property, array $constraints)
Chris@0 240 {
Chris@0 241 foreach ($constraints as $constraint) {
Chris@0 242 $this->addPropertyConstraint($property, $constraint);
Chris@0 243 }
Chris@0 244
Chris@0 245 return $this;
Chris@0 246 }
Chris@0 247
Chris@0 248 /**
Chris@0 249 * Adds a constraint to the getter of the given property.
Chris@0 250 *
Chris@0 251 * The name of the getter is assumed to be the name of the property with an
Chris@0 252 * uppercased first letter and either the prefix "get" or "is".
Chris@0 253 *
Chris@0 254 * @param string $property The name of the property
Chris@0 255 * @param Constraint $constraint The constraint
Chris@0 256 *
Chris@0 257 * @return $this
Chris@0 258 */
Chris@0 259 public function addGetterConstraint($property, Constraint $constraint)
Chris@0 260 {
Chris@0 261 if (!isset($this->getters[$property])) {
Chris@0 262 $this->getters[$property] = new GetterMetadata($this->getClassName(), $property);
Chris@0 263
Chris@0 264 $this->addPropertyMetadata($this->getters[$property]);
Chris@0 265 }
Chris@0 266
Chris@0 267 $constraint->addImplicitGroupName($this->getDefaultGroup());
Chris@0 268
Chris@0 269 $this->getters[$property]->addConstraint($constraint);
Chris@0 270
Chris@0 271 return $this;
Chris@0 272 }
Chris@0 273
Chris@0 274 /**
Chris@0 275 * Adds a constraint to the getter of the given property.
Chris@0 276 *
Chris@0 277 * @param string $property The name of the property
Chris@0 278 * @param string $method The name of the getter method
Chris@0 279 * @param Constraint $constraint The constraint
Chris@0 280 *
Chris@0 281 * @return $this
Chris@0 282 */
Chris@0 283 public function addGetterMethodConstraint($property, $method, Constraint $constraint)
Chris@0 284 {
Chris@0 285 if (!isset($this->getters[$property])) {
Chris@0 286 $this->getters[$property] = new GetterMetadata($this->getClassName(), $property, $method);
Chris@0 287
Chris@0 288 $this->addPropertyMetadata($this->getters[$property]);
Chris@0 289 }
Chris@0 290
Chris@0 291 $constraint->addImplicitGroupName($this->getDefaultGroup());
Chris@0 292
Chris@0 293 $this->getters[$property]->addConstraint($constraint);
Chris@0 294
Chris@0 295 return $this;
Chris@0 296 }
Chris@0 297
Chris@0 298 /**
Chris@0 299 * @param string $property
Chris@0 300 * @param Constraint[] $constraints
Chris@0 301 *
Chris@0 302 * @return $this
Chris@0 303 */
Chris@0 304 public function addGetterConstraints($property, array $constraints)
Chris@0 305 {
Chris@0 306 foreach ($constraints as $constraint) {
Chris@0 307 $this->addGetterConstraint($property, $constraint);
Chris@0 308 }
Chris@0 309
Chris@0 310 return $this;
Chris@0 311 }
Chris@0 312
Chris@0 313 /**
Chris@0 314 * @param string $property
Chris@0 315 * @param string $method
Chris@0 316 * @param Constraint[] $constraints
Chris@0 317 *
Chris@0 318 * @return $this
Chris@0 319 */
Chris@0 320 public function addGetterMethodConstraints($property, $method, array $constraints)
Chris@0 321 {
Chris@0 322 foreach ($constraints as $constraint) {
Chris@0 323 $this->addGetterMethodConstraint($property, $method, $constraint);
Chris@0 324 }
Chris@0 325
Chris@0 326 return $this;
Chris@0 327 }
Chris@0 328
Chris@0 329 /**
Chris@0 330 * Merges the constraints of the given metadata into this object.
Chris@0 331 */
Chris@16 332 public function mergeConstraints(self $source)
Chris@0 333 {
Chris@14 334 if ($source->isGroupSequenceProvider()) {
Chris@14 335 $this->setGroupSequenceProvider(true);
Chris@14 336 }
Chris@14 337
Chris@0 338 foreach ($source->getConstraints() as $constraint) {
Chris@0 339 $this->addConstraint(clone $constraint);
Chris@0 340 }
Chris@0 341
Chris@0 342 foreach ($source->getConstrainedProperties() as $property) {
Chris@0 343 foreach ($source->getPropertyMetadata($property) as $member) {
Chris@0 344 $member = clone $member;
Chris@0 345
Chris@0 346 foreach ($member->getConstraints() as $constraint) {
Chris@17 347 if (\in_array($constraint::DEFAULT_GROUP, $constraint->groups, true)) {
Chris@0 348 $member->constraintsByGroup[$this->getDefaultGroup()][] = $constraint;
Chris@0 349 }
Chris@0 350
Chris@0 351 $constraint->addImplicitGroupName($this->getDefaultGroup());
Chris@0 352 }
Chris@0 353
Chris@0 354 $this->addPropertyMetadata($member);
Chris@0 355
Chris@0 356 if ($member instanceof MemberMetadata && !$member->isPrivate($this->name)) {
Chris@0 357 $property = $member->getPropertyName();
Chris@0 358
Chris@0 359 if ($member instanceof PropertyMetadata && !isset($this->properties[$property])) {
Chris@0 360 $this->properties[$property] = $member;
Chris@0 361 } elseif ($member instanceof GetterMetadata && !isset($this->getters[$property])) {
Chris@0 362 $this->getters[$property] = $member;
Chris@0 363 }
Chris@0 364 }
Chris@0 365 }
Chris@0 366 }
Chris@0 367 }
Chris@0 368
Chris@0 369 /**
Chris@0 370 * {@inheritdoc}
Chris@0 371 */
Chris@0 372 public function hasPropertyMetadata($property)
Chris@0 373 {
Chris@18 374 return \array_key_exists($property, $this->members);
Chris@0 375 }
Chris@0 376
Chris@0 377 /**
Chris@0 378 * {@inheritdoc}
Chris@0 379 */
Chris@0 380 public function getPropertyMetadata($property)
Chris@0 381 {
Chris@0 382 if (!isset($this->members[$property])) {
Chris@17 383 return [];
Chris@0 384 }
Chris@0 385
Chris@0 386 return $this->members[$property];
Chris@0 387 }
Chris@0 388
Chris@0 389 /**
Chris@0 390 * {@inheritdoc}
Chris@0 391 */
Chris@0 392 public function getConstrainedProperties()
Chris@0 393 {
Chris@0 394 return array_keys($this->members);
Chris@0 395 }
Chris@0 396
Chris@0 397 /**
Chris@0 398 * Sets the default group sequence for this class.
Chris@0 399 *
Chris@17 400 * @param string[]|GroupSequence $groupSequence An array of group names
Chris@0 401 *
Chris@0 402 * @return $this
Chris@0 403 *
Chris@0 404 * @throws GroupDefinitionException
Chris@0 405 */
Chris@0 406 public function setGroupSequence($groupSequence)
Chris@0 407 {
Chris@0 408 if ($this->isGroupSequenceProvider()) {
Chris@0 409 throw new GroupDefinitionException('Defining a static group sequence is not allowed with a group sequence provider');
Chris@0 410 }
Chris@0 411
Chris@17 412 if (\is_array($groupSequence)) {
Chris@0 413 $groupSequence = new GroupSequence($groupSequence);
Chris@0 414 }
Chris@0 415
Chris@17 416 if (\in_array(Constraint::DEFAULT_GROUP, $groupSequence->groups, true)) {
Chris@0 417 throw new GroupDefinitionException(sprintf('The group "%s" is not allowed in group sequences', Constraint::DEFAULT_GROUP));
Chris@0 418 }
Chris@0 419
Chris@17 420 if (!\in_array($this->getDefaultGroup(), $groupSequence->groups, true)) {
Chris@0 421 throw new GroupDefinitionException(sprintf('The group "%s" is missing in the group sequence', $this->getDefaultGroup()));
Chris@0 422 }
Chris@0 423
Chris@0 424 $this->groupSequence = $groupSequence;
Chris@0 425
Chris@0 426 return $this;
Chris@0 427 }
Chris@0 428
Chris@0 429 /**
Chris@0 430 * {@inheritdoc}
Chris@0 431 */
Chris@0 432 public function hasGroupSequence()
Chris@0 433 {
Chris@17 434 return $this->groupSequence && \count($this->groupSequence->groups) > 0;
Chris@0 435 }
Chris@0 436
Chris@0 437 /**
Chris@0 438 * {@inheritdoc}
Chris@0 439 */
Chris@0 440 public function getGroupSequence()
Chris@0 441 {
Chris@0 442 return $this->groupSequence;
Chris@0 443 }
Chris@0 444
Chris@0 445 /**
Chris@0 446 * Returns a ReflectionClass instance for this class.
Chris@0 447 *
Chris@0 448 * @return \ReflectionClass
Chris@0 449 */
Chris@0 450 public function getReflectionClass()
Chris@0 451 {
Chris@0 452 if (!$this->reflClass) {
Chris@0 453 $this->reflClass = new \ReflectionClass($this->getClassName());
Chris@0 454 }
Chris@0 455
Chris@0 456 return $this->reflClass;
Chris@0 457 }
Chris@0 458
Chris@0 459 /**
Chris@0 460 * Sets whether a group sequence provider should be used.
Chris@0 461 *
Chris@0 462 * @param bool $active
Chris@0 463 *
Chris@0 464 * @throws GroupDefinitionException
Chris@0 465 */
Chris@0 466 public function setGroupSequenceProvider($active)
Chris@0 467 {
Chris@0 468 if ($this->hasGroupSequence()) {
Chris@0 469 throw new GroupDefinitionException('Defining a group sequence provider is not allowed with a static group sequence');
Chris@0 470 }
Chris@0 471
Chris@0 472 if (!$this->getReflectionClass()->implementsInterface('Symfony\Component\Validator\GroupSequenceProviderInterface')) {
Chris@0 473 throw new GroupDefinitionException(sprintf('Class "%s" must implement GroupSequenceProviderInterface', $this->name));
Chris@0 474 }
Chris@0 475
Chris@0 476 $this->groupSequenceProvider = $active;
Chris@0 477 }
Chris@0 478
Chris@0 479 /**
Chris@0 480 * {@inheritdoc}
Chris@0 481 */
Chris@0 482 public function isGroupSequenceProvider()
Chris@0 483 {
Chris@0 484 return $this->groupSequenceProvider;
Chris@0 485 }
Chris@0 486
Chris@0 487 /**
Chris@0 488 * Class nodes are never cascaded.
Chris@0 489 *
Chris@0 490 * {@inheritdoc}
Chris@0 491 */
Chris@0 492 public function getCascadingStrategy()
Chris@0 493 {
Chris@0 494 return CascadingStrategy::NONE;
Chris@0 495 }
Chris@0 496
Chris@0 497 private function addPropertyMetadata(PropertyMetadataInterface $metadata)
Chris@0 498 {
Chris@0 499 $property = $metadata->getPropertyName();
Chris@0 500
Chris@0 501 $this->members[$property][] = $metadata;
Chris@0 502 }
Chris@0 503 }