4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Constraints;
14 use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
15 use Symfony\Component\PropertyAccess\PropertyAccess;
16 use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
17 use Symfony\Component\PropertyAccess\PropertyPath;
18 use Symfony\Component\Validator\Constraint;
19 use Symfony\Component\Validator\ConstraintValidator;
20 use Symfony\Component\Validator\Context\ExecutionContextInterface;
21 use Symfony\Component\Validator\Exception\RuntimeException;
22 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
25 * @author Fabien Potencier <fabien@symfony.com>
26 * @author Bernhard Schussek <bschussek@symfony.com>
28 class ExpressionValidator extends ConstraintValidator
31 * @var PropertyAccessorInterface
33 private $propertyAccessor;
36 * @var ExpressionLanguage
38 private $expressionLanguage;
40 public function __construct(PropertyAccessorInterface $propertyAccessor = null, ExpressionLanguage $expressionLanguage = null)
42 $this->propertyAccessor = $propertyAccessor;
43 $this->expressionLanguage = $expressionLanguage;
49 public function validate($value, Constraint $constraint)
51 if (!$constraint instanceof Expression) {
52 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Expression');
58 if ($this->context instanceof ExecutionContextInterface) {
59 $variables['value'] = $value;
60 $variables['this'] = $this->context->getObject();
61 } elseif (null === $this->context->getPropertyName()) {
62 $variables['value'] = $value;
63 $variables['this'] = $value;
65 $root = $this->context->getRoot();
66 $variables['value'] = $value;
68 if (is_object($root)) {
69 // Extract the object that the property belongs to from the object
71 $path = new PropertyPath($this->context->getPropertyPath());
72 $parentPath = $path->getParent();
73 $variables['this'] = $parentPath ? $this->getPropertyAccessor()->getValue($root, $parentPath) : $root;
75 $variables['this'] = null;
79 if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
80 if ($this->context instanceof ExecutionContextInterface) {
81 $this->context->buildViolation($constraint->message)
82 ->setParameter('{{ value }}', $this->formatValue($value))
83 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
86 $this->buildViolation($constraint->message)
87 ->setParameter('{{ value }}', $this->formatValue($value))
88 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
94 private function getExpressionLanguage()
96 if (null === $this->expressionLanguage) {
97 if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
98 throw new RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
100 $this->expressionLanguage = new ExpressionLanguage();
103 return $this->expressionLanguage;
106 private function getPropertyAccessor()
108 if (null === $this->propertyAccessor) {
109 if (!class_exists('Symfony\Component\PropertyAccess\PropertyAccess')) {
110 throw new RuntimeException('Unable to use expressions as the Symfony PropertyAccess component is not installed.');
112 $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
115 return $this->propertyAccessor;