InlineValidator.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\AdminBundle\Validator;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\Validator\Constraint;
  13. use Symfony\Component\Validator\ConstraintValidator;
  14. use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
  15. class InlineValidator extends ConstraintValidator
  16. {
  17. protected $container;
  18. /**
  19. * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
  20. * @param \Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory $constraintValidatorFactory
  21. */
  22. public function __construct(ContainerInterface $container, ConstraintValidatorFactoryInterface $constraintValidatorFactory)
  23. {
  24. $this->container = $container;
  25. $this->constraintValidatorFactory = $constraintValidatorFactory;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function validate($value, Constraint $constraint)
  31. {
  32. $errorElement = new ErrorElement(
  33. $value,
  34. $this->constraintValidatorFactory,
  35. $this->context,
  36. $this->context->getGroup()
  37. );
  38. if ($constraint->isClosure()) {
  39. $function = $constraint->getClosure();
  40. } else {
  41. if (is_string($constraint->getService())) {
  42. $service = $this->container->get($constraint->getService());
  43. } else {
  44. $service = $constraint->getService();
  45. }
  46. $function = array($service, $constraint->getMethod());
  47. }
  48. call_user_func($function, $errorElement, $value);
  49. }
  50. }