Drupal investigation

ExpressionValidator.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\ExpressionLanguage\ExpressionLanguage;
  12. use Symfony\Component\PropertyAccess\PropertyAccess;
  13. use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
  14. use Symfony\Component\PropertyAccess\PropertyPath;
  15. use Symfony\Component\Validator\Constraint;
  16. use Symfony\Component\Validator\ConstraintValidator;
  17. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  18. use Symfony\Component\Validator\Exception\RuntimeException;
  19. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  20. /**
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. * @author Bernhard Schussek <bschussek@symfony.com>
  23. */
  24. class ExpressionValidator extends ConstraintValidator
  25. {
  26. /**
  27. * @var PropertyAccessorInterface
  28. */
  29. private $propertyAccessor;
  30. /**
  31. * @var ExpressionLanguage
  32. */
  33. private $expressionLanguage;
  34. public function __construct(PropertyAccessorInterface $propertyAccessor = null, ExpressionLanguage $expressionLanguage = null)
  35. {
  36. $this->propertyAccessor = $propertyAccessor;
  37. $this->expressionLanguage = $expressionLanguage;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function validate($value, Constraint $constraint)
  43. {
  44. if (!$constraint instanceof Expression) {
  45. throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Expression');
  46. }
  47. $variables = array();
  48. // Symfony 2.5+
  49. if ($this->context instanceof ExecutionContextInterface) {
  50. $variables['value'] = $value;
  51. $variables['this'] = $this->context->getObject();
  52. } elseif (null === $this->context->getPropertyName()) {
  53. $variables['value'] = $value;
  54. $variables['this'] = $value;
  55. } else {
  56. $root = $this->context->getRoot();
  57. $variables['value'] = $value;
  58. if (is_object($root)) {
  59. // Extract the object that the property belongs to from the object
  60. // graph
  61. $path = new PropertyPath($this->context->getPropertyPath());
  62. $parentPath = $path->getParent();
  63. $variables['this'] = $parentPath ? $this->getPropertyAccessor()->getValue($root, $parentPath) : $root;
  64. } else {
  65. $variables['this'] = null;
  66. }
  67. }
  68. if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
  69. if ($this->context instanceof ExecutionContextInterface) {
  70. $this->context->buildViolation($constraint->message)
  71. ->setParameter('{{ value }}', $this->formatValue($value))
  72. ->setCode(Expression::EXPRESSION_FAILED_ERROR)
  73. ->addViolation();
  74. } else {
  75. $this->buildViolation($constraint->message)
  76. ->setParameter('{{ value }}', $this->formatValue($value))
  77. ->setCode(Expression::EXPRESSION_FAILED_ERROR)
  78. ->addViolation();
  79. }
  80. }
  81. }
  82. private function getExpressionLanguage()
  83. {
  84. if (null === $this->expressionLanguage) {
  85. if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
  86. throw new RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
  87. }
  88. $this->expressionLanguage = new ExpressionLanguage();
  89. }
  90. return $this->expressionLanguage;
  91. }
  92. private function getPropertyAccessor()
  93. {
  94. if (null === $this->propertyAccessor) {
  95. if (!class_exists('Symfony\Component\PropertyAccess\PropertyAccess')) {
  96. throw new RuntimeException('Unable to use expressions as the Symfony PropertyAccess component is not installed.');
  97. }
  98. $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
  99. }
  100. return $this->propertyAccessor;
  101. }
  102. }