PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/extensions/doctrine/vendors/Symfony/Component/Validator/Constraints/CallbackValidator.php

https://bitbucket.org/NordLabs/yiidoctrine
PHP | 83 lines | 42 code | 12 blank | 29 comment | 7 complexity | baed9862ee9e347411438652070ac8a0 MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1, BSD-3-Clause
  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\UnexpectedTypeException;
  14. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  15. /**
  16. * Validator for Callback constraint
  17. *
  18. * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  19. *
  20. * @api
  21. */
  22. class CallbackValidator extends ConstraintValidator
  23. {
  24. /**
  25. * Checks if the passed value is valid.
  26. *
  27. * @param mixed $object The value that should be validated
  28. * @param Constraint $constraint The constraint for the validation
  29. *
  30. * @return Boolean Whether or not the value is valid
  31. *
  32. * @api
  33. */
  34. public function isValid($object, Constraint $constraint)
  35. {
  36. if (null === $object) {
  37. return true;
  38. }
  39. // has to be an array so that we can differentiate between callables
  40. // and method names
  41. if (!is_array($constraint->methods)) {
  42. throw new UnexpectedTypeException($constraint->methods, 'array');
  43. }
  44. $methods = $constraint->methods;
  45. $context = $this->context;
  46. // save context state
  47. $currentClass = $context->getCurrentClass();
  48. $currentProperty = $context->getCurrentProperty();
  49. $group = $context->getGroup();
  50. $propertyPath = $context->getPropertyPath();
  51. foreach ($methods as $method) {
  52. if (is_array($method) || $method instanceof \Closure) {
  53. if (!is_callable($method)) {
  54. throw new ConstraintDefinitionException(sprintf('"%s::%s" targeted by Callback constraint is not a valid callable', $method[0], $method[1]));
  55. }
  56. call_user_func($method, $object, $context);
  57. } else {
  58. if (!method_exists($object, $method)) {
  59. throw new ConstraintDefinitionException(sprintf('Method "%s" targeted by Callback constraint does not exist', $method));
  60. }
  61. $object->$method($context);
  62. }
  63. // restore context state
  64. $context->setCurrentClass($currentClass);
  65. $context->setCurrentProperty($currentProperty);
  66. $context->setGroup($group);
  67. $context->setPropertyPath($propertyPath);
  68. }
  69. return true;
  70. }
  71. }