composer package updates
[openemr.git] / vendor / twig / twig / lib / Twig / Node / Expression / Array.php
blob0e77bb08bca88c1199a38b4707962528989b1d35
1 <?php
3 /*
4 * This file is part of Twig.
6 * (c) Fabien Potencier
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
11 class Twig_Node_Expression_Array extends Twig_Node_Expression
13 protected $index;
15 public function __construct(array $elements, $lineno)
17 parent::__construct($elements, array(), $lineno);
19 $this->index = -1;
20 foreach ($this->getKeyValuePairs() as $pair) {
21 if ($pair['key'] instanceof Twig_Node_Expression_Constant && ctype_digit((string) $pair['key']->getAttribute('value')) && $pair['key']->getAttribute('value') > $this->index) {
22 $this->index = $pair['key']->getAttribute('value');
27 public function getKeyValuePairs()
29 $pairs = array();
31 foreach (array_chunk($this->nodes, 2) as $pair) {
32 $pairs[] = array(
33 'key' => $pair[0],
34 'value' => $pair[1],
38 return $pairs;
41 public function hasElement(Twig_Node_Expression $key)
43 foreach ($this->getKeyValuePairs() as $pair) {
44 // we compare the string representation of the keys
45 // to avoid comparing the line numbers which are not relevant here.
46 if ((string) $key === (string) $pair['key']) {
47 return true;
51 return false;
54 public function addElement(Twig_Node_Expression $value, Twig_Node_Expression $key = null)
56 if (null === $key) {
57 $key = new Twig_Node_Expression_Constant(++$this->index, $value->getTemplateLine());
60 array_push($this->nodes, $key, $value);
63 public function compile(Twig_Compiler $compiler)
65 $compiler->raw('array(');
66 $first = true;
67 foreach ($this->getKeyValuePairs() as $pair) {
68 if (!$first) {
69 $compiler->raw(', ');
71 $first = false;
73 $compiler
74 ->subcompile($pair['key'])
75 ->raw(' => ')
76 ->subcompile($pair['value'])
79 $compiler->raw(')');
83 class_alias('Twig_Node_Expression_Array', 'Twig\Node\Expression\ArrayExpression', false);