4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Constraints;
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
18 * A constraint that is composed of other constraints.
20 * You should never use the nested constraint instances anywhere else, because
21 * their groups are adapted when passed to the constructor of this class.
23 * If you want to create your own composite constraint, extend this class and
24 * let {@link getCompositeOption()} return the name of the property which
25 * contains the nested constraints.
27 * @author Bernhard Schussek <bschussek@gmail.com>
29 abstract class Composite extends Constraint
34 * The groups of the composite and its nested constraints are made
35 * consistent using the following strategy:
37 * - If groups are passed explicitly to the composite constraint, but
38 * not to the nested constraints, the options of the composite
39 * constraint are copied to the nested constraints;
41 * - If groups are passed explicitly to the nested constraints, but not
42 * to the composite constraint, the groups of all nested constraints
43 * are merged and used as groups for the composite constraint;
45 * - If groups are passed explicitly to both the composite and its nested
46 * constraints, the groups of the nested constraints must be a subset
47 * of the groups of the composite constraint. If not, a
48 * {@link ConstraintDefinitionException} is thrown.
50 * All this is done in the constructor, because constraints can then be
51 * cached. When constraints are loaded from the cache, no more group
52 * checks need to be done.
54 public function __construct($options = null)
56 parent::__construct($options);
58 $this->initializeNestedConstraints();
60 /* @var Constraint[] $nestedConstraints */
61 $compositeOption = $this->getCompositeOption();
62 $nestedConstraints = $this->$compositeOption;
64 if (!\is_array($nestedConstraints)) {
65 $nestedConstraints = array($nestedConstraints);
68 foreach ($nestedConstraints as $constraint) {
69 if (!$constraint instanceof Constraint) {
70 if (\is_object($constraint)) {
71 $constraint = \get_class($constraint);
74 throw new ConstraintDefinitionException(sprintf('The value %s is not an instance of Constraint in constraint %s', $constraint, \get_class($this)));
77 if ($constraint instanceof Valid) {
78 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)));
82 if (!property_exists($this, 'groups')) {
83 $mergedGroups = array();
85 foreach ($nestedConstraints as $constraint) {
86 foreach ($constraint->groups as $group) {
87 $mergedGroups[$group] = true;
91 $this->groups = array_keys($mergedGroups);
92 $this->$compositeOption = $nestedConstraints;
97 foreach ($nestedConstraints as $constraint) {
98 if (property_exists($constraint, 'groups')) {
99 $excessGroups = array_diff($constraint->groups, $this->groups);
101 if (\count($excessGroups) > 0) {
102 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)));
105 $constraint->groups = $this->groups;
109 $this->$compositeOption = $nestedConstraints;
115 * Implicit group names are forwarded to nested constraints.
117 * @param string $group
119 public function addImplicitGroupName($group)
121 parent::addImplicitGroupName($group);
123 /** @var Constraint[] $nestedConstraints */
124 $nestedConstraints = $this->{$this->getCompositeOption()};
126 foreach ($nestedConstraints as $constraint) {
127 $constraint->addImplicitGroupName($group);
132 * Returns the name of the property that contains the nested constraints.
134 * @return string The property name
136 abstract protected function getCompositeOption();
139 * Initializes the nested constraints.
141 * This method can be overwritten in subclasses to clean up the nested
142 * constraints passed to the constructor.
144 * @see Collection::initializeNestedConstraints()
146 protected function initializeNestedConstraints()