8000 [Validator] Completed GroupSequence implementation · symfony/symfony@8318286 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8318286

Browse files
committed
[Validator] Completed GroupSequence implementation
1 parent 5fbf848 commit 8318286

File tree

3 files changed

+147
-4
lines changed

3 files changed

+147
-4
lines changed

src/Symfony/Component/Validator/Constraints/GroupSequence.php

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,44 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Symfony\Component\Validator\Exception\OutOfBoundsException;
1415
use Traversable;
1516

1617
/**
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")
1852
*
1953
* @Annotation
2054
*
@@ -25,13 +59,14 @@
2559
class GroupSequence implements \ArrayAccess, \IteratorAggregate, \Countable
2660
{
2761
/**
28-
* The members of the sequence
29-
* @var array
62+
* The groups in the sequence
63+
*
64+
* @var string[]|GroupSequence[]
3065
*/
3166
public $groups;
3267

3368
/**
34-
* The group under which cascaded objects are validated when validating
69+
* The group in which cascaded objects are validated when validating
3570
* this sequence.
3671
*
3772
* By default, cascaded objects are validated in each of the groups of
@@ -46,27 +81,80 @@ class GroupSequence implements \ArrayAccess, \IteratorAggregate, \Countable
4681
*/
4782
public $cascadedGroup;
4883

84+
/**
85+
* Creates a new group sequence.
86+
*
87+
* @param string[] $groups The groups in the sequence
88+
*/
4989
public function __construct(array $groups)
5090
{
5191
// Support for Doctrine annotations
5292
$this->groups = isset($groups['value']) ? $groups['value'] : $groups;
5393
}
5494

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+
*/
55105
public function getIterator()
56106
{
57107
return new \ArrayIterator($this->groups);
58108
}
59109

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+
*/
60120
public function offsetExists($offset)
61121
{
62122
return isset($this->groups[$offset]);
63123
}
64124

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+
*/
65137
public function offsetGet($offset)
66138
{
139+
if (!isset($this->groups[$offset])) {
140+
throw new OutOfBoundsException(sprintf(
141+
'The offset "%s" does not exist.',
142+
$offset
143+
));
144+
}
145+
67146
return $this->groups[$offset];
68147
}
69148

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+
*/
70158
public function offsetSet($offset, $value)
71159
{
72160
if (null !== $offset) {
@@ -78,11 +166,27 @@ public function offsetSet($offset, $value)
78166
$this->groups[] = $value;
79167
}
80168

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+
*/
81177
public function offsetUnset($offset)
82178
{
83179
unset($this->groups[$offset]);
84180
}
85181

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+
*/
86190
public function count()
87191
{
88192
return count($this->groups);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Exception;
13+
14+
/**
15+
* Base OutOfBoundsException for the Validator component.
16+
*
17+
* @author Bernhard Schussek <bschussek@gmail.com>
18+
*/
19+
class OutOfBoundsException extends \OutOfBoundsException implements ExceptionInterface
20+
{
21+
}

src/Symfony/Component/Validator/Tests/Constraints/GroupSequenceTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,22 @@ public function testArrayAccess()
6363
$this->assertTrue(isset($sequence[0]));
6464
$this->assertSame('Group 1', $sequence[0]);
6565
}
66+
67+
/**
68+
* @expectedException \Symfony\Component\Validator\Exception\OutOfBoundsException
69+
*/
70+
public function testGetExpectsExistingKey()
71+
{
72+
$sequence = new GroupSequence(array('Group 1', 'Group 2'));
73+
74+
$sequence[2];
75+
}
76+
77+
public function testUnsetIgnoresNonExistingKeys()
78+
{
79+
$sequence = new GroupSequence(array('Group 1', 'Group 2'));
80+
81+
// should not fail
82+
unset($sequence[2]);
83+
}
6684
}

0 commit comments

Comments
 (0)
0