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\Exception\OutOfBoundsException;
17 * A sequence of validation groups.
19 * When validating a group sequence, each group will only be validated if all
20 * of the previous groups in the sequence succeeded. For example:
22 * $validator->validate($address, null, new GroupSequence(array('Basic', 'Strict')));
24 * In the first step, all constraints that belong to the group "Basic" will be
25 * validated. If none of the constraints fail, the validator will then validate
26 * the constraints in group "Strict". This is useful, for example, if "Strict"
27 * contains expensive checks that require a lot of CPU or slow, external
28 * services. You usually don't want to run expensive checks if any of the cheap
31 * When adding metadata to a class, you can override the "Default" group of
32 * that class with a group sequence:
35 * * @GroupSequence({"Address", "Strict"})
42 * Whenever you validate that object in the "Default" group, the group sequence
45 * $validator->validate($address);
47 * If you want to execute the constraints of the "Default" group for a class
48 * with an overridden default group, pass the class name as group name instead:
50 * $validator->validate($address, null, "Address")
53 * @Target({"CLASS", "ANNOTATION"})
55 * @author Bernhard Schussek <bschussek@gmail.com>
57 * Implementing \ArrayAccess, \IteratorAggregate and \Countable is @deprecated since 2.5 and will be removed in 3.0.
59 class GroupSequence implements \ArrayAccess, \IteratorAggregate, \Countable
62 * The groups in the sequence.
64 * @var string[]|GroupSequence[]
69 * The group in which cascaded objects are validated when validating
72 * By default, cascaded objects are validated in each of the groups of
75 * If a class has a group sequence attached, that sequence replaces the
76 * "Default" group. When validating that class in the "Default" group, the
77 * group sequence is used instead, but still the "Default" group should be
78 * cascaded to other objects.
80 * @var string|GroupSequence
82 public $cascadedGroup;
85 * Creates a new group sequence.
87 * @param string[] $groups The groups in the sequence
89 public function __construct(array $groups)
91 // Support for Doctrine annotations
92 $this->groups = isset($groups['value']) ? $groups['value'] : $groups;
96 * Returns an iterator for this group.
98 * Implemented for backwards compatibility with Symfony < 2.5.
100 * @return \Traversable The iterator
102 * @see \IteratorAggregate::getIterator()
103 * @deprecated since version 2.5, to be removed in 3.0.
105 public function getIterator()
107 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
109 return new \ArrayIterator($this->groups);
113 * Returns whether the given offset exists in the sequence.
115 * Implemented for backwards compatibility with Symfony < 2.5.
117 * @param int $offset The offset
119 * @return bool Whether the offset exists
121 * @deprecated since version 2.5, to be removed in 3.0.
123 public function offsetExists($offset)
125 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
127 return isset($this->groups[$offset]);
131 * Returns the group at the given offset.
133 * Implemented for backwards compatibility with Symfony < 2.5.
135 * @param int $offset The offset
137 * @return string The group a the given offset
139 * @throws OutOfBoundsException If the object does not exist
141 * @deprecated since version 2.5, to be removed in 3.0.
143 public function offsetGet($offset)
145 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
147 if (!isset($this->groups[$offset])) {
148 throw new OutOfBoundsException(sprintf(
149 'The offset "%s" does not exist.',
154 return $this->groups[$offset];
158 * Sets the group at the given offset.
160 * Implemented for backwards compatibility with Symfony < 2.5.
162 * @param int $offset The offset
163 * @param string $value The group name
165 * @deprecated since version 2.5, to be removed in 3.0.
167 public function offsetSet($offset, $value)
169 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
171 if (null !== $offset) {
172 $this->groups[$offset] = $value;
177 $this->groups[] = $value;
181 * Removes the group at the given offset.
183 * Implemented for backwards compatibility with Symfony < 2.5.
185 * @param int $offset The offset
187 * @deprecated since version 2.5, to be removed in 3.0.
189 public function offsetUnset($offset)
191 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
193 unset($this->groups[$offset]);
197 * Returns the number of groups in the sequence.
199 * Implemented for backwards compatibility with Symfony < 2.5.
201 * @return int The number of groups
203 * @deprecated since version 2.5, to be removed in 3.0.
205 public function count()
207 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
209 return count($this->groups);