Skip to content

Commit 25cdc68

Browse files
committed
[Validator] Refactored ValidatorTest and ValidationVisitorTest into an abstract validator test class
1 parent 95f8e43 commit 25cdc68

File tree

13 files changed

+1410
-835
lines changed

13 files changed

+1410
-835
lines changed

UPGRADE-2.5.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ Validator
5656

5757
After:
5858

59-
Default email validation is now done via a simple regex which may cause invalid emails (not RFC compilant) to be
59+
Default email validation is now done via a simple regex which may cause invalid emails (not RFC compilant) to be
6060
valid. This is the default behaviour.
6161

6262
Strict email validation has to be explicitly activated in the configuration file by adding
63+
6364
```
6465
framework:
6566
//...
@@ -68,7 +69,29 @@ Validator
6869
//...
6970
7071
```
72+
7173
Also you have to add to your composer.json:
74+
7275
```
7376
"egulias/email-validator": "1.1.*"
7477
```
78+
79+
* `ClassMetadata::getGroupSequence()` now returns `GroupSequence` instances
80+
instead of an array. The sequence implements `\Traversable`, `\ArrayAccess`
81+
and `\Countable`, so in most cases you should be fine. If you however use the
82+
sequence with PHP's `array_*()` functions, you should cast it to an array
83+
first using `iterator_to_array()`:
84+
85+
Before:
86+
87+
```
88+
$sequence = $metadata->getGroupSequence();
89+
$result = array_map($callback, $sequence);
90+
```
91+
92+
After:
93+
94+
```
95+
$sequence = iterator_to_array($metadata->getGroupSequence());
96+
$result = array_map($callback, $sequence);
97+
```

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ CHANGELOG
66

77
* deprecated `ApcCache` in favor of `DoctrineCache`
88
* added `DoctrineCache` to adapt any Doctrine cache
9+
* `GroupSequence` now implements `ArrayAccess`, `Countable` and `Traversable`
10+
* changed `ClassMetadata::getGroupSequence()` to return a `GroupSequence` instance instead of an array
911

1012
2.4.0
1113
-----

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,6 @@ public function getDefaultOption()
7171
*/
7272
public function getTargets()
7373
{
74-
return self::CLASS_CONSTRAINT;
74+
return array(self::CLASS_CONSTRAINT, self::PROPERTY_CONSTRAINT);
7575
}
7676
}

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @api
2222
*/
23-
class GroupSequence
23+
class GroupSequence implements \ArrayAccess, \IteratorAggregate, \Countable
2424
{
2525
/**
2626
* The members of the sequence
@@ -30,6 +30,43 @@ class GroupSequence
3030

3131
public function __construct(array $groups)
3232
{
33-
$this->groups = $groups['value'];
33+
// Support for Doctrine annotations
34+
$this->groups = isset($groups['value']) ? $groups['value'] : $groups;
35+
}
36+
37+
public function getIterator()
38+
{
39+
return new \ArrayIterator($this->groups);
40+
}
41+
42+
public function offsetExists($offset)
43+
{
44+
return isset($this->groups[$offset]);
45+
}
46+
47+
public function offsetGet($offset)
48+
{
49+
return $this->groups[$offset];
50+
}
51+
52+
public function offsetSet($offset, $value)
53+
{
54+
if (null !== $offset) {
55+
$this->groups[$offset] = $value;
56+
57+
return;
58+
}
59+
60+
$this->groups[] = $value;
61+
}
62+
63+
public function offsetUnset($offset)
64+
{
65+
unset($this->groups[$offset]);
66+
}
67+
68+
public function count()
69+
{
70+
return count($this->groups);
3471
}
3572
}

src/Symfony/Component/Validator/Mapping/ClassMetadata.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Mapping;
1313

14+
use Symfony\Component\Validator\Constraints\GroupSequence;
1415
use Symfony\Component\Validator\ValidationVisitorInterface;
1516
use Symfony\Component\Validator\PropertyMetadataContainerInterface;
1617
use Symfony\Component\Validator\ClassBasedInterface;
@@ -330,27 +331,31 @@ public function getConstrainedProperties()
330331
/**
331332
* Sets the default group sequence for this class.
332333
*
333-
* @param array $groups An array of group names
334+
* @param array $groupSequence An array of group names
334335
*
335336
* @return ClassMetadata
336337
*
337338
* @throws GroupDefinitionException
338339
*/
339-
public function setGroupSequence(array $groups)
340+
public function setGroupSequence($groupSequence)
340341
{
341342
if ($this->isGroupSequenceProvider()) {
342343
throw new GroupDefinitionException('Defining a static group sequence is not allowed with a group sequence provider');
343344
}
344345

345-
if (in_array(Constraint::DEFAULT_GROUP, $groups, true)) {
346+
if (is_array($groupSequence)) {
347+
$groupSequence = new GroupSequence($groupSequence);
348+
}
349+
350+
if (in_array(Constraint::DEFAULT_GROUP, $groupSequence->groups, true)) {
346351
throw new GroupDefinitionException(sprintf('The group "%s" is not allowed in group sequences', Constraint::DEFAULT_GROUP));
347352
}
348353

349-
if (!in_array($this->getDefaultGroup(), $groups, true)) {
354+
if (!in_array($this->getDefaultGroup(), $groupSequence->groups, true)) {
350355
throw new GroupDefinitionException(sprintf('The group "%s" is missing in the group sequence', $this->getDefaultGroup()));
351356
}
352357

353-
$this->groupSequence = $groups;
358+
$this->groupSequence = $groupSequence;
354359

355360
return $this;
356361
}
@@ -368,7 +373,7 @@ public function hasGroupSequence()
368373
/**
369374
* Returns the default group sequence for this class.
370375
*
371-
* @return array An array of group names
376+
* @return GroupSequence The group sequence or null
372377
*/
373378
public function getGroupSequence()
374379
{

0 commit comments

Comments
 (0)