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

Skip to content

Commit e8fa15b

Browse files
committed
[Validator] Fixed the new validator API under PHP < 5.3.9
1 parent 2936d10 commit e8fa15b

File tree

4 files changed

+22
-144
lines changed

4 files changed

+22
-144
lines changed

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Component\Validator\ConstraintViolation;
1717
use Symfony\Component\Validator\ConstraintViolationList;
1818
use Symfony\Component\Validator\Exception\BadMethodCallException;
19-
use Symfony\Component\Validator\ExecutionContextInterface as LegacyExecutionContextInterface;
2019
use Symfony\Component\Validator\Group\GroupManagerInterface;
2120
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
2221
use Symfony\Component\Validator\Node\Node;
@@ -33,7 +32,7 @@
3332
*
3433
* @see ExecutionContextInterface
3534
*/
36-
class ExecutionContext implements ExecutionContextInterface, LegacyExecutionContextInterface, NodeObserverInterface
35+
class ExecutionContext implements ExecutionContextInterface, NodeObserverInterface
3736
{
3837
/**
3938
* @var ValidatorInterface
@@ -121,6 +120,13 @@ public function addViolation($message, array $parameters = array(), $invalidValu
121120
// The parameters $invalidValue and following are ignored by the new
122121
// API, as they are not present in the new interface anymore.
123122
// You should use buildViolation() instead.
123+
if (func_num_args() > 2) {
124+
throw new BadMethodCallException(
125+
'The parameters $invalidValue, $pluralization and $code are '.
126+
'not supported anymore as of Symfony 2.5. Please use '.
127+
'buildViolation() instead or enable the legacy mode.'
128+
);
129+
}
124130

125131
$this->violations->add(new ConstraintViolation(
126132
$this->translator->trans($message, $parameters, $this->translationDomain),
@@ -235,8 +241,8 @@ public function getPropertyPath($subPath = '')
235241
public function addViolationAt($subPath, $message, array $parameters = array(), $invalidValue = null, $pluralization = null, $code = null)
236242
{
237243
throw new BadMethodCallException(
238-
'addViolationAt() is not supported anymore in the new API. '.
239-
'Please use buildViolation() or enable the legacy mode.'
244+
'addViolationAt() is not supported anymore as of Symfony 2.5. '.
245+
'Please use buildViolation() instead or enable the legacy mode.'
240246
);
241247
}
242248

@@ -246,8 +252,8 @@ public function addViolationAt($subPath, $message, array $parameters = array(),
246252
public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false)
247253
{
248254
throw new BadMethodCallException(
249-
'validate() is not supported anymore in the new API. '.
250-
'Please use getValidator() or enable the legacy mode.'
255+
'validate() is not supported anymore as of Symfony 2.5. '.
256+
'Please use getValidator() instead or enable the legacy mode.'
251257
);
252258
}
253259

@@ -257,8 +263,8 @@ public function validate($value, $subPath = '', $groups = null, $traverse = fals
257263
public function validateValue($value, $constraints, $subPath = '', $groups = null)
258264
{
259265
throw new BadMethodCallException(
260-
'validateValue() is not supported anymore in the new API. '.
261-
'Please use getValidator() or enable the legacy mode.'
266+
'validateValue() is not supported anymore as of Symfony 2.5. '.
267+
'Please use getValidator() instead or enable the legacy mode.'
262268
);
263269
}
264270

@@ -268,9 +274,9 @@ public function validateValue($value, $constraints, $subPath = '', $groups = nul
268274
public function getMetadataFactory()
269275
{
270276
throw new BadMethodCallException(
271-
'getMetadataFactory() is not supported anymore in the new API. '.
272-
'Please use getMetadataFor() or hasMetadataFor() or enable the '.
273-
'legacy mode.'
277+
'getMetadataFactory() is not supported anymore as of Symfony 2.5. '.
278+
'Please use getValidator() in combination with getMetadataFor() '.
279+
'or hasMetadataFor() instead or enable the legacy mode.'
274280
);
275281
}
276282
}

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

Lines changed: 2 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Validator\Constraints\GroupSequence;
1515
use Symfony\Component\Validator\ConstraintViolationListInterface;
16+
use Symfony\Component\Validator\ExecutionContextInterface as LegacyExecutionContextInterface;
1617
use Symfony\Component\Validator\Mapping\MetadataInterface;
1718
use Symfony\Component\Validator\Validator\ValidatorInterface;
1819
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
@@ -59,16 +60,8 @@
5960
* @since 2.5
6061
* @author Bernhard Schussek <[email protected]>
6162
*/
62-
interface ExecutionContextInterface
63+
interface ExecutionContextInterface extends LegacyExecutionContextInterface
6364
{
64-
/**
65-
* Adds a violation at the current node of the validation graph.
66-
*
67-
* @param string $message The error message
68-
* @param array $parameters The parameters substituted in the error message
69-
*/
70-
public function addViolation($message, array $parameters = array());
71-
7265
/**
7366
* Returns a builder for adding a violation with extended information.
7467
*
@@ -88,13 +81,6 @@ public function addViolation($message, array $parameters = array());
8881
*/
8982
public function buildViolation($message, array $parameters = array());
9083

91-
/**
92-
* Returns the violations generated in this context.
93-
*
94-
* @return ConstraintViolationListInterface The constraint violations
95-
*/
96-
public function getViolations();
97-
9884
/**
9985
* Returns the validator.
10086
*
@@ -114,108 +100,4 @@ public function getViolations();
114100
* @return ValidatorInterface
115101
*/
116102
public function getValidator();
117-
118-
/**
119-
* Returns the root value of the object graph.
120-
*
121-
* The validator, when given an object, traverses the properties and
122-
* related objects and their properties. The root of the validation is the
123-
* object at which the traversal started.
124-
*
125-
* The current value is returned by {@link getValue()}.
126-
*
127-
* @return mixed|null The root value of the validation or null, if no value
128-
* is currently being validated
129-
*/
130-
public function getRoot();
131-
132-
/**
133-
* Returns the value that the validator is currently validating.
134-
*
135-
* If you want to retrieve the object that was originally passed to the
136-
* validator, use {@link getRoot()}.
137-
*
138-
* @return mixed|null The currently validated value or null, if no value is
139-
* currently being validated
140-
*/
141-
public function getValue();
142-
143-
/**
144-
* Returns the metadata for the currently validated value.
145-
*
146-
* With the core implementation, this method returns a
147-
* {@link Mapping\ClassMetadata} instance if the current value is an object,
148-
* a {@link Mapping\PropertyMetadata} instance if the current value is
149-
* the value of a property and a {@link Mapping\GetterMetadata} instance if
150-
* the validated value is the result of a getter method. The metadata can
151-
* also be an {@link Mapping\GenericMetadata} if the current value does not
152-
* belong to any structural element.
153-
*
154-
* @return MetadataInterface|null The metadata of the currently validated
155-
* value or null, if no value is currently
156-
* being validated
157-
*/
158-
public function getMetadata();
159-
160-
/**
161-
* Returns the validation group that is currently being validated.
162-
*
163-
* @return string|GroupSequence|null The current validation group or null,
164-
* if no value is currently being
165-
* validated
166-
*/
167-
public function getGroup();
168-
169-
/**
170-
* Returns the class name of the current node.
171-
*
172-
* If the metadata of the current node does not implement
173-
* {@link ClassBasedInterface}, this method returns null.
174-
*
175-
* @return string|null The class name or null, if no class name could be
176-
* found
177-
*/
178-
public function getClassName();
179-
180-
/**
181-
* Returns the property name of the current node.
182-
*
183-
* If the metadata of the current node does not implement
184-
* {@link PropertyMetadataInterface}, this method returns null.
185-
*
186-
* @return string|null The property name or null, if no property name could
187-
* be found
188-
*/
189-
public function getPropertyName();
190-
191-
/**
192-
* Returns the property path to the value that the validator is currently
193-
* validating.
194-
*
195-
* For example, take the following object graph:
196-
*
197-
* (Person)---($address: Address)---($street: string)
198-
*
199-
* When the <tt>Person</tt> instance is passed to the validator, the
200-
* property path is initially empty. When the <tt>$address</tt> property
201-
* of that person is validated, the property path is "address". When
202-
* the <tt>$street</tt> property of the related <tt>Address</tt> instance
203-
* is validated, the property path is "address.street".
204-
*
205-
* Properties of objects are prefixed with a dot in the property path.
206-
* Indices of arrays or objects implementing the {@link \ArrayAccess}
207-
* interface are enclosed in brackets. For example, if the property in
208-
* the previous example is <tt>$addresses</tt> and contains an array
209-
* of <tt>Address</tt> instances, the property path generated for the
210-
* <tt>$street</tt> property of one of these addresses is for example
211-
* "addresses[0].street".
212-
*
213-
* @param string $subPath Optional. The suffix appended to the current
214-
* property path
215-
*
216-
* @return string The current property path. The result may be an empty
217-
* string if the validator is currently validating the
218-
* root value of the validation graph
219-
*/
220-
public function getPropertyPath($subPath = '');
221103
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Symfony\Component\Validator\Constraints\Traverse;
1616
use Symfony\Component\Validator\Constraints\Valid;
1717
use Symfony\Component\Validator\Exception\InvalidArgumentException;
18-
use Symfony\Component\Validator\ExecutionContextInterface as LegacyExecutionContextInterface;
1918
use Symfony\Component\Validator\Group\GroupManagerInterface;
2019
use Symfony\Component\Validator\Validator\ValidatorInterface;
2120
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
@@ -29,7 +28,7 @@
2928
* @deprecated Implemented for backwards compatibility with Symfony < 2.5. To be
3029
* removed in 3.0.
3130
*/
32-
class LegacyExecutionContext extends ExecutionContext implements LegacyExecutionContextInterface
31+
class LegacyExecutionContext extends ExecutionContext
3332
{
3433
/**
3534
* Creates a new context.
@@ -66,7 +65,7 @@ public function __construct(ValidatorInterface $validator, $root, GroupManagerIn
6665
*/
6766
public function addViolation($message, array $parameters = array(), $invalidValue = null, $pluralization = null, $code = null)
6867
{
69-
if (func_num_args() >= 3) {
68+
if (func_num_args() > 2) {
7069
$this
7170
->buildViolation($message, $parameters)
7271
->setInvalidValue($invalidValue)
@@ -86,7 +85,7 @@ public function addViolation($message, array $parameters = array(), $invalidValu
8685
*/
8786
public function addViolationAt($subPath, $message, array $parameters = array(), $invalidValue = null, $pluralization = null, $code = null)
8887
{
89-
if (func_num_args() >= 3) {
88+
if (func_num_args() > 2) {
9089
$this
9190
->buildViolation($message, $parameters)
9291
->atPath($subPath)

src/Symfony/Component/Validator/Tests/Validator/Validator2Dot5ApiTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@
2323

2424
class Validator2Dot5ApiTest extends Abstract2Dot5ApiTest
2525
{
26-
protected function setUp()
27-
{
28-
if (version_compare(PHP_VERSION, '5.3.9', '<')) {
29-
$this->markTestSkipped('Not supported prior to PHP 5.3.9');
30-
}
31-
32-
parent::setUp();
33-
}
34-
3526
protected function createValidator(MetadataFactoryInterface $metadataFactory)
3627
{
3728
$nodeTraverser = new NodeTraverser($metadataFactory);

0 commit comments

Comments
 (0)