From: Fabien Potencier Date: Sun, 25 Dec 2011 21:34:12 +0000 (+0100) Subject: added convenience methods to manipulate an array node X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=b7de8e40c8e6447e76d2b57848725a6809056231;p=web%2Fkonrad%2Ftwig.git added convenience methods to manipulate an array node --- diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index bd8ba78..25289fc 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -197,10 +197,11 @@ class Twig_ExpressionParser { $stream = $this->parser->getStream(); $stream->expect(Twig_Token::PUNCTUATION_TYPE, '[', 'An array element was expected'); - $elements = array(); - $index = 0; + + $node = new Twig_Node_Expression_Array(array(), $stream->getCurrent()->getLine()); + $first = true; while (!$stream->test(Twig_Token::PUNCTUATION_TYPE, ']')) { - if (!empty($elements)) { + if (!$first) { $stream->expect(Twig_Token::PUNCTUATION_TYPE, ',', 'An array element must be followed by a comma'); // trailing ,? @@ -208,26 +209,24 @@ class Twig_ExpressionParser break; } } + $first = false; - $value = $this->parseExpression(); - $key = new Twig_Node_Expression_Constant($index, $value->getLine()); - - array_push($elements, $key, $value); - - $index += 1; + $node->addElement($this->parseExpression()); } $stream->expect(Twig_Token::PUNCTUATION_TYPE, ']', 'An opened array is not properly closed'); - return new Twig_Node_Expression_Array($elements, $stream->getCurrent()->getLine()); + return $node; } public function parseHashExpression() { $stream = $this->parser->getStream(); $stream->expect(Twig_Token::PUNCTUATION_TYPE, '{', 'A hash element was expected'); - $elements = array(); + + $node = new Twig_Node_Expression_Array(array(), $stream->getCurrent()->getLine()); + $first = true; while (!$stream->test(Twig_Token::PUNCTUATION_TYPE, '}')) { - if (!empty($elements)) { + if (!$first) { $stream->expect(Twig_Token::PUNCTUATION_TYPE, ',', 'A hash value must be followed by a comma'); // trailing ,? @@ -235,16 +234,17 @@ class Twig_ExpressionParser break; } } + $first = false; $key = $this->parseExpression(); $stream->expect(Twig_Token::PUNCTUATION_TYPE, ':', 'A hash key must be followed by a colon (:)'); $value = $this->parseExpression(); - array_push($elements, $key, $value); + $node->addElement($value, $key); } $stream->expect(Twig_Token::PUNCTUATION_TYPE, '}', 'An opened hash is not properly closed'); - return new Twig_Node_Expression_Array($elements, $stream->getCurrent()->getLine()); + return $node; } public function parsePostfixExpression($node) diff --git a/lib/Twig/Node/Expression/Array.php b/lib/Twig/Node/Expression/Array.php index 648c6d4..541915e 100644 --- a/lib/Twig/Node/Expression/Array.php +++ b/lib/Twig/Node/Expression/Array.php @@ -10,9 +10,18 @@ */ class Twig_Node_Expression_Array extends Twig_Node_Expression { + protected $index; + public function __construct(array $elements, $lineno) { parent::__construct($elements, array(), $lineno); + + $this->index = -1; + foreach ($this->getKeyValuePairs() as $pair) { + if ($pair['key'] instanceof Twig_Node_Expression_Constant && ctype_digit((string) $pair['key']->getAttribute('value')) && $pair['key']->getAttribute('value') > $this->index) { + $this->index = $pair['key']->getAttribute('value'); + } + } } public function getKeyValuePairs() @@ -29,6 +38,26 @@ class Twig_Node_Expression_Array extends Twig_Node_Expression return $pairs; } + public function hasElement(Twig_Node_Expression $key) + { + foreach ($this->getKeyValuePairs() as $pair) { + if ($key == $pair['key']) { + return true; + } + } + + return false; + } + + public function addElement(Twig_Node_Expression $value, Twig_Node_Expression $key = null) + { + if (null === $key) { + $key = new Twig_Node_Expression_Constant(++$this->index, $value->getLine()); + } + + array_push($this->nodes, $key, $value); + } + /** * Compiles the node to PHP. *