Version 1
[yaffs-website] / vendor / symfony / validator / Constraints / ExpressionValidator.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\Constraints;
13
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;
23
24 /**
25  * @author Fabien Potencier <fabien@symfony.com>
26  * @author Bernhard Schussek <bschussek@symfony.com>
27  */
28 class ExpressionValidator extends ConstraintValidator
29 {
30     /**
31      * @var PropertyAccessorInterface
32      */
33     private $propertyAccessor;
34
35     /**
36      * @var ExpressionLanguage
37      */
38     private $expressionLanguage;
39
40     public function __construct(PropertyAccessorInterface $propertyAccessor = null, ExpressionLanguage $expressionLanguage = null)
41     {
42         $this->propertyAccessor = $propertyAccessor;
43         $this->expressionLanguage = $expressionLanguage;
44     }
45
46     /**
47      * {@inheritdoc}
48      */
49     public function validate($value, Constraint $constraint)
50     {
51         if (!$constraint instanceof Expression) {
52             throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Expression');
53         }
54
55         $variables = array();
56
57         // Symfony 2.5+
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;
64         } else {
65             $root = $this->context->getRoot();
66             $variables['value'] = $value;
67
68             if (is_object($root)) {
69                 // Extract the object that the property belongs to from the object
70                 // graph
71                 $path = new PropertyPath($this->context->getPropertyPath());
72                 $parentPath = $path->getParent();
73                 $variables['this'] = $parentPath ? $this->getPropertyAccessor()->getValue($root, $parentPath) : $root;
74             } else {
75                 $variables['this'] = null;
76             }
77         }
78
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)
84                     ->addViolation();
85             } else {
86                 $this->buildViolation($constraint->message)
87                     ->setParameter('{{ value }}', $this->formatValue($value))
88                     ->setCode(Expression::EXPRESSION_FAILED_ERROR)
89                     ->addViolation();
90             }
91         }
92     }
93
94     private function getExpressionLanguage()
95     {
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.');
99             }
100             $this->expressionLanguage = new ExpressionLanguage();
101         }
102
103         return $this->expressionLanguage;
104     }
105
106     private function getPropertyAccessor()
107     {
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.');
111             }
112             $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
113         }
114
115         return $this->propertyAccessor;
116     }
117 }