CallbackValidator.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\ConstraintValidator;
  13. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  14. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  15. /**
  16. * Validator for Callback constraint.
  17. *
  18. * @author Bernhard Schussek <bschussek@gmail.com>
  19. */
  20. class CallbackValidator extends ConstraintValidator
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function validate($object, Constraint $constraint)
  26. {
  27. if (!$constraint instanceof Callback) {
  28. throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Callback');
  29. }
  30. if (null !== $constraint->callback && null !== $constraint->methods) {
  31. throw new ConstraintDefinitionException(
  32. 'The Callback constraint supports either the option "callback" '.
  33. 'or "methods", but not both at the same time.'
  34. );
  35. }
  36. // has to be an array so that we can differentiate between callables
  37. // and method names
  38. if (null !== $constraint->methods && !is_array($constraint->methods)) {
  39. throw new UnexpectedTypeException($constraint->methods, 'array');
  40. }
  41. $methods = $constraint->methods ?: array($constraint->callback);
  42. foreach ($methods as $method) {
  43. if ($method instanceof \Closure) {
  44. $method($object, $this->context);
  45. } elseif (is_array($method)) {
  46. if (!is_callable($method)) {
  47. if (isset($method[0]) && is_object($method[0])) {
  48. $method[0] = get_class($method[0]);
  49. }
  50. throw new ConstraintDefinitionException(sprintf('%s targeted by Callback constraint is not a valid callable', json_encode($method)));
  51. }
  52. call_user_func($method, $object, $this->context);
  53. } elseif (null !== $object) {
  54. if (!method_exists($object, $method)) {
  55. throw new ConstraintDefinitionException(sprintf('Method "%s" targeted by Callback constraint does not exist in class %s', $method, get_class($object)));
  56. }
  57. $reflMethod = new \ReflectionMethod($object, $method);
  58. if ($reflMethod->isStatic()) {
  59. $reflMethod->invoke(null, $object, $this->context);
  60. } else {
  61. $reflMethod->invoke($object, $this->context);
  62. }
  63. }
  64. }
  65. }
  66. }