8000 [FrameworkBundle] Made ConstraintValidatorFactory aware of the legacy… · symfony/symfony@870a41a · GitHub
[go: up one dir, main page]

Skip to content

Commit 870a41a

Browse files
committed
[FrameworkBundle] Made ConstraintValidatorFactory aware of the legacy validators
1 parent 7504448 commit 870a41a

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
718718
switch ($config['api']) {
719719
case '2.4':
720720
$api = Validation::API_VERSION_2_4;
721+
$container->setParameter('validator.validator_factory.class', $container->getParameter('validator.legacy_validator_factory.class'));
721722
break;
722723
case '2.5':
723724
$api = Validation::API_VERSION_2_5;

src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<parameter key="validator.mapping.cache.apc.class">Symfony\Component\Validator\Mapping\Cache\ApcCache</parameter>
1212
<parameter key="validator.mapping.cache.prefix" />
1313
<parameter key="validator.validator_factory.class">Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory</parameter>
14+
<parameter key="validator.legacy_validator_factory.class">Symfony\Bundle\FrameworkBundle\Validator\LegacyConstraintValidatorFactory</parameter>
1415
<parameter key="validator.expression.class">Symfony\Component\Validator\Constraints\ExpressionValidator</parameter>
1516
<parameter key="validator.email.class">Symfony\Component\Validator\Constraints\EmailValidator</parameter>
1617
</parameters>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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\Bundle\FrameworkBundle\Validator;
13+
14+
use Symfony\Component\DependencyInjection\ContainerInterface;
15+
use Symfony\Component\Validator\Constraint;
16+
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
17+
use Symfony\Component\Validator\ConstraintValidatorInterface;
18+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19+
20+
/**
21+
* Like {@link ConstraintValidatorFactory}, but aware of services compatible
22+
* with the 2.4 API.
23+
*
24+
* @author Bernhard Schussek <bschussek@gmail.com>
25+
* @author Kris Wallsmith <kris@symfony.com>
26+
*
27+
* @see ConstraintValidatorFactory
28+
*/
29+
class LegacyConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
30+
{
31+
const BASE_NAMESPACE = 'Symfony\\Component\\Validator\\Constraints';
32+
33+
protected $container;
34+
protected $validators;
35+
36+
/**
37+
* Constructor.
38+
*
39+
* @param ContainerInterface $container The service container
40+
* @param array $validators An array of validators
41+
*/
42+
public func EDBE tion __construct(ContainerInterface $container, array $validators = array())
43+
{
44+
$this->container = $container;
45+
$this->validators = $validators;
46+
}
47+
48+
/**
49+
* Returns the validator for the supplied constraint.
50+
*
51+
* @param Constraint $constraint A constraint
52+
*
53+
* @return ConstraintValidatorInterface A validator for the supplied constraint
54+
*
55+
* @throws UnexpectedTypeException When the validator is not an instance of ConstraintValidatorInterface
56+
*/
57+
public function getInstance(Constraint $constraint)
58+
{
59+
$name = $constraint->validatedBy();
60+
61+
if (!isset($this->validators[$name])) {
62+
switch (get_class($constraint)) {
63+
case self::BASE_NAMESPACE.'\\All':
64+
$name = self::BASE_NAMESPACE.'\\LegacyAllValidator';
65+
break;
66+
case self::BASE_NAMESPACE.'\\Choice':
67+
$name = self::BASE_NAMESPACE.'\\LegacyChoiceValidator';
68+
break;
69+
case self::BASE_NAMESPACE.'\\Collection':
70+
$name = self::BASE_NAMESPACE.'\\LegacyCollectionValidator';
71+
break;
72+
case self::BASE_NAMESPACE.'\\Count':
73+
$name = self::BASE_NAMESPACE.'\\LegacyCountValidator';
74+
break;
75+
case self::BASE_NAMESPACE.'\\Length':
76+
$name = self::BASE_NAMESPACE.'\\LegacyLengthValidator';
77+
break;
78+
}
79+
80+
$this->validators[$name] = new $name();
81+
} elseif (is_string($this->validators[$name])) {
82+
$this->validators[$name] = $this->container->get($this->validators[$name]);
83+
}
84+
85+
if (!$this->validators[$name] instanceof ConstraintValidatorInterface) {
86+
throw new UnexpectedTypeException($this->validators[$name], 'Symfony\Component\Validator\ConstraintValidatorInterface');
87+
}
88+
89+
return $this->validators[$name];
90+
}
91+
}

0 commit comments

Comments
 (0)
0