ConstraintValidatorFactory.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\Bundle\FrameworkBundle\Validator;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\Validator\Constraint;
  13. use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
  14. use Symfony\Component\Validator\ConstraintValidatorInterface;
  15. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  16. use Symfony\Component\Validator\Exception\ValidatorException;
  17. /**
  18. * Uses a service container to create constraint validators.
  19. *
  20. * A constraint validator should be tagged as "validator.constraint_validator"
  21. * in the service container and include an "alias" attribute:
  22. *
  23. * <service id="some_doctrine_validator">
  24. * <argument type="service" id="doctrine.orm.some_entity_manager" />
  25. * <tag name="validator.constraint_validator" alias="some_alias" />
  26. * </service>
  27. *
  28. * A constraint may then return this alias in its validatedBy() method:
  29. *
  30. * public function validatedBy()
  31. * {
  32. * return 'some_alias';
  33. * }
  34. *
  35. * @author Kris Wallsmith <kris@symfony.com>
  36. */
  37. class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
  38. {
  39. protected $container;
  40. protected $validators;
  41. /**
  42. * @param ContainerInterface $container The service container
  43. * @param array $validators An array of validators
  44. */
  45. public function __construct(ContainerInterface $container, array $validators = array())
  46. {
  47. $this->container = $container;
  48. $this->validators = $validators;
  49. }
  50. /**
  51. * Returns the validator for the supplied constraint.
  52. *
  53. * @return ConstraintValidatorInterface A validator for the supplied constraint
  54. *
  55. * @throws ValidatorException When the validator class does not exist
  56. * @throws UnexpectedTypeException When the validator is not an instance of ConstraintValidatorInterface
  57. */
  58. public function getInstance(Constraint $constraint)
  59. {
  60. $name = $constraint->validatedBy();
  61. if (!isset($this->validators[$name])) {
  62. if (!class_exists($name)) {
  63. throw new ValidatorException(sprintf('Constraint validator "%s" does not exist or it is not enabled. Check the "validatedBy" method in your constraint class "%s".', $name, \get_class($constraint)));
  64. }
  65. $this->validators[$name] = new $name();
  66. } elseif (\is_string($this->validators[$name])) {
  67. $this->validators[$name] = $this->container->get($this->validators[$name]);
  68. }
  69. if (!$this->validators[$name] instanceof ConstraintValidatorInterface) {
  70. throw new UnexpectedTypeException($this->validators[$name], 'Symfony\Component\Validator\ConstraintValidatorInterface');
  71. }
  72. return $this->validators[$name];
  73. }
  74. }