Yaffs site version 1.1
[yaffs-website] / vendor / symfony / expression-language / Compiler.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;
13
14 /**
15  * Compiles a node to PHP code.
16  *
17  * @author Fabien Potencier <fabien@symfony.com>
18  */
19 class Compiler
20 {
21     private $source;
22     private $functions;
23
24     public function __construct(array $functions)
25     {
26         $this->functions = $functions;
27     }
28
29     public function getFunction($name)
30     {
31         return $this->functions[$name];
32     }
33
34     /**
35      * Gets the current PHP code after compilation.
36      *
37      * @return string The PHP code
38      */
39     public function getSource()
40     {
41         return $this->source;
42     }
43
44     public function reset()
45     {
46         $this->source = '';
47
48         return $this;
49     }
50
51     /**
52      * Compiles a node.
53      *
54      * @param Node\Node $node The node to compile
55      *
56      * @return $this
57      */
58     public function compile(Node\Node $node)
59     {
60         $node->compile($this);
61
62         return $this;
63     }
64
65     public function subcompile(Node\Node $node)
66     {
67         $current = $this->source;
68         $this->source = '';
69
70         $node->compile($this);
71
72         $source = $this->source;
73         $this->source = $current;
74
75         return $source;
76     }
77
78     /**
79      * Adds a raw string to the compiled code.
80      *
81      * @param string $string The string
82      *
83      * @return $this
84      */
85     public function raw($string)
86     {
87         $this->source .= $string;
88
89         return $this;
90     }
91
92     /**
93      * Adds a quoted string to the compiled code.
94      *
95      * @param string $value The string
96      *
97      * @return $this
98      */
99     public function string($value)
100     {
101         $this->source .= sprintf('"%s"', addcslashes($value, "\0\t\"\$\\"));
102
103         return $this;
104     }
105
106     /**
107      * Returns a PHP representation of a given value.
108      *
109      * @param mixed $value The value to convert
110      *
111      * @return $this
112      */
113     public function repr($value)
114     {
115         if (is_int($value) || is_float($value)) {
116             if (false !== $locale = setlocale(LC_NUMERIC, 0)) {
117                 setlocale(LC_NUMERIC, 'C');
118             }
119
120             $this->raw($value);
121
122             if (false !== $locale) {
123                 setlocale(LC_NUMERIC, $locale);
124             }
125         } elseif (null === $value) {
126             $this->raw('null');
127         } elseif (is_bool($value)) {
128             $this->raw($value ? 'true' : 'false');
129         } elseif (is_array($value)) {
130             $this->raw('array(');
131             $first = true;
132             foreach ($value as $key => $value) {
133                 if (!$first) {
134                     $this->raw(', ');
135                 }
136                 $first = false;
137                 $this->repr($key);
138                 $this->raw(' => ');
139                 $this->repr($value);
140             }
141             $this->raw(')');
142         } else {
143             $this->string($value);
144         }
145
146         return $this;
147     }
148 }