Skip to content

Commit d30ff4e

Browse files
committed
Complete the removal of API versions in the validator component
Thanks to the PHP version requirement bump to 5.3.9+, the BC layer can be available all the time.
1 parent 75088c0 commit d30ff4e

File tree

52 files changed

+124
-1390
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+124
-1390
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,8 +763,9 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
763763

764764
// You can use this parameter to check the API version in your own
765765
// bundle extension classes
766+
// This is set to 2.5-bc for compatibility with Symfony 2.5 and 2.6.
766767
// @deprecated since version 2.7, to be removed in 3.0
767-
$container->setParameter('validator.api', $config['api']);
768+
$container->setParameter('validator.api', '2.5-bc');
768769
}
769770

770771
private function getValidatorMappingFiles(ContainerBuilder $container)

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

Lines changed: 82 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
use Symfony\Component\Translation\TranslatorInterface;
1515
use Symfony\Component\Validator\ClassBasedInterface;
1616
use Symfony\Component\Validator\Constraint;
17+
use Symfony\Component\Validator\Constraints\Valid;
1718
use Symfony\Component\Validator\ConstraintViolation;
1819
use Symfony\Component\Validator\ConstraintViolationList;
19-
use Symfony\Component\Validator\Exception\BadMethodCallException;
2020
use Symfony\Component\Validator\Mapping\MetadataInterface;
2121
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
2222
use Symfony\Component\Validator\Util\PropertyPath;
2323
use Symfony\Component\Validator\Validator\ValidatorInterface;
24+
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
2425
use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;
2526

2627
/**
@@ -187,11 +188,17 @@ public function addViolation($message, array $parameters = array(), $invalidValu
187188
// API, as they are not present in the new interface anymore.
188189
// You should use buildViolation() instead.
189190
if (func_num_args() > 2) {
190-
throw new BadMethodCallException(
191-
'The parameters $invalidValue, $plural and $code are '.
192-
'not supported anymore as of Symfony 2.5. Please use '.
193-
'buildViolation() instead or enable the legacy mode.'
194-
);
191+
trigger_error('The parameters $invalidValue, $plural and $code in method '.__METHOD__.' are deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::buildViolation method instead.', E_USER_DEPRECATED);
192+
193+
$this
194+
->buildViolation($message, $parameters)
195+
->setInvalidValue($invalidValue)
196+
->setPlural($plural)
197+
->setCode($code)
198+
->addViolation()
199+
;
200+
201+
return;
195202
}
196203

197204
$this->violations->add(new ConstraintViolation(
@@ -310,44 +317,96 @@ public function getPropertyPath($subPath = '')
310317
*/
311318
public function addViolationAt($subPath, $message, array $parameters = array(), $invalidValue = null, $plural = null, $code = null)
312319
{
313-
throw new BadMethodCallException(
314-
'addViolationAt() is not supported anymore as of Symfony 2.5. '.
315-
'Please use buildViolation() instead or enable the legacy mode.'
316-
);
320+
trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::buildViolation method instead.', E_USER_DEPRECATED);
321+
322+
if (func_num_args() > 2) {
323+
$this
324+
->buildViolation($message, $parameters)
325+
->atPath($subPath)
326+
->setInvalidValue($invalidValue)
327+
->setPlural($plural)
328+
->setCode($code)
329+
->addViolation()
330+
;
331+
332+
return;
333+
}
334+
335+
$this
336+
->buildViolation($message, $parameters)
337+
->atPath($subPath)
338+
->addViolation()
339+
;
317340
}
318341

