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 ArrayNode extends Node
25 public function __construct()
30 public function addElement(Node $value, Node $key = null)
33 $key = new ConstantNode(++$this->index);
36 array_push($this->nodes, $key, $value);
40 * Compiles the node to PHP.
42 * @param Compiler $compiler A Compiler instance
44 public function compile(Compiler $compiler)
46 $compiler->raw('array(');
47 $this->compileArguments($compiler);
51 public function evaluate($functions, $values)
54 foreach ($this->getKeyValuePairs() as $pair) {
55 $result[$pair['key']->evaluate($functions, $values)] = $pair['value']->evaluate($functions, $values);
61 protected function getKeyValuePairs()
64 foreach (array_chunk($this->nodes, 2) as $pair) {
65 $pairs[] = array('key' => $pair[0], 'value' => $pair[1]);
71 protected function compileArguments(Compiler $compiler, $withKeys = true)
74 foreach ($this->getKeyValuePairs() as $pair) {
82 ->compile($pair['key'])
87 $compiler->compile($pair['value']);