Version 1
[yaffs-website] / vendor / symfony / expression-language / Node / UnaryNode.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 UnaryNode extends Node
22 {
23     private static $operators = array(
24         '!' => '!',
25         'not' => '!',
26         '+' => '+',
27         '-' => '-',
28     );
29
30     public function __construct($operator, Node $node)
31     {
32         parent::__construct(
33             array('node' => $node),
34             array('operator' => $operator)
35         );
36     }
37
38     public function compile(Compiler $compiler)
39     {
40         $compiler
41             ->raw('(')
42             ->raw(self::$operators[$this->attributes['operator']])
43             ->compile($this->nodes['node'])
44             ->raw(')')
45         ;
46     }
47
48     public function evaluate($functions, $values)
49     {
50         $value = $this->nodes['node']->evaluate($functions, $values);
51         switch ($this->attributes['operator']) {
52             case 'not':
53             case '!':
54                 return !$value;
55             case '-':
56                 return -$value;
57         }
58
59         return $value;
60     }
61 }