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\Constraints;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Validator\Constraint;
|
Chris@0
|
15 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * A constraint that is composed of other constraints.
|
Chris@0
|
19 *
|
Chris@0
|
20 * You should never use the nested constraint instances anywhere else, because
|
Chris@0
|
21 * their groups are adapted when passed to the constructor of this class.
|
Chris@0
|
22 *
|
Chris@0
|
23 * If you want to create your own composite constraint, extend this class and
|
Chris@0
|
24 * let {@link getCompositeOption()} return the name of the property which
|
Chris@0
|
25 * contains the nested constraints.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @author Bernhard Schussek <bschussek@gmail.com>
|
Chris@0
|
28 */
|
Chris@0
|
29 abstract class Composite extends Constraint
|
Chris@0
|
30 {
|
Chris@0
|
31 /**
|
Chris@0
|
32 * {@inheritdoc}
|
Chris@0
|
33 *
|
Chris@0
|
34 * The groups of the composite and its nested constraints are made
|
Chris@0
|
35 * consistent using the following strategy:
|
Chris@0
|
36 *
|
Chris@0
|
37 * - If groups are passed explicitly to the composite constraint, but
|
Chris@0
|
38 * not to the nested constraints, the options of the composite
|
Chris@0
|
39 * constraint are copied to the nested constraints;
|
Chris@0
|
40 *
|
Chris@0
|
41 * - If groups are passed explicitly to the nested constraints, but not
|
Chris@0
|
42 * to the composite constraint, the groups of all nested constraints
|
Chris@0
|
43 * are merged and used as groups for the composite constraint;
|
Chris@0
|
44 *
|
Chris@0
|
45 * - If groups are passed explicitly to both the composite and its nested
|
Chris@0
|
46 * constraints, the groups of the nested constraints must be a subset
|
Chris@0
|
47 * of the groups of the composite constraint. If not, a
|
Chris@0
|
48 * {@link ConstraintDefinitionException} is thrown.
|
Chris@0
|
49 *
|
Chris@0
|
50 * All this is done in the constructor, because constraints can then be
|
Chris@0
|
51 * cached. When constraints are loaded from the cache, no more group
|
Chris@0
|
52 * checks need to be done.
|
Chris@0
|
53 */
|
Chris@0
|
54 public function __construct($options = null)
|
Chris@0
|
55 {
|
Chris@0
|
56 parent::__construct($options);
|
Chris@0
|
57
|
Chris@0
|
58 $this->initializeNestedConstraints();
|
Chris@0
|
59
|
Chris@0
|
60 /* @var Constraint[] $nestedConstraints */
|
Chris@0
|
61 $compositeOption = $this->getCompositeOption();
|
Chris@0
|
62 $nestedConstraints = $this->$compositeOption;
|
Chris@0
|
63
|
Chris@17
|
64 if (!\is_array($nestedConstraints)) {
|
Chris@17
|
65 $nestedConstraints = [$nestedConstraints];
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 foreach ($nestedConstraints as $constraint) {
|
Chris@0
|
69 if (!$constraint instanceof Constraint) {
|
Chris@17
|
70 if (\is_object($constraint)) {
|
Chris@17
|
71 $constraint = \get_class($constraint);
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@17
|
74 throw new ConstraintDefinitionException(sprintf('The value %s is not an instance of Constraint in constraint %s', $constraint, \get_class($this)));
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 if ($constraint instanceof Valid) {
|
Chris@17
|
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)));
|
Chris@0
|
79 }
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 if (!property_exists($this, 'groups')) {
|
Chris@17
|
83 $mergedGroups = [];
|
Chris@0
|
84
|
Chris@0
|
85 foreach ($nestedConstraints as $constraint) {
|
Chris@0
|
86 foreach ($constraint->groups as $group) {
|
Chris@0
|
87 $mergedGroups[$group] = true;
|
Chris@0
|
88 }
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 $this->groups = array_keys($mergedGroups);
|
Chris@0
|
92 $this->$compositeOption = $nestedConstraints;
|
Chris@0
|
93
|
Chris@0
|
94 return;
|
Chris@0
|
95 }
|
Chris@0
|
96
|
Chris@0
|
97 foreach ($nestedConstraints as $constraint) {
|
Chris@0
|
98 if (property_exists($constraint, 'groups')) {
|
Chris@0
|
99 $excessGroups = array_diff($constraint->groups, $this->groups);
|
Chris@0
|
100
|
Chris@17
|
101 if (\count($excessGroups) > 0) {
|
Chris@17
|
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)));
|
Chris@0
|
103 }
|
Chris@0
|
104 } else {
|
Chris@0
|
105 $constraint->groups = $this->groups;
|
Chris@0
|
106 }
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 $this->$compositeOption = $nestedConstraints;
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@0
|
112 /**
|
Chris@0
|
113 * {@inheritdoc}
|
Chris@0
|
114 *
|
Chris@0
|
115 * Implicit group names are forwarded to nested constraints.
|
Chris@0
|
116 *
|
Chris@0
|
117 * @param string $group
|
Chris@0
|
118 */
|
Chris@0
|
119 public function addImplicitGroupName($group)
|
Chris@0
|
120 {
|
Chris@0
|
121 parent::addImplicitGroupName($group);
|
Chris@0
|
122
|
Chris@0
|
123 /** @var Constraint[] $nestedConstraints */
|
Chris@0
|
124 $nestedConstraints = $this->{$this->getCompositeOption()};
|
Chris@0
|
125
|
Chris@0
|
126 foreach ($nestedConstraints as $constraint) {
|
Chris@0
|
127 $constraint->addImplicitGroupName($group);
|
Chris@0
|
128 }
|
Chris@0
|
129 }
|
Chris@0
|
130
|
Chris@0
|
131 /**
|
Chris@0
|
132 * Returns the name of the property that contains the nested constraints.
|
Chris@0
|
133 *
|
Chris@0
|
134 * @return string The property name
|
Chris@0
|
135 */
|
Chris@0
|
136 abstract protected function getCompositeOption();
|
Chris@0
|
137
|
Chris@0
|
138 /**
|
Chris@0
|
139 * Initializes the nested constraints.
|
Chris@0
|
140 *
|
Chris@0
|
141 * This method can be overwritten in subclasses to clean up the nested
|
Chris@0
|
142 * constraints passed to the constructor.
|
Chris@0
|
143 *
|
Chris@0
|
144 * @see Collection::initializeNestedConstraints()
|
Chris@0
|
145 */
|
Chris@0
|
146 protected function initializeNestedConstraints()
|
Chris@0
|
147 {
|
Chris@0
|
148 }
|
Chris@0
|
149 }
|