4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\ExpressionLanguage\Node;
14 use Symfony\Component\ExpressionLanguage\Compiler;
17 * @author Fabien Potencier <fabien@symfony.com>
21 class GetAttrNode extends Node
23 const PROPERTY_CALL = 1;
24 const METHOD_CALL = 2;
27 public function __construct(Node $node, Node $attribute, ArrayNode $arguments, $type)
30 array('node' => $node, 'attribute' => $attribute, 'arguments' => $arguments),
31 array('type' => $type)
35 public function compile(Compiler $compiler)
37 switch ($this->attributes['type']) {
38 case self::PROPERTY_CALL:
40 ->compile($this->nodes['node'])
42 ->raw($this->nodes['attribute']->attributes['value'])
46 case self::METHOD_CALL:
48 ->compile($this->nodes['node'])
50 ->raw($this->nodes['attribute']->attributes['value'])
52 ->compile($this->nodes['arguments'])
57 case self::ARRAY_CALL:
59 ->compile($this->nodes['node'])
61 ->compile($this->nodes['attribute'])->raw(']')
67 public function evaluate($functions, $values)
69 switch ($this->attributes['type']) {
70 case self::PROPERTY_CALL:
71 $obj = $this->nodes['node']->evaluate($functions, $values);
72 if (!is_object($obj)) {
73 throw new \RuntimeException('Unable to get a property on a non-object.');
76 $property = $this->nodes['attribute']->attributes['value'];
78 return $obj->$property;
80 case self::METHOD_CALL:
81 $obj = $this->nodes['node']->evaluate($functions, $values);
82 if (!is_object($obj)) {
83 throw new \RuntimeException('Unable to get a property on a non-object.');
86 return call_user_func_array(array($obj, $this->nodes['attribute']->attributes['value']), $this->nodes['arguments']->evaluate($functions, $values));
88 case self::ARRAY_CALL:
89 $array = $this->nodes['node']->evaluate($functions, $values);
90 if (!is_array($array) && !$array instanceof \ArrayAccess) {
91 throw new \RuntimeException('Unable to get an item on a non-array.');
94 return $array[$this->nodes['attribute']->evaluate($functions, $values)];