Twig_Node_Expression_Array

This file is part of Twig.

Defined (1)

The class is defined in the following location(s).

/vendor/twig/twig/lib/Twig/Node/Expression/Array.php  
  1. class Twig_Node_Expression_Array extends Twig_Node_Expression 
  2. protected $index; 
  3.  
  4. public function __construct(array $elements, $lineno) 
  5. parent::__construct($elements, array(), $lineno); 
  6.  
  7. $this->index = -1; 
  8. foreach ($this->getKeyValuePairs() as $pair) { 
  9. if ($pair['key'] instanceof Twig_Node_Expression_Constant && ctype_digit((string) $pair['key']->getAttribute('value')) && $pair['key']->getAttribute('value') > $this->index) { 
  10. $this->index = $pair['key']->getAttribute('value'); 
  11.  
  12. public function getKeyValuePairs() 
  13. $pairs = array(); 
  14.  
  15. foreach (array_chunk($this->nodes, 2) as $pair) { 
  16. $pairs[] = array( 
  17. 'key' => $pair[0],  
  18. 'value' => $pair[1],  
  19. ); 
  20.  
  21. return $pairs; 
  22.  
  23. public function hasElement(Twig_Node_Expression $key) 
  24. foreach ($this->getKeyValuePairs() as $pair) { 
  25. // we compare the string representation of the keys 
  26. // to avoid comparing the line numbers which are not relevant here. 
  27. if ((string) $key === (string) $pair['key']) { 
  28. return true; 
  29.  
  30. return false; 
  31.  
  32. public function addElement(Twig_Node_Expression $value, Twig_Node_Expression $key = null) 
  33. if (null === $key) { 
  34. $key = new Twig_Node_Expression_Constant(++$this->index, $value->getTemplateLine()); 
  35.  
  36. array_push($this->nodes, $key, $value); 
  37.  
  38. public function compile(Twig_Compiler $compiler) 
  39. $compiler->raw('array('); 
  40. $first = true; 
  41. foreach ($this->getKeyValuePairs() as $pair) { 
  42. if (!$first) { 
  43. $compiler->raw(', '); 
  44. $first = false; 
  45.  
  46. $compiler 
  47. ->subcompile($pair['key']) 
  48. ->raw(' => ') 
  49. ->subcompile($pair['value']) 
  50. $compiler->raw(')');