You are here

public function Composite::__construct in Plug 7

The groups of the composite and its nested constraints are made consistent using the following strategy:

  • If groups are passed explicitly to the composite constraint, but not to the nested constraints, the options of the composite constraint are copied to the nested constraints;
  • If groups are passed explicitly to the nested constraints, but not to the composite constraint, the groups of all nested constraints are merged and used as groups for the composite constraint;
  • If groups are passed explicitly to both the composite and its nested constraints, the groups of the nested constraints must be a subset of the groups of the composite constraint. If not, a {@link ConstraintDefinitionException} is thrown.

All this is done in the constructor, because constraints can then be cached. When constraints are loaded from the cache, no more group checks need to be done.

Overrides Constraint::__construct

1 call to Composite::__construct()
Collection::__construct in lib/Symfony/validator/Symfony/Component/Validator/Constraints/Collection.php
The groups of the composite and its nested constraints are made consistent using the following strategy:
1 method overrides Composite::__construct()
Collection::__construct in lib/Symfony/validator/Symfony/Component/Validator/Constraints/Collection.php
The groups of the composite and its nested constraints are made consistent using the following strategy:

File

lib/Symfony/validator/Symfony/Component/Validator/Constraints/Composite.php, line 55

Class

Composite
A constraint that is composed of other constraints.

Namespace

Symfony\Component\Validator\Constraints

Code

public function __construct($options = null) {
  parent::__construct($options);
  $this
    ->initializeNestedConstraints();

  /** @var Constraint[] $nestedConstraints */
  $compositeOption = $this
    ->getCompositeOption();
  $nestedConstraints = $this->{$compositeOption};
  if (!is_array($nestedConstraints)) {
    $nestedConstraints = array(
      $nestedConstraints,
    );
  }
  foreach ($nestedConstraints as $constraint) {
    if (!$constraint instanceof Constraint) {
      throw new ConstraintDefinitionException(sprintf('The value %s is not an instance of Constraint in constraint %s', $constraint, get_class($this)));
    }
    if ($constraint instanceof Valid) {
      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.', get_class($this)));
    }
  }
  if (!property_exists($this, 'groups')) {
    $mergedGroups = array();
    foreach ($nestedConstraints as $constraint) {
      foreach ($constraint->groups as $group) {
        $mergedGroups[$group] = true;
      }
    }
    $this->groups = array_keys($mergedGroups);
    $this->{$compositeOption} = $nestedConstraints;
    return;
  }
  foreach ($nestedConstraints as $constraint) {
    if (property_exists($constraint, 'groups')) {
      $excessGroups = array_diff($constraint->groups, $this->groups);
      if (count($excessGroups) > 0) {
        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_class($constraint), get_class($this)));
      }
    }
    else {
      $constraint->groups = $this->groups;
    }
  }
  $this->{$compositeOption} = $nestedConstraints;
}