8000 bug #10042 [Expression Language] fix foo[index] (schokocappucino) · symfony/symfony@2f8d4ad · GitHub
[go: up one dir, main page]

Skip to content

Commit 2f8d4ad

Browse files
committed
bug #10042 [Expression Language] fix foo[index] (schokocappucino)
This PR was submitted for the 2.4-dev branch but it was merged into the 2.4 branch instead (closes #10042). Discussion ---------- [Expression Language] fix foo[index] | Q | A | |----|-----| | Bug fix? | yes | | New feature? | no | | BC breaks? | no | | Deprecations? | no | | Tests pass? | yes | | Fixed tickets | | | License | MIT | | Doc PR | | This patch fixes the usage of variables as key in GetAttrNode. ```php $language = new \Symfony\Component\ExpressionLanguage\ExpressionLanguage(); $language->evaluate('foo[i]', ['foo' => ['a', 'b', 'c'], 'i' => 1]); // 'b' ``` Commits ------- f657d03 [Expression Language] fix foo[index]
2 parents ac45678 + d090b76 commit 2f8d4ad

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/Symfony/Component/ExpressionLanguage/Node/GetAttrNode.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ public function evaluate($functions, $values)
8181
return call_user_func_array(array($obj, $this->nodes['attribute']->evaluate($functions, $values)), $this->nodes['arguments']->evaluate($functions, $values));
8282

8383
case self::ARRAY_CALL:
84-
$values = $this->nodes['node']->evaluate($functions, $values);
85-
if (!is_array($values) && !$values instanceof \ArrayAccess) {
84+
$array = $this->nodes['node']->evaluate($functions, $values);
85+
if (!is_array($array) && !$array instanceof \ArrayAccess) {
8686
throw new \RuntimeException('Unable to get an item on a non-array.');
8787
}
8888

89-
return $values[$this->nodes['attribute']->evaluate($functions, $values)];
89+
return $array[$this->nodes['attribute']->evaluate($functions, $values)];
9090
}
9191
}
9292
}

src/Symfony/Component/ExpressionLanguage/Tests/Node/GetAttrNodeTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function getEvaluateData()
2727
array('bar', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), $this->getArrayNode(), GetAttrNode::PROPERTY_CALL), array('foo' => new Obj())),
2828

2929
array('baz', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), $this->getArrayNode(), GetAttrNode::METHOD_CALL), array('foo' => new Obj())),
30+
array('a', new GetAttrNode(new NameNode('foo'), new NameNode('index'), $this->getArrayNode(), GetAttrNode::ARRAY_CALL), array('foo' => array('b' => 'a', 'b'), 'index' => 'b')),
3031
);
3132
}
3233

@@ -39,6 +40,7 @@ public function getCompileData()
3940
array('$foo->foo', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), $this->getArrayNode(), GetAttrNode::PROPERTY_CALL), array('foo' => new Obj())),
4041

4142
array('$foo->foo(array("b" => "a", 0 => "b"))', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), $this->getArrayNode(), GetAttrNode::METHOD_CALL), array('foo' => new Obj())),
43+
array('$foo[$index]', new GetAttrNode(new NameNode('foo'), new NameNode('index'), $this->getArrayNode(), GetAttrNode::ARRAY_CALL)),
4244
);
4345
}
4446

0 commit comments

Comments
 (0)
0