319342
/**
320343
* {@inheritdoc}
321344
*/
322345
public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false)
323346
{
324-
throw new BadMethodCallException(
325-
'validate() is not supported anymore as of Symfony 2.5. '.
326-
'Please use getValidator() instead or enable the legacy mode.'
327-
);
347+
trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::getValidator() method instead.', E_USER_DEPRECATED);
348+
349+
if (is_array($value)) {
350+
// The $traverse flag is ignored for arrays
351+
$constraint = new Valid(array('traverse' => true, 'deep' => $deep));
352+
353+
return $this
354+
->getValidator()
355+
->inContext($this)
356+
->atPath($subPath)
357+
->validate($value, $constraint, $groups)
358+
;
359+
}
360+
361+
if ($traverse && $value instanceof \Traversable) {
362+
$constraint = new Valid(array('traverse' => true, 'deep' => $deep));
363+
364+
return $this
365+
->getValidator()
366+
->inContext($this)
367+
->atPath($subPath)
368+
->validate($value, $constraint, $groups)
369+
;
370+
}
371+
372+
return $this
373+
->getValidator()
374+
->inContext($this)
375+
->atPath($subPath)
376+
->validate($value, null, $groups)
377+
;
328378
}
329379

330380
/**
331381
* {@inheritdoc}
332382
*/
333383
public function validateValue($value, $constraints, $subPath = '', $groups = null)
334384
{
335-
throw new BadMethodCallException(
336-
'validateValue() is not supported anymore as of Symfony 2.5. '.
337-
'Please use getValidator() instead or enable the legacy mode.'
338-
);
385+
trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::getValidator() method instead.', E_USER_DEPRECATED);
386+
387+
return $this
388+
->getValidator()
389+
->inContext($this)
390+
->atPath($subPath)
391+
->validate($value, $constraints, $groups)
392+
;
339393
}
340394

341395
/**
342396
* {@inheritdoc}
343397
*/
344398
public function getMetadataFactory()
345399
{
346-
throw new BadMethodCallException(
347-
'getMetadataFactory() is not supported anymore as of Symfony 2.5. '.
348-
'Please use getValidator() in combination with getMetadataFor() '.
349-
'or hasMetadataFor() instead or enable the legacy mode.'
350-
);
400+
trigger_error('The '.__METHOD__.' is deprecated since version 2.5 and will be removed in 3.0. Use the new Symfony\Component\Validator\Context\ExecutionContext::getValidator method in combination with Symfony\Component\Validator\Validator\ValidatorInterface::getMetadataFor or Symfony\Component\Validator\Validator\ValidatorInterface::hasMetadataFor method instead.', E_USER_DEPRECATED);
401+
402+
$validator = $this->getValidator();
403+
404+
if ($validator instanceof LegacyValidatorInterface) {
405+
return $validator->getMetadataFactory();
406+
}
407+
408+
// The ValidatorInterface extends from the deprecated MetadataFactoryInterface, so return it when we don't have the factory instance itself
409+
return $validator;
351410
}
352411

