vendor/symfony/validator/Constraints/Composite.php line 52

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  13. /**
  14.  * A constraint that is composed of other constraints.
  15.  *
  16.  * You should never use the nested constraint instances anywhere else, because
  17.  * their groups are adapted when passed to the constructor of this class.
  18.  *
  19.  * If you want to create your own composite constraint, extend this class and
  20.  * let {@link getCompositeOption()} return the name of the property which
  21.  * contains the nested constraints.
  22.  *
  23.  * @author Bernhard Schussek <bschussek@gmail.com>
  24.  */
  25. abstract class Composite extends Constraint
  26. {
  27.     /**
  28.      * The groups of the composite and its nested constraints are made
  29.      * consistent using the following strategy:
  30.      *
  31.      *   - If groups are passed explicitly to the composite constraint, but
  32.      *     not to the nested constraints, the options of the composite
  33.      *     constraint are copied to the nested constraints;
  34.      *
  35.      *   - If groups are passed explicitly to the nested constraints, but not
  36.      *     to the composite constraint, the groups of all nested constraints
  37.      *     are merged and used as groups for the composite constraint;
  38.      *
  39.      *   - If groups are passed explicitly to both the composite and its nested
  40.      *     constraints, the groups of the nested constraints must be a subset
  41.      *     of the groups of the composite constraint. If not, a
  42.      *     {@link ConstraintDefinitionException} is thrown.
  43.      *
  44.      * All this is done in the constructor, because constraints can then be
  45.      * cached. When constraints are loaded from the cache, no more group
  46.      * checks need to be done.
  47.      */
  48.     public function __construct(mixed $options null, array $groups nullmixed $payload null)
  49.     {
  50.         parent::__construct($options$groups$payload);
  51.         $this->initializeNestedConstraints();
  52.         /* @var Constraint[] $nestedConstraints */
  53.         $compositeOption $this->getCompositeOption();
  54.         $nestedConstraints $this->$compositeOption;
  55.         if (!\is_array($nestedConstraints)) {
  56.             $nestedConstraints = [$nestedConstraints];
  57.         }
  58.         foreach ($nestedConstraints as $constraint) {
  59.             if (!$constraint instanceof Constraint) {
  60.                 if (\is_object($constraint)) {
  61.                     $constraint $constraint::class;
  62.                 }
  63.                 throw new ConstraintDefinitionException(sprintf('The value "%s" is not an instance of Constraint in constraint "%s".'$constraint, static::class));
  64.             }
  65.             if ($constraint instanceof Valid) {
  66.                 throw new ConstraintDefinitionException(sprintf('The constraint Valid cannot be nested inside constraint "%s". You can only declare the Valid constraint directly on a field or method.', static::class));
  67.             }
  68.         }
  69.         if (!isset(((array) $this)['groups'])) {
  70.             $mergedGroups = [];
  71.             foreach ($nestedConstraints as $constraint) {
  72.                 foreach ($constraint->groups as $group) {
  73.                     $mergedGroups[$group] = true;
  74.                 }
  75.             }
  76.             // prevent empty composite constraint to have empty groups
  77.             $this->groups array_keys($mergedGroups) ?: [self::DEFAULT_GROUP];
  78.             $this->$compositeOption $nestedConstraints;
  79.             return;
  80.         }
  81.         foreach ($nestedConstraints as $constraint) {
  82.             if (isset(((array) $constraint)['groups'])) {
  83.                 $excessGroups array_diff($constraint->groups$this->groups);
  84.                 if (\count($excessGroups) > 0) {
  85.                     throw new ConstraintDefinitionException(sprintf('The group(s) "%s" passed to the constraint "%s" should also be passed to its containing constraint "%s".'implode('", "'$excessGroups), get_debug_type($constraint), static::class));
  86.                 }
  87.             } else {
  88.                 $constraint->groups $this->groups;
  89.             }
  90.         }
  91.         $this->$compositeOption $nestedConstraints;
  92.     }
  93.     /**
  94.      * Implicit group names are forwarded to nested constraints.
  95.      */
  96.     public function addImplicitGroupName(string $group)
  97.     {
  98.         parent::addImplicitGroupName($group);
  99.         /** @var Constraint[] $nestedConstraints */
  100.         $nestedConstraints $this->{$this->getCompositeOption()};
  101.         foreach ($nestedConstraints as $constraint) {
  102.             $constraint->addImplicitGroupName($group);
  103.         }
  104.     }
  105.     /**
  106.      * Returns the name of the property that contains the nested constraints.
  107.      */
  108.     abstract protected function getCompositeOption(): string;
  109.     /**
  110.      * @internal Used by metadata
  111.      *
  112.      * @return Constraint[]
  113.      */
  114.     public function getNestedConstraints(): array
  115.     {
  116.         /* @var Constraint[] $nestedConstraints */
  117.         return $this->{$this->getCompositeOption()};
  118.     }
  119.     /**
  120.      * Initializes the nested constraints.
  121.      *
  122.      * This method can be overwritten in subclasses to clean up the nested
  123.      * constraints passed to the constructor.
  124.      *
  125.      * @see Collection::initializeNestedConstraints()
  126.      */
  127.     protected function initializeNestedConstraints()
  128.     {
  129.     }
  130. }