Updating Media dependent modules to versions compatible with core Media.
[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\Validator\Constraint;
16 use Symfony\Component\Validator\ConstraintValidator;
17 use Symfony\Component\Validator\Exception\RuntimeException;
18 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19
20 /**
21  * @author Fabien Potencier <fabien@symfony.com>
22  * @author Bernhard Schussek <bschussek@symfony.com>
23  */
24 class ExpressionValidator extends ConstraintValidator
25 {
26     private $expressionLanguage;
27
28     public function __construct($propertyAccessor = null, ExpressionLanguage $expressionLanguage = null)
29     {
30         $this->expressionLanguage = $expressionLanguage;
31     }
32
33     /**
34      * {@inheritdoc}
35      */
36     public function validate($value, Constraint $constraint)
37     {
38         if (!$constraint instanceof Expression) {
39             throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Expression');
40         }
41
42         $variables = array();
43         $variables['value'] = $value;
44         $variables['this'] = $this->context->getObject();
45
46         if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
47             $this->context->buildViolation($constraint->message)
48                 ->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING))
49                 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
50                 ->addViolation();
51         }
52     }
53
54     private function getExpressionLanguage()
55     {
56         if (null === $this->expressionLanguage) {
57             if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
58                 throw new RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
59             }
60             $this->expressionLanguage = new ExpressionLanguage();
61         }
62
63         return $this->expressionLanguage;
64     }
65 }