11
11
12
12
namespace Symfony \Component \Validator \Constraints ;
13
13
14
+ use Symfony \Component \Validator \Exception \OutOfBoundsException ;
14
15
use Traversable ;
15
16
16
17
/**
17
- * Annotation for group sequences
18
+ * A sequence of validation groups.
19
+ *
20
+ * When validating a group sequence, each group will only be validated if all
21
+ * of the previous groups in the sequence succeeded. For example:
22
+ *
23
+ * $validator->validateObject($address, new GroupSequence('Basic', 'Strict'));
24
+ *
25
+ * In the first step, all constraints that belong to the group "Basic" will be
26
+ * validated. If none of the constraints fail, the validator will then validate
27
+ * the constraints in group "Strict". This is useful, for example, if "Strict"
28
+ * contains expensive checks that require a lot of CPU or slow, external
29
+ * services. You usually don't want to run expensive checks if any of the cheap
30
+ * checks fails.
31
+ *
32
+ * When adding metadata to a class, you can override the "Default" group of
33
+ * that class with a group sequence:
34
+ *
35
+ * /**
36
+ * * @GroupSequence({"Address", "Strict"})
37
+ * *\/
38
+ * class Address
39
+ * {
40
+ * // ...
41
+ * }
42
+ *
43
+ * Whenever you validate that object in the "Default" group, the group sequence
44
+ * will be validated:
45
+ *
46
+ * $validator->validateObject($address);
47
+ *
48
+ * If you want to execute the constraints of the "Default" group for a class
49
+ * with an overridden default group, pass the class name as group name instead:
50
+ *
51
+ * $validator->validateObject($address, "Address")
18
52
*
19
53
* @Annotation
20
54
*
25
59
class GroupSequence implements \ArrayAccess, \IteratorAggregate, \Countable
26
60
{
27
61
/**
28
- * The members of the sequence
29
- * @var array
62
+ * The groups in the sequence
63
+ *
64
+ * @var string[]|GroupSequence[]
30
65
*/
31
66
public $ groups ;
32
67
33
68
/**
34
- * The group under which cascaded objects are validated when validating
69
+ * The group in which cascaded objects are validated when validating
35
70
* this sequence.
36
71
*
37
72
* By default, cascaded objects are validated in each of the groups of
@@ -46,27 +81,80 @@ class GroupSequence implements \ArrayAccess, \IteratorAggregate, \Countable
46
81
*/
47
82
public $ cascadedGroup ;
48
83
84
+ /**
85
+ * Creates a new group sequence.
86
+ *
87
+ * @param string[] $groups The groups in the sequence
88
+ */
49
89
public function __construct (array $ groups )
50
90
{
51
91
// Support for Doctrine annotations
52
92
$ this ->groups = isset ($ groups ['value ' ]) ? $ groups ['value ' ] : $ groups ;
53
93
}
54
94
95
+ /**
96
+ * Returns an iterator for this group.
97
+ *
98
+ * @return Traversable The iterator
99
+ *
100
+ * @see \IteratorAggregate::getIterator()
101
+ *
102
+ * @deprecated Implemented for backwards compatibility. To be removed in
103
+ * Symfony 3.0.
104
+ */
55
105
public function getIterator ()
56
106
{
57
107
return new \ArrayIterator ($ this ->groups );
58
108
}
59
109
110
+ /**
111
+ * Returns whether the given offset exists in the sequence.
112
+ *
113
+ * @param integer $offset The offset
114
+ *
115
+ * @return Boolean Whether the offset exists
116
+ *
117
+ * @deprecated Implemented for backwards compatibility. To be removed in
118
+ * Symfony 3.0.
119
+ */
60
120
public function offsetExists ($ offset )
61
121
{
62
122
return isset ($ this ->groups [$ offset ]);
63
123
}
64
124
125
+ /**
126
+ * Returns the group at the given offset.
127
+ *
128
+ * @param integer $offset The offset
129
+ *
130
+ * @return string The group a the given offset
131
+ *
132
+ * @throws OutOfBoundsException If the object does not exist
133
+ *
134
+ * @deprecated Implemented for backwards compatibility. To be removed in
135
+ * Symfony 3.0.
136
+ */
65
137
public function offsetGet ($ offset )
66
138
{
139
+ if (!isset ($ this ->groups [$ offset ])) {
140
+ throw new OutOfBoundsException (sprintf (
141
+ 'The offset "%s" does not exist. ' ,
142
+ $ offset
143
+ ));
144
+ }
145
+
67
146
return $ this ->groups [$ offset ];
68
147
}
69
148
149
+ /**
150
+ * Sets the group at the given offset.
151
+ *
152
+ * @param integer $offset The offset
153
+ * @param string $value The group name
154
+ *
155
+ * @deprecated Implemented for backwards compatibility. To be removed in
156
+ * Symfony 3.0.
157
+ */
70
158
public function offsetSet ($ offset , $ value )
71
159
{
72
160
if (null !== $ offset ) {
@@ -78,11 +166,27 @@ public function offsetSet($offset, $value)
78
166
$ this ->groups [] = $ value ;
79
167
}
80
168
169
+ /**
170
+ * Removes the group at the given offset.
171
+ *
172
+ * @param integer $offset The offset
173
+ *
174
+ * @deprecated Implemented for backwards compatibility. To be removed in
175
+ * Symfony 3.0.
176
+ */
81
177
public function offsetUnset ($ offset )
82
178
{
83
179
unset($ this ->groups [$ offset ]);
84
180
}
85
181
182
+ /**
183
+ * Returns the number of groups in the sequence.
184
+ *
185
+ * @return integer The number of groups
186
+ *
187
+ * @deprecated Implemented for backwards compatibility. To be removed in
188
+ * Symfony 3.0.
189
+ */
86
190
public function count ()
87
191
{
88
192
return count ($ this ->groups );
0 commit comments