4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator;
14 use Symfony\Component\Validator\Constraints\ExpressionValidator;
17 * Default implementation of the ConstraintValidatorFactoryInterface.
19 * This enforces the convention that the validatedBy() method on any
20 * Constraint will return the class name of the ConstraintValidator that
21 * should validate the Constraint.
23 * @author Bernhard Schussek <bschussek@gmail.com>
25 class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
27 protected $validators = array();
29 private $propertyAccessor;
31 public function __construct($propertyAccessor = null)
33 $this->propertyAccessor = $propertyAccessor;
39 public function getInstance(Constraint $constraint)
41 $className = $constraint->validatedBy();
43 if (!isset($this->validators[$className])) {
44 $this->validators[$className] = 'validator.expression' === $className
45 ? new ExpressionValidator($this->propertyAccessor)
49 return $this->validators[$className];