353412
/**

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

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

1212
namespace Symfony\Component\Validator\Context;
1313

14+
trigger_error('The '.__NAMESPACE__.'\LegacyExecutionContext class is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
15+
1416
use Symfony\Component\Translation\TranslatorInterface;
15-
use Symfony\Component\Validator\Constraints\Valid;
1617
use Symfony\Component\Validator\MetadataFactoryInterface;
1718
use Symfony\Component\Validator\Validator\ValidatorInterface;
1819

@@ -50,114 +51,4 @@ public function __construct(ValidatorInterface $validator, $root, MetadataFactor
5051

5152
$this->metadataFactory = $metadataFactory;
5253
}
53-
54-
/**
55-
* {@inheritdoc}
56-
*/
57-
public function addViolation($message, array $parameters = array(), $invalidValue = null, $plural = null, $code = null)
58-
{
59-
if (func_num_args() > 2) {
60-
trigger_error('The parameters $invalidValue, $plural and $code in method '.__METHOD__.' are deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::buildViolation method instead.', E_USER_DEPRECATED);
61-
62-
$this
63-
->buildViolation($message, $parameters)
64-
->setInvalidValue($invalidValue)
65-
->setPlural($plural)
66-
->setCode($code)
67-
->addViolation()
68-
;
69-
70-
return;
71-
}
72-
73-
parent::addViolation($message, $parameters);
74-
}
75-
76-
/**
77-
* {@inheritdoc}
78-
*/
79-
public function addViolationAt($subPath, $message, array $parameters = array(), $invalidValue = null, $plural = null, $code = null)
80-
{
81-
trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::buildViolation method instead.', E_USER_DEPRECATED);
82-
83-
if (func_num_args() > 2) {
84-
$this
85-
->buildViolation($message, $parameters)
86-
->atPath($subPath)
87-
->setInvalidValue($invalidValue)
88-
->setPlural($plural)
89-
->setCode($code)
90-
->addViolation()
91-
;
92-
93-
return;
94-
}
95-
96-
$this
97-
->buildViolation($message, $parameters)
98-
->atPath($subPath)
99-
->addViolation()
100-
;
101-
}
102-
103-
/**
104-
* {@inheritdoc}
105-
*/
106-
public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false)
107-
{
108-
if (is_array($value)) {
109-
// The $traverse flag is ignored for arrays
110-
$constraint = new Valid(array('traverse' => true, 'deep' => $deep));
111-
112-
return $this
113-
->getValidator()
114-
->inContext($this)
115-
->atPath($subPath)
116-
->validate($value, $constraint, $groups)
117-
;
118-
}
119-
120-
if ($traverse && $value instanceof \Traversable) {
121-
$constraint = new Valid(array('traverse' => true, 'deep' => $deep));
122-
123-
return $this
124-
->getValidator()
125-
->inContext($this)
126-
->atPath($subPath)
127-
->validate($value, $constraint, $groups)
128-
;
129-
}
130-
131-
return $this
132-
->getValidator()
133-
->inContext($this)
134-
->atPath($subPath)
135-
->validate($value, null, $groups)
136-
;
137-
}
138-
139-
/**
140-
* {@inheritdoc}
141-
*/
142-
public function validateValue($value, $constraints, $subPath = '', $groups = null)
143-
{
144-
trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::validate method instead.', E_USER_DEPRECATED);
145-
146-
return $this
147-
->getValidator()
148-
->inContext($this)
149-
->atPath($subPath)
150-
->validate($value, $constraints, $groups)
151-
;
152-
}
153-
154-
/**
155-
* {@inheritdoc}
156-
*/
157-
public function getMetadataFactory()
158-
{
159-
trigger_error('The '.__METHOD__.' is deprecated since version 2.5 and will be removed in 3.0. Use the new Symfony\Component\Validator\Context\ExecutionContext::getValidator method in combination with Symfony\Component\Validator\Validator\ValidatorInterface::getMetadataFor or Symfony\Component\Validator\Validator\ValidatorInterface::hasMetadataFor method instead.', E_USER_DEPRECATED);
160-
161-
return $this->metadataFactory;
162-
}
16354
}

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

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

1212
namespace Symfony\Component\Validator\Context;
1313

14+
trigger_error('The '.__NAMESPACE__.'\LegacyExecutionContextFactory class is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
15+
1416
use Symfony\Component\Translation\TranslatorInterface;
1517
use Symfony\Component\Validator\MetadataFactoryInterface;
1618
use Symfony\Component\Validator\Validator\ValidatorInterface;

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use Symfony\Component\Validator\ExecutionContextInterface as LegacyExecutionContextInterface;
2222
use Symfony\Component\Validator\Mapping\ClassMetadata;
2323
use Symfony\Component\Validator\Mapping\PropertyMetadata;
24-
use Symfony\Component\Validator\Tests\Fixtures\StubGlobalExecutionContext;
2524
use Symfony\Component\Validator\Validation;
2625

2726
/**
@@ -303,7 +302,10 @@ protected function buildViolation($message)
303302
return new ConstraintViolationAssertion($this->context, $message, $this->constraint);
304303
}
305304

306-
abstract protected function getApiVersion();
305+
protected function getApiVersion()
306+
{
307+
return Validation::API_VERSION_2_5;
308+
}
307309

308310
abstract protected function createValidator();
309311
}

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

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

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

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

0 commit comments

Comments
 (0)