Drupal investigation

CallbackValidator.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. }