ConstraintValidatorFactory.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace Symfony\Component\Validator;
  3. /*
  4. * This file is part of the Symfony framework.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
  12. use Symfony\Component\Validator\Constraint;
  13. /**
  14. * Default implementation of the ConstraintValidatorFactoryInterface.
  15. *
  16. * This enforces the convention that the validatedBy() method on any
  17. * Constrain will return the class name of the ConstraintValidator that
  18. * should validate the Constraint.
  19. */
  20. class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
  21. {
  22. protected $validators = array();
  23. /**
  24. * {@inheritDoc}
  25. */
  26. public function getInstance(Constraint $constraint)
  27. {
  28. $className = $constraint->validatedBy();
  29. if (!isset($this->validators[$className])) {
  30. $this->validators[$className] = new $className();
  31. }
  32. return $this->validators[$className];
  33. }
  34. }