Version 1
[yaffs-website] / vendor / symfony / expression-language / Node / BinaryNode.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\ExpressionLanguage\Node;
13
14 use Symfony\Component\ExpressionLanguage\Compiler;
15
16 /**
17  * @author Fabien Potencier <fabien@symfony.com>
18  *
19  * @internal
20  */
21 class BinaryNode extends Node
22 {
23     private static $operators = array(
24         '~' => '.',
25         'and' => '&&',
26         'or' => '||',
27     );
28
29     private static $functions = array(
30         '**' => 'pow',
31         '..' => 'range',
32         'in' => 'in_array',
33         'not in' => '!in_array',
34     );
35
36     public function __construct($operator, Node $left, Node $right)
37     {
38         parent::__construct(
39             array('left' => $left, 'right' => $right),
40             array('operator' => $operator)
41         );
42     }
43
44     public function compile(Compiler $compiler)
45     {
46         $operator = $this->attributes['operator'];
47
48         if ('matches' == $operator) {
49             $compiler
50                 ->raw('preg_match(')
51                 ->compile($this->nodes['right'])
52                 ->raw(', ')
53                 ->compile($this->nodes['left'])
54                 ->raw(')')
55             ;
56
57             return;
58         }
59
60         if (isset(self::$functions[$operator])) {
61             $compiler
62                 ->raw(sprintf('%s(', self::$functions[$operator]))
63                 ->compile($this->nodes['left'])
64                 ->raw(', ')
65                 ->compile($this->nodes['right'])
66                 ->raw(')')
67             ;
68
69             return;
70         }
71
72         if (isset(self::$operators[$operator])) {
73             $operator = self::$operators[$operator];
74         }
75
76         $compiler
77             ->raw('(')
78             ->compile($this->nodes['left'])
79             ->raw(' ')
80             ->raw($operator)
81             ->raw(' ')
82             ->compile($this->nodes['right'])
83             ->raw(')')
84         ;
85     }
86
87     public function evaluate($functions, $values)
88     {
89         $operator = $this->attributes['operator'];
90         $left = $this->nodes['left']->evaluate($functions, $values);
91
92         if (isset(self::$functions[$operator])) {
93             $right = $this->nodes['right']->evaluate($functions, $values);
94
95             if ('not in' === $operator) {
96                 return !in_array($left, $right);
97             }
98             $f = self::$functions[$operator];
99
100             return $f($left, $right);
101         }
102
103         switch ($operator) {
104             case 'or':
105             case '||':
106                 return $left || $this->nodes['right']->evaluate($functions, $values);
107             case 'and':
108             case '&&':
109                 return $left && $this->nodes['right']->evaluate($functions, $values);
110         }
111
112         $right = $this->nodes['right']->evaluate($functions, $values);
113
114         switch ($operator) {
115             case '|':
116                 return $left | $right;
117             case '^':
118                 return $left ^ $right;
119             case '&':
120                 return $left & $right;
121             case '==':
122                 return $left == $right;
123             case '===':
124                 return $left === $right;
125             case '!=':
126                 return $left != $right;
127             case '!==':
128                 return $left !== $right;
129             case '<':
130                 return $left < $right;
131             case '>':
132                 return $left > $right;
133             case '>=':
134                 return $left >= $right;
135             case '<=':
136                 return $left <= $right;
137             case 'not in':
138                 return !in_array($left, $right);
139             case 'in':
140                 return in_array($left, $right);
141             case '+':
142                 return $left + $right;
143             case '-':
144                 return $left - $right;
145             case '~':
146                 return $left.$right;
147             case '*':
148                 return $left * $right;
149             case '/':
150                 return $left / $right;
151             case '%':
152                 return $left % $right;
153             case 'matches':
154                 return preg_match($right, $left);
155         }
156     }
157 }