ConstraintValidatorFactory.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator;
  11. use Symfony\Component\Validator\Constraints\ExpressionValidator;
  12. /**
  13. * Default implementation of the ConstraintValidatorFactoryInterface.
  14. *
  15. * This enforces the convention that the validatedBy() method on any
  16. * Constraint will return the class name of the ConstraintValidator that
  17. * should validate the Constraint.
  18. *
  19. * @author Bernhard Schussek <bschussek@gmail.com>
  20. */
  21. class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
  22. {
  23. protected $validators = [];
  24. public function __construct()
  25. {
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function getInstance(Constraint $constraint)
  31. {
  32. $className = $constraint->validatedBy();
  33. if (!isset($this->validators[$className])) {
  34. $this->validators[$className] = 'validator.expression' === $className
  35. ? new ExpressionValidator()
  36. : new $className();
  37. }
  38. return $this->validators[$className];
  39. }
  40. }