ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Array.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of Twig.
5  *
6  * (c) Fabien Potencier
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
12 {
13  protected $index;
14 
15  public function __construct(array $elements, $lineno)
16  {
17  parent::__construct($elements, array(), $lineno);
18 
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');
23  }
24  }
25  }
26 
27  public function getKeyValuePairs()
28  {
29  $pairs = array();
30 
31  foreach (array_chunk($this->nodes, 2) as $pair) {
32  $pairs[] = array(
33  'key' => $pair[0],
34  'value' => $pair[1],
35  );
36  }
37 
38  return $pairs;
39  }
40 
42  {
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;
48  }
49  }
50 
51  return false;
52  }
53 
54  public function addElement(Twig_Node_Expression $value, Twig_Node_Expression $key = null)
55  {
56  if (null === $key) {
57  $key = new Twig_Node_Expression_Constant(++$this->index, $value->getTemplateLine());
58  }
59 
60  array_push($this->nodes, $key, $value);
61  }
62 
63  public function compile(Twig_Compiler $compiler)
64  {
65  $compiler->raw('array(');
66  $first = true;
67  foreach ($this->getKeyValuePairs() as $pair) {
68  if (!$first) {
69  $compiler->raw(', ');
70  }
71  $first = false;
72 
73  $compiler
74  ->subcompile($pair['key'])
75  ->raw(' => ')
76  ->subcompile($pair['value'])
77  ;
78  }
79  $compiler->raw(')');
80  }
81 }
82 
83 class_alias('Twig_Node_Expression_Array', 'Twig\Node\Expression\ArrayExpression', false);
$lineno
Definition: Node.php:22
raw($string)
Adds a raw string to the compiled code.
Definition: Compiler.php:112
addElement(Twig_Node_Expression $value, Twig_Node_Expression $key=null)
Definition: Array.php:54
compile(Twig_Compiler $compiler)
Compiles the node to PHP.
Definition: Array.php:63
subcompile(Twig_NodeInterface $node, $raw=true)
Definition: Compiler.php:94
hasElement(Twig_Node_Expression $key)
Definition: Array.php:41
Abstract class for all nodes that represents an expression.
Definition: Expression.php:18
getAttribute($name)
Definition: Node.php:152
getTemplateLine()
Definition: Node.php:121
Create styles array
The data for the language used.
__construct(array $elements, $lineno)
Definition: Array.php:15
$key
Definition: croninfo.php:18