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\ExpressionLanguage\Node;
14 use Symfony\Component\ExpressionLanguage\Compiler;
17 * @author Fabien Potencier <fabien@symfony.com>
21 class BinaryNode extends Node
23 private static $operators = array(
29 private static $functions = array(
33 'not in' => '!in_array',
36 public function __construct($operator, Node $left, Node $right)
39 array('left' => $left, 'right' => $right),
40 array('operator' => $operator)
44 public function compile(Compiler $compiler)
46 $operator = $this->attributes['operator'];
48 if ('matches' == $operator) {
51 ->compile($this->nodes['right'])
53 ->compile($this->nodes['left'])
60 if (isset(self::$functions[$operator])) {
62 ->raw(sprintf('%s(', self::$functions[$operator]))
63 ->compile($this->nodes['left'])
65 ->compile($this->nodes['right'])
72 if (isset(self::$operators[$operator])) {
73 $operator = self::$operators[$operator];
78 ->compile($this->nodes['left'])
82 ->compile($this->nodes['right'])
87 public function evaluate($functions, $values)
89 $operator = $this->attributes['operator'];
90 $left = $this->nodes['left']->evaluate($functions, $values);
92 if (isset(self::$functions[$operator])) {
93 $right = $this->nodes['right']->evaluate($functions, $values);
95 if ('not in' === $operator) {
96 return !in_array($left, $right);
98 $f = self::$functions[$operator];
100 return $f($left, $right);
106 return $left || $this->nodes['right']->evaluate($functions, $values);
109 return $left && $this->nodes['right']->evaluate($functions, $values);
112 $right = $this->nodes['right']->evaluate($functions, $values);
116 return $left | $right;
118 return $left ^ $right;
120 return $left & $right;
122 return $left == $right;
124 return $left === $right;
126 return $left != $right;
128 return $left !== $right;
130 return $left < $right;
132 return $left > $right;
134 return $left >= $right;
136 return $left <= $right;
138 return !in_array($left, $right);
140 return in_array($left, $right);
142 return $left + $right;
144 return $left - $right;
148 return $left * $right;
150 return $left / $right;
152 return $left % $right;
154 return preg_match($right, $left);