Yaffs site version 1.1
[yaffs-website] / vendor / symfony / expression-language / Node / GetAttrNode.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\ExpressionLanguage\Node;
13
14 use Symfony\Component\ExpressionLanguage\Compiler;
15
16 /**
17  * @author Fabien Potencier <fabien@symfony.com>
18  *
19  * @internal
20  */
21 class GetAttrNode extends Node
22 {
23     const PROPERTY_CALL = 1;
24     const METHOD_CALL = 2;
25     const ARRAY_CALL = 3;
26
27     public function __construct(Node $node, Node $attribute, ArrayNode $arguments, $type)
28     {
29         parent::__construct(
30             array('node' => $node, 'attribute' => $attribute, 'arguments' => $arguments),
31             array('type' => $type)
32         );
33     }
34
35     public function compile(Compiler $compiler)
36     {
37         switch ($this->attributes['type']) {
38             case self::PROPERTY_CALL:
39                 $compiler
40                     ->compile($this->nodes['node'])
41                     ->raw('->')
42                     ->raw($this->nodes['attribute']->attributes['value'])
43                 ;
44                 break;
45
46             case self::METHOD_CALL:
47                 $compiler
48                     ->compile($this->nodes['node'])
49                     ->raw('->')
50                     ->raw($this->nodes['attribute']->attributes['value'])
51                     ->raw('(')
52                     ->compile($this->nodes['arguments'])
53                     ->raw(')')
54                 ;
55                 break;
56
57             case self::ARRAY_CALL:
58                 $compiler
59                     ->compile($this->nodes['node'])
60                     ->raw('[')
61                     ->compile($this->nodes['attribute'])->raw(']')
62                 ;
63                 break;
64         }
65     }
66
67     public function evaluate($functions, $values)
68     {
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.');
74                 }
75
76                 $property = $this->nodes['attribute']->attributes['value'];
77
78                 return $obj->$property;
79
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.');
84                 }
85
86                 return call_user_func_array(array($obj, $this->nodes['attribute']->attributes['value']), $this->nodes['arguments']->evaluate($functions, $values));
87
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.');
92                 }
93
94                 return $array[$this->nodes['attribute']->evaluate($functions, $values)];
95         }
96     }
97 }