8000 [Validator] Implemented BC validation of arrays through validate() · symfony/symfony@9b07b0c · GitHub
[go: up one dir, main page]

Skip to content

Commit 9b07b0c

Browse files
committed
[Validator] Implemented BC validation of arrays through validate()
1 parent 405a03b commit 9b07b0c

18 files changed

+426
-219
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
16+
17+
/**
18+
* @Annotation
19+
*
20+
* @since 2.5
21+
* @author Bernhard Schussek <bschussek@gmail.com>
22+
*/
23+
class Traverse extends Constraint
24+
{
25+
public $traverse = true;
26+
27+
public $deep = false;
28+
29+
public function __construct($options = null)
30+
{
31+
if (is_array($options) && array_key_exists('groups', $options)) {
32+
throw new ConstraintDefinitionException(sprintf(
33+
'The option "groups" is not supported by the constraint %s',
34+
__CLASS__
35+
));
36+
}
37+
38+
parent::__construct($options);
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function getDefaultOption()
45+
{
46+
return 'traverse';
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function getTargets()
53+
{
54+
return array(self::CLASS_CONSTRAINT, self::PROPERTY_CONSTRAINT);
55+
}
56+
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@
2121
*
2222
* @api
2323
*/
24-
class Valid extends Constraint
24+
class Valid extends Traverse
2525
{
26-
public $traverse = true;
27-
28-
public $deep = false;
29-
3026
public function __construct($options = null)
3127
{
3228
if (is_array($options) && array_key_exists('groups', $options)) {
@@ -35,4 +31,11 @@ public function __construct($options = null)
3531

3632
parent::__construct($options);
3733
}
34+
35+
public function getDefaultOption()
36+
{
37+
// Traverse is extended for backwards compatibility reasons
38+
// The parent class should be removed in 3.0
39+
return null;
40+
}
3841
}

src/Symfony/Component/Validator/Context/ExecutionContext.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,23 @@ class ExecutionContext implements ExecutionContextInterface
8080
*/
8181
private $translationDomain;
8282

83+
/**
84+
* Creates a new execution context.
85+
*
86+
* @param mixed $root The root value of the
87+
* validated object graph
88+
* @param ValidatorInterface $validator The validator
89+
* @param GroupManagerInterface $groupManager The manager for accessing
90+
* the currently validated
91+
* group
92+
* @param TranslatorInterface $translator The translator
93+
* @param string|null $translationDomain The translation domain to
94+
* use for translating
95+
* violation messages
96+
*
97+
* @internal Called by {@link ExecutionContextManager}. Should not be used
98+
* in user code.
99+
*/
83100
public function __construct($root, ValidatorInterface $validator, GroupManagerInterface $groupManager, TranslatorInterface $translator, $translationDomain = null)
84101
{
85102
$this->root = $root;

src/Symfony/Component/Validator/Context/ExecutionContextInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function getValue();
148148
* a {@link Mapping\PropertyMetadata} instance if the current value is
149149
* the value of a property and a {@link Mapping\GetterMetadata} instance if
150150
* the validated value is the result of a getter method. The metadata can
151-
* also be an {@link Mapping\AdHocMetadata} if the current value does not
151+
* also be an {@link Mapping\GenericMetadata} if the current value does not
152152
* belong to any structural element.
153153
*
154154
* @return MetadataInterface|null The metadata of the currently validated

src/Symfony/Component/Validator/Context/LegacyExecutionContext.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,25 @@
1919
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
2020

2121
/**
22-
* @since %%NextVersion%%
22+
* A backwards compatible execution context.
23+
*
24+
* @since 2.5
2325
* @author Bernhard Schussek <bschussek@gmail.com>
26+
*
27+
* @deprecated Implemented for backwards compatibility with Symfony < 2.5. To be
28+
* removed in 3.0.
2429
*/
2530
class LegacyExecutionContext extends ExecutionContext implements LegacyExecutionContextInterface
2631
{
32+
/**
33+
* Creates a new context.
34+
*
35+
* This constructor ensures that the given validator implements the
36+
* deprecated {@link \Symfony\Component\Validator\ValidatorInterface}. If
37+
* it does not, an {@link InvalidArgumentException} is thrown.
38+
*
39+
* @see ExecutionContext::__construct()
40+
*/
2741
public function __construct($root, ValidatorInterface $validator, GroupManagerInterface $groupManager, TranslatorInterface $translator, $translationDomain = null)
2842
{
2943
if (!$validator instanceof LegacyValidatorInterface) {
@@ -56,6 +70,9 @@ public function addViolation($message, array $parameters = array(), $invalidValu
5670
parent::addViolation($message, $parameters);
5771
}
5872

73+
/**
74+
* {@inheritdoc}
75+
*/
5976
public function addViolationAt($subPath, $message, array $parameters = array(), $invalidValue = null, $pluralization = null, $code = null)
6077
{
6178
if (func_num_args() >= 3) {
@@ -78,6 +95,9 @@ public function addViolationAt($subPath, $message, array $parameters = array(),
7895
;
7996
}
8097

98+
/**
99+
* {@inheritdoc}
100+
*/
81101
public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false)
82102
{
83103
// TODO handle $traverse and $deep
@@ -90,6 +110,9 @@ public function validate($value, $subPath = '', $groups = null, $traverse = fals
90110
;
91111
}
92112

113+
/**
114+
* {@inheritdoc}
115+
*/
93116
public function validateValue($value, $constraints, $subPath = '', $groups = null)
94117
{
95118
return $this
@@ -100,6 +123,9 @@ public function validateValue($value, $constraints, $subPath = '', $groups = nul
100123
;
101124
}
102125

126+
/**
127+
* {@inheritdoc}
128+
*/
103129
public function getMetadataFactory()
104130
{
105131
return $this->getValidator()->getMetadataFactory();

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

Lines changed: 0 additions & 59 deletions
This file was deleted.

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Mapping;
1313

1414
use Symfony\Component\Validator\Constraints\GroupSequence;
15+
use Symfony\Component\Validator\Constraints\Valid;
1516
use Symfony\Component\Validator\ValidationVisitorInterface;
1617
use Symfony\Component\Validator\PropertyMetadataContainerInterface;
1718
use Symfony\Component\Validator\MetadataInterface as LegacyMetadataInterface;
@@ -62,8 +63,6 @@ class ClassMetadata extends ElementMetadata implements LegacyMetadataInterface,
6263
*/
6364
public $groupSequenceProvider = false;
6465

65-
public $traversalStrategy = TraversalStrategy::IMPLICIT;
66-
6766
/**
6867
* @var \ReflectionClass
6968
*/
@@ -126,7 +125,12 @@ public function accept(ValidationVisitorInterface $visitor, $value, $group, $pro
126125
*/
127126
public function __sleep()
128127
{
129-
return array_merge(parent::__sleep(), array(
128+
$parentProperties = parent::__sleep();
129+
130+
// Don't store the cascading strategy. Classes never cascade.
131+
unset($parentProperties[array_search('cascadingStrategy', $parentProperties)]);
132+
133+
return array_merge($parentProperties, array(
130134
'getters',
131135
'groupSequence',
132136
'groupSequenceProvider',
@@ -174,7 +178,14 @@ public function addConstraint(Constraint $constraint)
174178
{
175179
if (!in_array(Constraint::CLASS_CONSTRAINT, (array) $constraint->getTargets())) {
176180
throw new ConstraintDefinitionException(sprintf(
177-
'The constraint %s cannot be put on classes',
181+
'The constraint "%s" cannot be put on classes.',
182+
get_class($constraint)
183+
));
184+
}
185+
186+
if ($constraint instanceof Valid) {
187+
throw new ConstraintDefinitionException(sprintf(
188+
'The constraint "%s" cannot be put on classes.',
178189
get_class($constraint)
179190
));
180191
}
@@ -434,9 +445,4 @@ public function getCascadingStrategy()
434445
{
435446
return CascadingStrategy::NONE;
436447
}
437-
438-
public function getTraversalStrategy()
439-
{
440-
return $this->traversalStrategy;
441-
}
442448
}

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

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -11,97 +11,6 @@
1111

1212
namespace Symfony\Component\Validator\Mapping;
1313

14-
use Symfony\Component\Validator\Constraint;
15-
16-
abstract class ElementMetadata
14+
abstract class ElementMetadata extends GenericMetadata
1715
{
18-
/**
19-
* @var Constraint[]
20-
*/
21-
public $constraints = array();
22-
23-
/**
24-
* @var array
25-
*/
26-
public $constraintsByGroup = array();
27-
28-
/**
29-
* Returns the names of the properties that should be serialized.
30-
*
31-
* @return array
32-
*/
33-
public function __sleep()
34-
{
35-
return array(
36-
'constraints',
37-
'constraintsByGroup',
38-
);
39-
}
40-
41-
/**
42-
* Clones this object.
43-
*/
44-
public function __clone()
45-
{
46-
$constraints = $this->constraints;
47-
48-
$this->constraints = array();
49-
$this->constraintsByGroup = array();
50-
51-
foreach ($constraints as $constraint) {
52-
$this->addConstraint(clone $constraint);
53-
}
54-
}
55-
56-
/**
57-
* Adds a constraint to this element.
58-
*
59-
* @param Constraint $constraint
60-
*
61-
* @return ElementMetadata
62-
*/
63-
public function addConstraint(Constraint $constraint)
64-
{
65-
$this->constraints[] = $constraint;
66-
67-
foreach ($constraint->groups as $group) {
68-
$this->constraintsByGroup[$group][] = $constraint;
69-
}
70-
71-
return $this;
72-
}
73-
74-
/**
75-
* Returns all constraints of this element.
76-
*
77-
* @return Constraint[] An array of Constraint instances
78-
*/
79-
public function getConstraints()
80-
{
81-
return $this->constraints;
82-
}
83-
84-
/**
85-
* Returns whether this element has any constraints.
86-
*
87-
* @return Boolean
88-
*/
89-
public function hasConstraints()
90-
{
91-
return count($this->constraints) > 0;
92-
}
93-
94-
/**
95-
* Returns the constraints of the given group and global ones (* group).
96-
*
97-
* @param string $group The group name
98-
*
99-
* @return array An array with all Constraint instances belonging to the group
100-
*/
101-
public function findConstraints($group)
102-
{
103-
return isset($this->constraintsByGroup[$group])
104-
? $this->constraintsByGroup[$group]
105-
: array();
106-
}
10716
}

0 commit comments

Comments
 (0)
0