Permalink
Browse files

added convenience methods to manipulate an array node

  • Loading branch information...
fabpot committed Dec 25, 2011
1 parent 591f982 commit b7de8e40c8e6447e76d2b57848725a6809056231
Showing with 43 additions and 14 deletions.
  1. +14 −14 lib/Twig/ExpressionParser.php
  2. +29 −0 lib/Twig/Node/Expression/Array.php
@@ -197,54 +197,54 @@ public function parseArrayExpression()
{
$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 ,?
if ($stream->test(Twig_Token::PUNCTUATION_TYPE, ']')) {
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 ,?
if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '}')) {
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)
@@ -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 @@ public function getKeyValuePairs()
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.
*

0 comments on commit b7de8e4

Please sign in to comment.