465527e5b7c670caa6b58f5c3c0050b699e89a86
[yaffs-website] / vendor / symfony / expression-language / Node / ArrayNode.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 ArrayNode extends Node
22 {
23     protected $index;
24
25     public function __construct()
26     {
27         $this->index = -1;
28     }
29
30     public function addElement(Node $value, Node $key = null)
31     {
32         if (null === $key) {
33             $key = new ConstantNode(++$this->index);
34         }
35
36         array_push($this->nodes, $key, $value);
37     }
38
39     /**
40      * Compiles the node to PHP.
41      *
42      * @param Compiler $compiler A Compiler instance
43      */
44     public function compile(Compiler $compiler)
45     {
46         $compiler->raw('array(');
47         $this->compileArguments($compiler);
48         $compiler->raw(')');
49     }
50
51     public function evaluate($functions, $values)
52     {
53         $result = array();
54         foreach ($this->getKeyValuePairs() as $pair) {
55             $result[$pair['key']->evaluate($functions, $values)] = $pair['value']->evaluate($functions, $values);
56         }
57
58         return $result;
59     }
60
61     protected function getKeyValuePairs()
62     {
63         $pairs = array();
64         foreach (array_chunk($this->nodes, 2) as $pair) {
65             $pairs[] = array('key' => $pair[0], 'value' => $pair[1]);
66         }
67
68         return $pairs;
69     }
70
71     protected function compileArguments(Compiler $compiler, $withKeys = true)
72     {
73         $first = true;
74         foreach ($this->getKeyValuePairs() as $pair) {
75             if (!$first) {
76                 $compiler->raw(', ');
77             }
78             $first = false;
79
80             if ($withKeys) {
81                 $compiler
82                     ->compile($pair['key'])
83                     ->raw(' => ')
84                 ;
85             }
86
87             $compiler->compile($pair['value']);
88         }
89     }
90 }