Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a3555fb

Browse files
committed
[Validator] Fixed: Objects are not traversed unless they are instances of Traversable
1 parent 2c65a28 commit a3555fb

15 files changed

+539
-116
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,27 @@
2121
*
2222
* @api
2323
*/
24-
class Valid extends Traverse
24+
class Valid extends Constraint
2525
{
26+
/**
27+
* @deprecated Deprecated since version 2.5, to be removed in Symfony 3.0.
28+
* Use the {@link Traverse} constraint instead.
29+
*/
30+
public $traverse = true;
31+
32+
/**
33+
* @deprecated Deprecated since version 2.5, to be removed in Symfony 3.0.
34+
* Use the {@link Traverse} constraint instead.
35+
*/
36+
public $deep = false;
37+
2638
public function __construct($options = null)
2739
{
2840
if (is_array($options) && array_key_exists('groups', $options)) {
29-
throw new ConstraintDefinitionException(sprintf('The option "groups" is not supported by the constraint %s', __CLASS__));
41+
throw new ConstraintDefinitionException(sprintf(
42+
'The option "groups" is not supported by the constraint %s',
43+
__CLASS__
44+
));
3045
}
3146

3247
parent::__construct($options);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ public function addConstraint(Constraint $constraint)
7777
if ($constraint instanceof Valid) {
7878
$this->cascadingStrategy = CascadingStrategy::CASCADE;
7979

80-
// Continue. Valid extends Traverse, so the return statement in the
81-
// next block is going be executed.
80+
return $this;
8281
}
8382

8483
if ($constraint instanceof Traverse) {

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

Lines changed: 19 additions & 0 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\Valid;
1415
use Symfony\Component\Validator\ValidationVisitorInterface;
1516
use Symfony\Component\Validator\PropertyMetadataInterface as LegacyPropertyMetadataInterface;
1617
use Symfony\Component\Validator\Constraint;
@@ -58,6 +59,24 @@ public function addConstraint(Constraint $constraint)
5859
));
5960
}
6061

62+
// BC with Symfony < 2.5
63+
// Only process if the traversal strategy was not already set by the
64+
// Traverse constraint
65+
if ($constraint instanceof Valid && !$this->traversalStrategy) {
66+
if (true === $constraint->traverse) {
67+
// Try to traverse cascaded objects, but ignore if they do not
68+
// implement Traversable
69+
$this->traversalStrategy = TraversalStrategy::TRAVERSE
70+
| TraversalStrategy::IGNORE_NON_TRAVERSABLE;
71+
72+
if ($constraint->deep) {
73+
$this->traversalStrategy |= TraversalStrategy::RECURSIVE;
74+
}
75+
} elseif (false === $constraint->traverse) {
76+
$this->traversalStrategy = TraversalStrategy::NONE;
77+
}
78+
}
79+
6180
parent::addConstraint($constraint);
6281

6382
return $this;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class TraversalStrategy
2525

2626
const RECURSIVE = 4;
2727

28+
const IGNORE_NON_TRAVERSABLE = 8;
29+
2830
private function __construct()
2931
{
3032
}

src/Symfony/Component/Validator/Node/ClassNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ class ClassNode extends Node
3737
* to this node
3838
* @param string[] $groups The groups in which this
3939
* node should be validated
40-
* @param string[] $cascadedGroups The groups in which
40+
* @param string[]|null $cascadedGroups The groups in which
4141
* cascaded objects should be
4242
* validated
4343
*
4444
* @throws UnexpectedTypeException If the given value is not an object
4545
*/
46-
public function __construct($object, ClassMetadataInterface $metadata, $propertyPath, array $groups, array $cascadedGroups)
46+
public function __construct($object, ClassMetadataInterface $metadata, $propertyPath, array $groups, $cascadedGroups = null)
4747
{
4848
if (!is_object($object)) {
4949
throw new UnexpectedTypeException($object, 'object');

src/Symfony/Component/Validator/Node/Node.php

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

1212
namespace Symfony\Component\Validator\Node;
1313

14+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1415
use Symfony\Component\Validator\Mapping\MetadataInterface;
1516

1617
/**
@@ -65,11 +66,17 @@ abstract class Node
6566
* this node
6667
* @param string[] $groups The groups in which this node
6768
* should be validated
68-
* @param string[] $cascadedGroups The groups in which cascaded
69+
* @param string[]|null $cascadedGroups The groups in which cascaded
6970
* objects should be validated
71+
*
72+
* @throws UnexpectedTypeException If $cascadedGroups is invalid
7073
*/
71-
public function __construct($value, MetadataInterface $metadata, $propertyPath, array $groups, array $cascadedGroups)
74+
public function __construct($value, MetadataInterface $metadata, $propertyPath, array $groups, $cascadedGroups = null)
7275
{
76+
if (null !== $cascadedGroups && !is_array($cascadedGroups)) {
77+
throw new UnexpectedTypeException($cascadedGroups, 'null or array');
78+
}
79+
7380
$this->value = $value;
7481
$this->metadata = $metadata;
7582
$this->propertyPath = $propertyPath;

src/Symfony/Component/Validator/Node/PropertyNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ class PropertyNode extends Node
4646
* to this node
4747
* @param string[] $groups The groups in which this
4848
* node should be validated
49-
* @param string[] $cascadedGroups The groups in which
49+
* @param string[]|null $cascadedGroups The groups in which
5050
* cascaded objects should
5151
* be validated
5252
*/
53-
public function __construct($value, PropertyMetadataInterface $metadata, $propertyPath, array $groups, array $cascadedGroups)
53+
public function __construct($value, PropertyMetadataInterface $metadata, $propertyPath, array $groups, $cascadedGroups = null)
5454
{
5555
parent::__construct(
5656
$value,

0 commit comments

Comments
 (0)