annotate vendor/symfony/validator/Validator/RecursiveContextualValidator.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\Validator;
Chris@0 13
Chris@0 14 use Symfony\Component\Validator\Constraint;
Chris@17 15 use Symfony\Component\Validator\Constraints\Composite;
Chris@0 16 use Symfony\Component\Validator\Constraints\GroupSequence;
Chris@0 17 use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
Chris@0 18 use Symfony\Component\Validator\Context\ExecutionContext;
Chris@0 19 use Symfony\Component\Validator\Context\ExecutionContextInterface;
Chris@0 20 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
Chris@0 21 use Symfony\Component\Validator\Exception\NoSuchMetadataException;
Chris@0 22 use Symfony\Component\Validator\Exception\RuntimeException;
Chris@0 23 use Symfony\Component\Validator\Exception\UnsupportedMetadataException;
Chris@0 24 use Symfony\Component\Validator\Exception\ValidatorException;
Chris@0 25 use Symfony\Component\Validator\Mapping\CascadingStrategy;
Chris@0 26 use Symfony\Component\Validator\Mapping\ClassMetadataInterface;
Chris@17 27 use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
Chris@0 28 use Symfony\Component\Validator\Mapping\GenericMetadata;
Chris@0 29 use Symfony\Component\Validator\Mapping\MetadataInterface;
Chris@0 30 use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
Chris@0 31 use Symfony\Component\Validator\Mapping\TraversalStrategy;
Chris@0 32 use Symfony\Component\Validator\ObjectInitializerInterface;
Chris@0 33 use Symfony\Component\Validator\Util\PropertyPath;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * Recursive implementation of {@link ContextualValidatorInterface}.
Chris@0 37 *
Chris@0 38 * @author Bernhard Schussek <bschussek@gmail.com>
Chris@0 39 */
Chris@0 40 class RecursiveContextualValidator implements ContextualValidatorInterface
Chris@0 41 {
Chris@0 42 private $context;
Chris@0 43 private $defaultPropertyPath;
Chris@0 44 private $defaultGroups;
Chris@0 45 private $metadataFactory;
Chris@0 46 private $validatorFactory;
Chris@0 47 private $objectInitializers;
Chris@0 48
Chris@0 49 /**
Chris@0 50 * Creates a validator for the given context.
Chris@0 51 *
Chris@0 52 * @param ExecutionContextInterface $context The execution context
Chris@0 53 * @param MetadataFactoryInterface $metadataFactory The factory for
Chris@0 54 * fetching the metadata
Chris@0 55 * of validated objects
Chris@0 56 * @param ConstraintValidatorFactoryInterface $validatorFactory The factory for creating
Chris@0 57 * constraint validators
Chris@0 58 * @param ObjectInitializerInterface[] $objectInitializers The object initializers
Chris@0 59 */
Chris@17 60 public function __construct(ExecutionContextInterface $context, MetadataFactoryInterface $metadataFactory, ConstraintValidatorFactoryInterface $validatorFactory, array $objectInitializers = [])
Chris@0 61 {
Chris@0 62 $this->context = $context;
Chris@0 63 $this->defaultPropertyPath = $context->getPropertyPath();
Chris@17 64 $this->defaultGroups = [$context->getGroup() ?: Constraint::DEFAULT_GROUP];
Chris@0 65 $this->metadataFactory = $metadataFactory;
Chris@0 66 $this->validatorFactory = $validatorFactory;
Chris@0 67 $this->objectInitializers = $objectInitializers;
Chris@0 68 }
Chris@0 69
Chris@0 70 /**
Chris@0 71 * {@inheritdoc}
Chris@0 72 */
Chris@0 73 public function atPath($path)
Chris@0 74 {
Chris@0 75 $this->defaultPropertyPath = $this->context->getPropertyPath($path);
Chris@0 76
Chris@0 77 return $this;
Chris@0 78 }
Chris@0 79
Chris@0 80 /**
Chris@0 81 * {@inheritdoc}
Chris@0 82 */
Chris@0 83 public function validate($value, $constraints = null, $groups = null)
Chris@0 84 {
Chris@0 85 $groups = $groups ? $this->normalizeGroups($groups) : $this->defaultGroups;
Chris@0 86
Chris@0 87 $previousValue = $this->context->getValue();
Chris@0 88 $previousObject = $this->context->getObject();
Chris@0 89 $previousMetadata = $this->context->getMetadata();
Chris@0 90 $previousPath = $this->context->getPropertyPath();
Chris@0 91 $previousGroup = $this->context->getGroup();
Chris@0 92 $previousConstraint = null;
Chris@0 93
Chris@0 94 if ($this->context instanceof ExecutionContext || method_exists($this->context, 'getConstraint')) {
Chris@0 95 $previousConstraint = $this->context->getConstraint();
Chris@0 96 }
Chris@0 97
Chris@0 98 // If explicit constraints are passed, validate the value against
Chris@0 99 // those constraints
Chris@0 100 if (null !== $constraints) {
Chris@0 101 // You can pass a single constraint or an array of constraints
Chris@0 102 // Make sure to deal with an array in the rest of the code
Chris@17 103 if (!\is_array($constraints)) {
Chris@17 104 $constraints = [$constraints];
Chris@0 105 }
Chris@0 106
Chris@0 107 $metadata = new GenericMetadata();
Chris@0 108 $metadata->addConstraints($constraints);
Chris@0 109
Chris@0 110 $this->validateGenericNode(
Chris@0 111 $value,
Chris@14 112 $previousObject,
Chris@17 113 \is_object($value) ? spl_object_hash($value) : null,
Chris@0 114 $metadata,
Chris@0 115 $this->defaultPropertyPath,
Chris@0 116 $groups,
Chris@0 117 null,
Chris@0 118 TraversalStrategy::IMPLICIT,
Chris@0 119 $this->context
Chris@0 120 );
Chris@0 121
Chris@0 122 $this->context->setNode($previousValue, $previousObject, $previousMetadata, $previousPath);
Chris@0 123 $this->context->setGroup($previousGroup);
Chris@0 124
Chris@0 125 if (null !== $previousConstraint) {
Chris@0 126 $this->context->setConstraint($previousConstraint);
Chris@0 127 }
Chris@0 128
Chris@0 129 return $this;
Chris@0 130 }
Chris@0 131
Chris@0 132 // If an object is passed without explicit constraints, validate that
Chris@0 133 // object against the constraints defined for the object's class
Chris@17 134 if (\is_object($value)) {
Chris@0 135 $this->validateObject(
Chris@0 136 $value,
Chris@0 137 $this->defaultPropertyPath,
Chris@0 138 $groups,
Chris@0 139 TraversalStrategy::IMPLICIT,
Chris@0 140 $this->context
Chris@0 141 );
Chris@0 142
Chris@0 143 $this->context->setNode($previousValue, $previousObject, $previousMetadata, $previousPath);
Chris@0 144 $this->context->setGroup($previousGroup);
Chris@0 145
Chris@0 146 return $this;
Chris@0 147 }
Chris@0 148
Chris@0 149 // If an array is passed without explicit constraints, validate each
Chris@0 150 // object in the array
Chris@17 151 if (\is_array($value)) {
Chris@0 152 $this->validateEachObjectIn(
Chris@0 153 $value,
Chris@0 154 $this->defaultPropertyPath,
Chris@0 155 $groups,
Chris@0 156 $this->context
Chris@0 157 );
Chris@0 158
Chris@0 159 $this->context->setNode($previousValue, $previousObject, $previousMetadata, $previousPath);
Chris@0 160 $this->context->setGroup($previousGroup);
Chris@0 161
Chris@0 162 return $this;
Chris@0 163 }
Chris@0 164
Chris@17 165 throw new RuntimeException(sprintf('Cannot validate values of type "%s" automatically. Please provide a constraint.', \gettype($value)));
Chris@0 166 }
Chris@0 167
Chris@0 168 /**
Chris@0 169 * {@inheritdoc}
Chris@0 170 */
Chris@0 171 public function validateProperty($object, $propertyName, $groups = null)
Chris@0 172 {
Chris@0 173 $classMetadata = $this->metadataFactory->getMetadataFor($object);
Chris@0 174
Chris@0 175 if (!$classMetadata instanceof ClassMetadataInterface) {
Chris@17 176 throw new ValidatorException(sprintf('The metadata factory should return instances of "\Symfony\Component\Validator\Mapping\ClassMetadataInterface", got: "%s".', \is_object($classMetadata) ? \get_class($classMetadata) : \gettype($classMetadata)));
Chris@0 177 }
Chris@0 178
Chris@0 179 $propertyMetadatas = $classMetadata->getPropertyMetadata($propertyName);
Chris@0 180 $groups = $groups ? $this->normalizeGroups($groups) : $this->defaultGroups;
Chris@0 181 $cacheKey = spl_object_hash($object);
Chris@0 182 $propertyPath = PropertyPath::append($this->defaultPropertyPath, $propertyName);
Chris@0 183
Chris@0 184 $previousValue = $this->context->getValue();
Chris@0 185 $previousObject = $this->context->getObject();
Chris@0 186 $previousMetadata = $this->context->getMetadata();
Chris@0 187 $previousPath = $this->context->getPropertyPath();
Chris@0 188 $previousGroup = $this->context->getGroup();
Chris@0 189
Chris@0 190 foreach ($propertyMetadatas as $propertyMetadata) {
Chris@0 191 $propertyValue = $propertyMetadata->getPropertyValue($object);
Chris@0 192
Chris@0 193 $this->validateGenericNode(
Chris@0 194 $propertyValue,
Chris@0 195 $object,
Chris@17 196 $cacheKey.':'.\get_class($object).':'.$propertyName,
Chris@0 197 $propertyMetadata,
Chris@0 198 $propertyPath,
Chris@0 199 $groups,
Chris@0 200 null,
Chris@0 201 TraversalStrategy::IMPLICIT,
Chris@0 202 $this->context
Chris@0 203 );
Chris@0 204 }
Chris@0 205
Chris@0 206 $this->context->setNode($previousValue, $previousObject, $previousMetadata, $previousPath);
Chris@0 207 $this->context->setGroup($previousGroup);
Chris@0 208
Chris@0 209 return $this;
Chris@0 210 }
Chris@0 211
Chris@0 212 /**
Chris@0 213 * {@inheritdoc}
Chris@0 214 */
Chris@0 215 public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null)
Chris@0 216 {
Chris@0 217 $classMetadata = $this->metadataFactory->getMetadataFor($objectOrClass);
Chris@0 218
Chris@0 219 if (!$classMetadata instanceof ClassMetadataInterface) {
Chris@17 220 throw new ValidatorException(sprintf('The metadata factory should return instances of "\Symfony\Component\Validator\Mapping\ClassMetadataInterface", got: "%s".', \is_object($classMetadata) ? \get_class($classMetadata) : \gettype($classMetadata)));
Chris@0 221 }
Chris@0 222
Chris@0 223 $propertyMetadatas = $classMetadata->getPropertyMetadata($propertyName);
Chris@0 224 $groups = $groups ? $this->normalizeGroups($groups) : $this->defaultGroups;
Chris@0 225
Chris@17 226 if (\is_object($objectOrClass)) {
Chris@0 227 $object = $objectOrClass;
Chris@17 228 $class = \get_class($object);
Chris@0 229 $cacheKey = spl_object_hash($objectOrClass);
Chris@0 230 $propertyPath = PropertyPath::append($this->defaultPropertyPath, $propertyName);
Chris@0 231 } else {
Chris@0 232 // $objectOrClass contains a class name
Chris@0 233 $object = null;
Chris@14 234 $class = $objectOrClass;
Chris@0 235 $cacheKey = null;
Chris@0 236 $propertyPath = $this->defaultPropertyPath;
Chris@0 237 }
Chris@0 238
Chris@0 239 $previousValue = $this->context->getValue();
Chris@0 240 $previousObject = $this->context->getObject();
Chris@0 241 $previousMetadata = $this->context->getMetadata();
Chris@0 242 $previousPath = $this->context->getPropertyPath();
Chris@0 243 $previousGroup = $this->context->getGroup();
Chris@0 244
Chris@0 245 foreach ($propertyMetadatas as $propertyMetadata) {
Chris@0 246 $this->validateGenericNode(
Chris@0 247 $value,
Chris@0 248 $object,
Chris@14 249 $cacheKey.':'.$class.':'.$propertyName,
Chris@0 250 $propertyMetadata,
Chris@0 251 $propertyPath,
Chris@0 252 $groups,
Chris@0 253 null,
Chris@0 254 TraversalStrategy::IMPLICIT,
Chris@0 255 $this->context
Chris@0 256 );
Chris@0 257 }
Chris@0 258
Chris@0 259 $this->context->setNode($previousValue, $previousObject, $previousMetadata, $previousPath);
Chris@0 260 $this->context->setGroup($previousGroup);
Chris@0 261
Chris@0 262 return $this;
Chris@0 263 }
Chris@0 264
Chris@0 265 /**
Chris@0 266 * {@inheritdoc}
Chris@0 267 */
Chris@0 268 public function getViolations()
Chris@0 269 {
Chris@0 270 return $this->context->getViolations();
Chris@0 271 }
Chris@0 272
Chris@0 273 /**
Chris@0 274 * Normalizes the given group or list of groups to an array.
Chris@0 275 *
Chris@17 276 * @param string|GroupSequence|(string|GroupSequence)[] $groups The groups to normalize
Chris@0 277 *
Chris@17 278 * @return (string|GroupSequence)[] A group array
Chris@0 279 */
Chris@0 280 protected function normalizeGroups($groups)
Chris@0 281 {
Chris@17 282 if (\is_array($groups)) {
Chris@0 283 return $groups;
Chris@0 284 }
Chris@0 285
Chris@17 286 return [$groups];
Chris@0 287 }
Chris@0 288
Chris@0 289 /**
Chris@0 290 * Validates an object against the constraints defined for its class.
Chris@0 291 *
Chris@0 292 * If no metadata is available for the class, but the class is an instance
Chris@0 293 * of {@link \Traversable} and the selected traversal strategy allows
Chris@0 294 * traversal, the object will be iterated and each nested object will be
Chris@0 295 * validated instead.
Chris@0 296 *
Chris@0 297 * @param object $object The object to cascade
Chris@0 298 * @param string $propertyPath The current property path
Chris@17 299 * @param (string|GroupSequence)[] $groups The validated groups
Chris@0 300 * @param int $traversalStrategy The strategy for traversing the
Chris@0 301 * cascaded object
Chris@0 302 * @param ExecutionContextInterface $context The current execution context
Chris@0 303 *
Chris@0 304 * @throws NoSuchMetadataException If the object has no associated metadata
Chris@0 305 * and does not implement {@link \Traversable}
Chris@0 306 * or if traversal is disabled via the
Chris@0 307 * $traversalStrategy argument
Chris@0 308 * @throws UnsupportedMetadataException If the metadata returned by the
Chris@0 309 * metadata factory does not implement
Chris@0 310 * {@link ClassMetadataInterface}
Chris@0 311 */
Chris@0 312 private function validateObject($object, $propertyPath, array $groups, $traversalStrategy, ExecutionContextInterface $context)
Chris@0 313 {
Chris@0 314 try {
Chris@0 315 $classMetadata = $this->metadataFactory->getMetadataFor($object);
Chris@0 316
Chris@0 317 if (!$classMetadata instanceof ClassMetadataInterface) {
Chris@17 318 throw new UnsupportedMetadataException(sprintf('The metadata factory should return instances of "Symfony\Component\Validator\Mapping\ClassMetadataInterface", got: "%s".', \is_object($classMetadata) ? \get_class($classMetadata) : \gettype($classMetadata)));
Chris@0 319 }
Chris@0 320
Chris@0 321 $this->validateClassNode(
Chris@0 322 $object,
Chris@0 323 spl_object_hash($object),
Chris@0 324 $classMetadata,
Chris@0 325 $propertyPath,
Chris@0 326 $groups,
Chris@0 327 null,
Chris@0 328 $traversalStrategy,
Chris@0 329 $context
Chris@0 330 );
Chris@0 331 } catch (NoSuchMetadataException $e) {
Chris@0 332 // Rethrow if not Traversable
Chris@0 333 if (!$object instanceof \Traversable) {
Chris@0 334 throw $e;
Chris@0 335 }
Chris@0 336
Chris@0 337 // Rethrow unless IMPLICIT or TRAVERSE
Chris@0 338 if (!($traversalStrategy & (TraversalStrategy::IMPLICIT | TraversalStrategy::TRAVERSE))) {
Chris@0 339 throw $e;
Chris@0 340 }
Chris@0 341
Chris@0 342 $this->validateEachObjectIn(
Chris@0 343 $object,
Chris@0 344 $propertyPath,
Chris@0 345 $groups,
Chris@0 346 $context
Chris@0 347 );
Chris@0 348 }
Chris@0 349 }
Chris@0 350
Chris@0 351 /**
Chris@0 352 * Validates each object in a collection against the constraints defined
Chris@0 353 * for their classes.
Chris@0 354 *
Chris@18 355 * Nested arrays are also iterated.
Chris@0 356 *
Chris@14 357 * @param iterable $collection The collection
Chris@0 358 * @param string $propertyPath The current property path
Chris@17 359 * @param (string|GroupSequence)[] $groups The validated groups
Chris@0 360 * @param ExecutionContextInterface $context The current execution context
Chris@0 361 */
Chris@0 362 private function validateEachObjectIn($collection, $propertyPath, array $groups, ExecutionContextInterface $context)
Chris@0 363 {
Chris@0 364 foreach ($collection as $key => $value) {
Chris@17 365 if (\is_array($value)) {
Chris@18 366 // Also traverse nested arrays
Chris@0 367 $this->validateEachObjectIn(
Chris@0 368 $value,
Chris@0 369 $propertyPath.'['.$key.']',
Chris@0 370 $groups,
Chris@0 371 $context
Chris@0 372 );
Chris@0 373
Chris@0 374 continue;
Chris@0 375 }
Chris@0 376
Chris@0 377 // Scalar and null values in the collection are ignored
Chris@17 378 if (\is_object($value)) {
Chris@0 379 $this->validateObject(
Chris@0 380 $value,
Chris@0 381 $propertyPath.'['.$key.']',
Chris@0 382 $groups,
Chris@0 383 TraversalStrategy::IMPLICIT,
Chris@0 384 $context
Chris@0 385 );
Chris@0 386 }
Chris@0 387 }
Chris@0 388 }
Chris@0 389
Chris@0 390 /**
Chris@0 391 * Validates a class node.
Chris@0 392 *
Chris@0 393 * A class node is a combination of an object with a {@link ClassMetadataInterface}
Chris@0 394 * instance. Each class node (conceptionally) has zero or more succeeding
Chris@0 395 * property nodes:
Chris@0 396 *
Chris@0 397 * (Article:class node)
Chris@0 398 * \
Chris@0 399 * ($title:property node)
Chris@0 400 *
Chris@0 401 * This method validates the passed objects against all constraints defined
Chris@0 402 * at class level. It furthermore triggers the validation of each of the
Chris@0 403 * class' properties against the constraints for that property.
Chris@0 404 *
Chris@0 405 * If the selected traversal strategy allows traversal, the object is
Chris@0 406 * iterated and each nested object is validated against its own constraints.
Chris@0 407 * The object is not traversed if traversal is disabled in the class
Chris@0 408 * metadata.
Chris@0 409 *
Chris@0 410 * If the passed groups contain the group "Default", the validator will
Chris@0 411 * check whether the "Default" group has been replaced by a group sequence
Chris@0 412 * in the class metadata. If this is the case, the group sequence is
Chris@0 413 * validated instead.
Chris@0 414 *
Chris@0 415 * @param object $object The validated object
Chris@0 416 * @param string $cacheKey The key for caching
Chris@0 417 * the validated object
Chris@0 418 * @param ClassMetadataInterface $metadata The class metadata of
Chris@0 419 * the object
Chris@0 420 * @param string $propertyPath The property path leading
Chris@0 421 * to the object
Chris@17 422 * @param (string|GroupSequence)[] $groups The groups in which the
Chris@0 423 * object should be validated
Chris@0 424 * @param string[]|null $cascadedGroups The groups in which
Chris@0 425 * cascaded objects should
Chris@0 426 * be validated
Chris@0 427 * @param int $traversalStrategy The strategy used for
Chris@0 428 * traversing the object
Chris@0 429 * @param ExecutionContextInterface $context The current execution context
Chris@0 430 *
Chris@0 431 * @throws UnsupportedMetadataException If a property metadata does not
Chris@0 432 * implement {@link PropertyMetadataInterface}
Chris@0 433 * @throws ConstraintDefinitionException If traversal was enabled but the
Chris@0 434 * object does not implement
Chris@0 435 * {@link \Traversable}
Chris@0 436 *
Chris@0 437 * @see TraversalStrategy
Chris@0 438 */
Chris@0 439 private function validateClassNode($object, $cacheKey, ClassMetadataInterface $metadata = null, $propertyPath, array $groups, $cascadedGroups, $traversalStrategy, ExecutionContextInterface $context)
Chris@0 440 {
Chris@0 441 $context->setNode($object, $object, $metadata, $propertyPath);
Chris@0 442
Chris@0 443 if (!$context->isObjectInitialized($cacheKey)) {
Chris@0 444 foreach ($this->objectInitializers as $initializer) {
Chris@0 445 $initializer->initialize($object);
Chris@0 446 }
Chris@0 447
Chris@0 448 $context->markObjectAsInitialized($cacheKey);
Chris@0 449 }
Chris@0 450
Chris@0 451 foreach ($groups as $key => $group) {
Chris@0 452 // If the "Default" group is replaced by a group sequence, remember
Chris@0 453 // to cascade the "Default" group when traversing the group
Chris@0 454 // sequence
Chris@0 455 $defaultOverridden = false;
Chris@0 456
Chris@0 457 // Use the object hash for group sequences
Chris@17 458 $groupHash = \is_object($group) ? spl_object_hash($group) : $group;
Chris@0 459
Chris@0 460 if ($context->isGroupValidated($cacheKey, $groupHash)) {
Chris@0 461 // Skip this group when validating the properties and when
Chris@0 462 // traversing the object
Chris@0 463 unset($groups[$key]);
Chris@0 464
Chris@0 465 continue;
Chris@0 466 }
Chris@0 467
Chris@0 468 $context->markGroupAsValidated($cacheKey, $groupHash);
Chris@0 469
Chris@0 470 // Replace the "Default" group by the group sequence defined
Chris@0 471 // for the class, if applicable.
Chris@0 472 // This is done after checking the cache, so that
Chris@0 473 // spl_object_hash() isn't called for this sequence and
Chris@0 474 // "Default" is used instead in the cache. This is useful
Chris@0 475 // if the getters below return different group sequences in
Chris@0 476 // every call.
Chris@0 477 if (Constraint::DEFAULT_GROUP === $group) {
Chris@0 478 if ($metadata->hasGroupSequence()) {
Chris@0 479 // The group sequence is statically defined for the class
Chris@0 480 $group = $metadata->getGroupSequence();
Chris@0 481 $defaultOverridden = true;
Chris@0 482 } elseif ($metadata->isGroupSequenceProvider()) {
Chris@0 483 // The group sequence is dynamically obtained from the validated
Chris@0 484 // object
Chris@0 485 /* @var \Symfony\Component\Validator\GroupSequenceProviderInterface $object */
Chris@0 486 $group = $object->getGroupSequence();
Chris@0 487 $defaultOverridden = true;
Chris@0 488
Chris@0 489 if (!$group instanceof GroupSequence) {
Chris@0 490 $group = new GroupSequence($group);
Chris@0 491 }
Chris@0 492 }
Chris@0 493 }
Chris@0 494
Chris@0 495 // If the groups (=[<G1,G2>,G3,G4]) contain a group sequence
Chris@0 496 // (=<G1,G2>), then call validateClassNode() with each entry of the
Chris@0 497 // group sequence and abort if necessary (G1, G2)
Chris@0 498 if ($group instanceof GroupSequence) {
Chris@0 499 $this->stepThroughGroupSequence(
Chris@0 500 $object,
Chris@0 501 $object,
Chris@0 502 $cacheKey,
Chris@0 503 $metadata,
Chris@0 504 $propertyPath,
Chris@0 505 $traversalStrategy,
Chris@0 506 $group,
Chris@0 507 $defaultOverridden ? Constraint::DEFAULT_GROUP : null,
Chris@0 508 $context
Chris@0 509 );
Chris@0 510
Chris@0 511 // Skip the group sequence when validating properties, because
Chris@0 512 // stepThroughGroupSequence() already validates the properties
Chris@0 513 unset($groups[$key]);
Chris@0 514
Chris@0 515 continue;
Chris@0 516 }
Chris@0 517
Chris@0 518 $this->validateInGroup($object, $cacheKey, $metadata, $group, $context);
Chris@0 519 }
Chris@0 520
Chris@0 521 // If no more groups should be validated for the property nodes,
Chris@0 522 // we can safely quit
Chris@17 523 if (0 === \count($groups)) {
Chris@0 524 return;
Chris@0 525 }
Chris@0 526
Chris@0 527 // Validate all properties against their constraints
Chris@0 528 foreach ($metadata->getConstrainedProperties() as $propertyName) {
Chris@0 529 // If constraints are defined both on the getter of a property as
Chris@0 530 // well as on the property itself, then getPropertyMetadata()
Chris@0 531 // returns two metadata objects, not just one
Chris@0 532 foreach ($metadata->getPropertyMetadata($propertyName) as $propertyMetadata) {
Chris@0 533 if (!$propertyMetadata instanceof PropertyMetadataInterface) {
Chris@17 534 throw new UnsupportedMetadataException(sprintf('The property metadata instances should implement "Symfony\Component\Validator\Mapping\PropertyMetadataInterface", got: "%s".', \is_object($propertyMetadata) ? \get_class($propertyMetadata) : \gettype($propertyMetadata)));
Chris@0 535 }
Chris@0 536
Chris@0 537 $propertyValue = $propertyMetadata->getPropertyValue($object);
Chris@0 538
Chris@0 539 $this->validateGenericNode(
Chris@0 540 $propertyValue,
Chris@0 541 $object,
Chris@17 542 $cacheKey.':'.\get_class($object).':'.$propertyName,
Chris@0 543 $propertyMetadata,
Chris@0 544 PropertyPath::append($propertyPath, $propertyName),
Chris@0 545 $groups,
Chris@0 546 $cascadedGroups,
Chris@0 547 TraversalStrategy::IMPLICIT,
Chris@0 548 $context
Chris@0 549 );
Chris@0 550 }
Chris@0 551 }
Chris@0 552
Chris@0 553 // If no specific traversal strategy was requested when this method
Chris@0 554 // was called, use the traversal strategy of the class' metadata
Chris@0 555 if ($traversalStrategy & TraversalStrategy::IMPLICIT) {
Chris@0 556 $traversalStrategy = $metadata->getTraversalStrategy();
Chris@0 557 }
Chris@0 558
Chris@0 559 // Traverse only if IMPLICIT or TRAVERSE
Chris@0 560 if (!($traversalStrategy & (TraversalStrategy::IMPLICIT | TraversalStrategy::TRAVERSE))) {
Chris@0 561 return;
Chris@0 562 }
Chris@0 563
Chris@0 564 // If IMPLICIT, stop unless we deal with a Traversable
Chris@0 565 if ($traversalStrategy & TraversalStrategy::IMPLICIT && !$object instanceof \Traversable) {
Chris@0 566 return;
Chris@0 567 }
Chris@0 568
Chris@0 569 // If TRAVERSE, fail if we have no Traversable
Chris@0 570 if (!$object instanceof \Traversable) {
Chris@17 571 throw new ConstraintDefinitionException(sprintf('Traversal was enabled for "%s", but this class does not implement "\Traversable".', \get_class($object)));
Chris@0 572 }
Chris@0 573
Chris@0 574 $this->validateEachObjectIn(
Chris@0 575 $object,
Chris@0 576 $propertyPath,
Chris@0 577 $groups,
Chris@0 578 $context
Chris@0 579 );
Chris@0 580 }
Chris@0 581
Chris@0 582 /**
Chris@0 583 * Validates a node that is not a class node.
Chris@0 584 *
Chris@0 585 * Currently, two such node types exist:
Chris@0 586 *
Chris@0 587 * - property nodes, which consist of the value of an object's
Chris@0 588 * property together with a {@link PropertyMetadataInterface} instance
Chris@0 589 * - generic nodes, which consist of a value and some arbitrary
Chris@0 590 * constraints defined in a {@link MetadataInterface} container
Chris@0 591 *
Chris@0 592 * In both cases, the value is validated against all constraints defined
Chris@0 593 * in the passed metadata object. Then, if the value is an instance of
Chris@0 594 * {@link \Traversable} and the selected traversal strategy permits it,
Chris@0 595 * the value is traversed and each nested object validated against its own
Chris@18 596 * constraints. If the value is an array, it is traversed regardless of
Chris@18 597 * the given strategy.
Chris@0 598 *
Chris@0 599 * @param mixed $value The validated value
Chris@0 600 * @param object|null $object The current object
Chris@0 601 * @param string $cacheKey The key for caching
Chris@0 602 * the validated value
Chris@0 603 * @param MetadataInterface $metadata The metadata of the
Chris@0 604 * value
Chris@0 605 * @param string $propertyPath The property path leading
Chris@0 606 * to the value
Chris@17 607 * @param (string|GroupSequence)[] $groups The groups in which the
Chris@0 608 * value should be validated
Chris@0 609 * @param string[]|null $cascadedGroups The groups in which
Chris@0 610 * cascaded objects should
Chris@0 611 * be validated
Chris@0 612 * @param int $traversalStrategy The strategy used for
Chris@0 613 * traversing the value
Chris@0 614 * @param ExecutionContextInterface $context The current execution context
Chris@0 615 *
Chris@0 616 * @see TraversalStrategy
Chris@0 617 */
Chris@0 618 private function validateGenericNode($value, $object, $cacheKey, MetadataInterface $metadata = null, $propertyPath, array $groups, $cascadedGroups, $traversalStrategy, ExecutionContextInterface $context)
Chris@0 619 {
Chris@0 620 $context->setNode($value, $object, $metadata, $propertyPath);
Chris@0 621
Chris@0 622 foreach ($groups as $key => $group) {
Chris@0 623 if ($group instanceof GroupSequence) {
Chris@0 624 $this->stepThroughGroupSequence(
Chris@0 625 $value,
Chris@0 626 $object,
Chris@0 627 $cacheKey,
Chris@0 628 $metadata,
Chris@0 629 $propertyPath,
Chris@0 630 $traversalStrategy,
Chris@0 631 $group,
Chris@0 632 null,
Chris@0 633 $context
Chris@0 634 );
Chris@0 635
Chris@0 636 // Skip the group sequence when cascading, as the cascading
Chris@0 637 // logic is already done in stepThroughGroupSequence()
Chris@0 638 unset($groups[$key]);
Chris@0 639
Chris@0 640 continue;
Chris@0 641 }
Chris@0 642
Chris@0 643 $this->validateInGroup($value, $cacheKey, $metadata, $group, $context);
Chris@0 644 }
Chris@0 645
Chris@17 646 if (0 === \count($groups)) {
Chris@0 647 return;
Chris@0 648 }
Chris@0 649
Chris@0 650 if (null === $value) {
Chris@0 651 return;
Chris@0 652 }
Chris@0 653
Chris@0 654 $cascadingStrategy = $metadata->getCascadingStrategy();
Chris@0 655
Chris@18 656 // Quit unless we cascade
Chris@18 657 if (!($cascadingStrategy & CascadingStrategy::CASCADE)) {
Chris@0 658 return;
Chris@0 659 }
Chris@0 660
Chris@0 661 // If no specific traversal strategy was requested when this method
Chris@0 662 // was called, use the traversal strategy of the node's metadata
Chris@0 663 if ($traversalStrategy & TraversalStrategy::IMPLICIT) {
Chris@0 664 $traversalStrategy = $metadata->getTraversalStrategy();
Chris@0 665 }
Chris@0 666
Chris@0 667 // The $cascadedGroups property is set, if the "Default" group is
Chris@0 668 // overridden by a group sequence
Chris@0 669 // See validateClassNode()
Chris@17 670 $cascadedGroups = null !== $cascadedGroups && \count($cascadedGroups) > 0 ? $cascadedGroups : $groups;
Chris@0 671
Chris@17 672 if (\is_array($value)) {
Chris@0 673 // Arrays are always traversed, independent of the specified
Chris@0 674 // traversal strategy
Chris@0 675 $this->validateEachObjectIn(
Chris@0 676 $value,
Chris@0 677 $propertyPath,
Chris@0 678 $cascadedGroups,
Chris@0 679 $context
Chris@0 680 );
Chris@0 681
Chris@0 682 return;
Chris@0 683 }
Chris@0 684
Chris@0 685 // If the value is a scalar, pass it anyway, because we want
Chris@0 686 // a NoSuchMetadataException to be thrown in that case
Chris@0 687 $this->validateObject(
Chris@0 688 $value,
Chris@0 689 $propertyPath,
Chris@0 690 $cascadedGroups,
Chris@0 691 $traversalStrategy,
Chris@0 692 $context
Chris@0 693 );
Chris@0 694
Chris@0 695 // Currently, the traversal strategy can only be TRAVERSE for a
Chris@0 696 // generic node if the cascading strategy is CASCADE. Thus, traversable
Chris@0 697 // objects will always be handled within validateObject() and there's
Chris@0 698 // nothing more to do here.
Chris@0 699
Chris@0 700 // see GenericMetadata::addConstraint()
Chris@0 701 }
Chris@0 702
Chris@0 703 /**
Chris@0 704 * Sequentially validates a node's value in each group of a group sequence.
Chris@0 705 *
Chris@0 706 * If any of the constraints generates a violation, subsequent groups in the
Chris@0 707 * group sequence are skipped.
Chris@0 708 *
Chris@0 709 * @param mixed $value The validated value
Chris@0 710 * @param object|null $object The current object
Chris@0 711 * @param string $cacheKey The key for caching
Chris@0 712 * the validated value
Chris@0 713 * @param MetadataInterface $metadata The metadata of the
Chris@0 714 * value
Chris@0 715 * @param string $propertyPath The property path leading
Chris@0 716 * to the value
Chris@0 717 * @param int $traversalStrategy The strategy used for
Chris@0 718 * traversing the value
Chris@0 719 * @param GroupSequence $groupSequence The group sequence
Chris@0 720 * @param string|null $cascadedGroup The group that should
Chris@0 721 * be passed to cascaded
Chris@0 722 * objects instead of
Chris@0 723 * the group sequence
Chris@0 724 * @param ExecutionContextInterface $context The execution context
Chris@0 725 */
Chris@0 726 private function stepThroughGroupSequence($value, $object, $cacheKey, MetadataInterface $metadata = null, $propertyPath, $traversalStrategy, GroupSequence $groupSequence, $cascadedGroup, ExecutionContextInterface $context)
Chris@0 727 {
Chris@17 728 $violationCount = \count($context->getViolations());
Chris@17 729 $cascadedGroups = $cascadedGroup ? [$cascadedGroup] : null;
Chris@0 730
Chris@0 731 foreach ($groupSequence->groups as $groupInSequence) {
Chris@0 732 $groups = (array) $groupInSequence;
Chris@0 733
Chris@0 734 if ($metadata instanceof ClassMetadataInterface) {
Chris@0 735 $this->validateClassNode(
Chris@0 736 $value,
Chris@0 737 $cacheKey,
Chris@0 738 $metadata,
Chris@0 739 $propertyPath,
Chris@0 740 $groups,
Chris@0 741 $cascadedGroups,
Chris@0 742 $traversalStrategy,
Chris@0 743 $context
Chris@0 744 );
Chris@0 745 } else {
Chris@0 746 $this->validateGenericNode(
Chris@0 747 $value,
Chris@0 748 $object,
Chris@0 749 $cacheKey,
Chris@0 750 $metadata,
Chris@0 751 $propertyPath,
Chris@0 752 $groups,
Chris@0 753 $cascadedGroups,
Chris@0 754 $traversalStrategy,
Chris@0 755 $context
Chris@0 756 );
Chris@0 757 }
Chris@0 758
Chris@0 759 // Abort sequence validation if a violation was generated
Chris@17 760 if (\count($context->getViolations()) > $violationCount) {
Chris@0 761 break;
Chris@0 762 }
Chris@0 763 }
Chris@0 764 }
Chris@0 765
Chris@0 766 /**
Chris@0 767 * Validates a node's value against all constraints in the given group.
Chris@0 768 *
Chris@0 769 * @param mixed $value The validated value
Chris@0 770 * @param string $cacheKey The key for caching the
Chris@0 771 * validated value
Chris@0 772 * @param MetadataInterface $metadata The metadata of the value
Chris@0 773 * @param string $group The group to validate
Chris@0 774 * @param ExecutionContextInterface $context The execution context
Chris@0 775 */
Chris@0 776 private function validateInGroup($value, $cacheKey, MetadataInterface $metadata, $group, ExecutionContextInterface $context)
Chris@0 777 {
Chris@0 778 $context->setGroup($group);
Chris@0 779
Chris@0 780 foreach ($metadata->findConstraints($group) as $constraint) {
Chris@0 781 // Prevent duplicate validation of constraints, in the case
Chris@0 782 // that constraints belong to multiple validated groups
Chris@0 783 if (null !== $cacheKey) {
Chris@0 784 $constraintHash = spl_object_hash($constraint);
Chris@0 785
Chris@17 786 if ($constraint instanceof Composite) {
Chris@17 787 $constraintHash .= $group;
Chris@17 788 }
Chris@17 789
Chris@0 790 if ($context->isConstraintValidated($cacheKey, $constraintHash)) {
Chris@0 791 continue;
Chris@0 792 }
Chris@0 793
Chris@0 794 $context->markConstraintAsValidated($cacheKey, $constraintHash);
Chris@0 795 }
Chris@0 796
Chris@0 797 $context->setConstraint($constraint);
Chris@0 798
Chris@0 799 $validator = $this->validatorFactory->getInstance($constraint);
Chris@0 800 $validator->initialize($context);
Chris@0 801 $validator->validate($value, $constraint);
Chris@0 802 }
Chris@0 803 }
Chris@0 804 }