|
13 | 13 |
|
14 | 14 | use Symfony\Component\PropertyAccess\PropertyAccess;
|
15 | 15 | use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
|
| 16 | +use Symfony\Component\Validator\Context\ExecutionContextFactory; |
| 17 | +use Symfony\Component\Validator\Context\LegacyExecutionContextFactory; |
| 18 | +use Symfony\Component\Validator\Exception\InvalidArgumentException; |
16 | 19 | use Symfony\Component\Validator\Mapping\ClassMetadataFactory;
|
17 | 20 | use Symfony\Component\Validator\Exception\ValidatorException;
|
18 | 21 | use Symfony\Component\Validator\Mapping\Loader\LoaderChain;
|
|
28 | 31 | use Doctrine\Common\Annotations\AnnotationReader;
|
29 | 32 | use Doctrine\Common\Annotations\CachedReader;
|
30 | 33 | use Doctrine\Common\Cache\ArrayCache;
|
| 34 | +use Symfony\Component\Validator\NodeTraverser\NonRecursiveNodeTraverser; |
| 35 | +use Symfony\Component\Validator\NodeVisitor\ContextUpdateVisitor; |
| 36 | +use Symfony\Component\Validator\NodeVisitor\DefaultGroupReplacingVisitor; |
| 37 | +use Symfony\Component\Validator\NodeVisitor\NodeValidationVisitor; |
| 38 | +use Symfony\Component\Validator\NodeVisitor\ObjectInitializationVisitor; |
| 39 | +use Symfony\Component\Validator\Validator as ValidatorV24; |
| 40 | +use Symfony\Component\Validator\Validator\Validator; |
| 41 | +use Symfony\Component\Validator\Validator\LegacyValidator; |
31 | 42 |
|
32 | 43 | /**
|
33 | 44 | * The default implementation of {@link ValidatorBuilderInterface}.
|
@@ -91,6 +102,11 @@ class ValidatorBuilder implements ValidatorBuilderInterface
|
91 | 102 | */
|
92 | 103 | private $propertyAccessor;
|
93 | 104 |
|
| 105 | + /** |
| 106 | + * @var integer |
| 107 | + */ |
| 108 | + private $apiVersion; |
| 109 | + |
94 | 110 | /**
|
95 | 111 | * {@inheritdoc}
|
96 | 112 | */
|
@@ -303,6 +319,32 @@ public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
|
303 | 319 | return $this;
|
304 | 320 | }
|
305 | 321 |
|
| 322 | + /** |
| 323 | + * {@inheritdoc} |
| 324 | + */ |
| 325 | + public function setApiVersion($apiVersion) |
| 326 | + { |
| 327 | + if (!($apiVersion & (Validation::API_VERSION_2_4 | Validation::API_VERSION_2_5))) { |
| 328 | + throw new InvalidArgumentException(sprintf( |
| 329 | + 'The requested API version is invalid: "%s"', |
| 330 | + $apiVersion |
| 331 | + )); |
| 332 | + } |
| 333 | + |
| 334 | + if (version_compare(PHP_VERSION, '5.3.9', '<') && $apiVersion === (Validation::API_VERSION_2_4 | Validation::API_VERSION_2_5)) { |
| 335 | + throw new InvalidArgumentException(sprintf( |
| 336 | + 'The Validator API that is compatible with both Symfony 2.4 '. |
| 337 | + 'and Symfony 2.5 can only be used on PHP 5.3.9 and higher. '. |
| 338 | + 'Your current PHP version is %s.', |
| 339 | + PHP_VERSION |
| 340 | + )); |
| 341 | + } |
| 342 | + |
| 343 | + $this->apiVersion = $apiVersion; |
| 344 | + |
| 345 | + return $this; |
| 346 | + } |
| 347 | + |
306 | 348 | /**
|
307 | 349 | * {@inheritdoc}
|
308 | 350 | */
|
@@ -347,7 +389,38 @@ public function getValidator()
|
347 | 389 | $propertyAccessor = $this->propertyAccessor ?: PropertyAccess::createPropertyAccessor();
|
348 | 390 | $validatorFactory = $this->validatorFactory ?: new ConstraintValidatorFactory($propertyAccessor);
|
349 | 391 | $translator = $this->translator ?: new DefaultTranslator();
|
| 392 | + $apiVersion = $this->apiVersion; |
| 393 | + |
| 394 | + if (null === $apiVersion) { |
| 395 | + $apiVersion = version_compare(PHP_VERSION, '5.3.9', '<') |
| 396 | + ? Validation::API_VERSION_2_4 |
| 397 | + : (Validation::API_VERSION_2_4 | Validation::API_VERSION_2_5); |
| 398 | + } |
| 399 | + |
| 400 | + if (Validation::API_VERSION_2_4 === $apiVersion) { |
| 401 | + return new ValidatorV24($metadataFactory, $validatorFactory, $translator, $this->translationDomain, $this->initializers); |
| 402 | + } |
| 403 | + |
| 404 | + $nodeTraverser = new NonRecursiveNodeTraverser($metadataFactory); |
| 405 | + $nodeValidator = new NodeValidationVisitor($nodeTraverser, $validatorFactory); |
| 406 | + |
| 407 | + if (Validation::API_VERSION_2_5 === $apiVersion) { |
| 408 | + $contextFactory = new ExecutionContextFactory($nodeValidator, $translator, $this->translationDomain); |
| 409 | + } else { |
| 410 | + $contextFactory = new LegacyExecutionContextFactory($nodeValidator, $translator, $this->translationDomain); |
| 411 | + } |
| 412 | + |
| 413 | + $nodeTraverser->addVisitor(new ContextUpdateVisitor()); |
| 414 | + if (count($this->initializers) > 0) { |
| 415 | + $nodeTraverser->addVisitor(new ObjectInitializationVisitor($this->initializers)); |
| 416 | + } |
| 417 | + $nodeTraverser->addVisitor(new DefaultGroupReplacingVisitor()); |
| 418 | + $nodeTraverser->addVisitor($nodeValidator); |
| 419 | + |
| 420 | + if (Validation::API_VERSION_2_5 === $apiVersion) { |
| 421 | + return new Validator($contextFactory, $nodeTraverser, $metadataFactory); |
| 422 | + } |
350 | 423 |
|
351 |
| - return new Validator($metadataFactory, $validatorFactory, $translator, $this->translationDomain, $this->initializers); |
| 424 | + return new LegacyValidator($contextFactory, $nodeTraverser, $metadataFactory); |
352 | 425 | }
|
353 | 426 | }
|
0 commit comments