1 | <?php |
---|
2 | |
---|
3 | require_once('Destructable.php'); |
---|
4 | |
---|
5 | class JavaScriptVariable extends Destructable { |
---|
6 | protected $variable; |
---|
7 | protected $resolved_variables; |
---|
8 | protected $global_scope; |
---|
9 | |
---|
10 | public function __construct($variable, $instantiated = FALSE) { |
---|
11 | $this->variable = $variable; |
---|
12 | } |
---|
13 | |
---|
14 | public function __destruct() { |
---|
15 | $this->mem_flush('variable', 'resolved_variables', 'global_scope'); |
---|
16 | } |
---|
17 | |
---|
18 | private function resolve() { |
---|
19 | if (!$this->variable->is_lookup()) { |
---|
20 | $this->global_scope = FALSE; |
---|
21 | $this->resolved_variables = array(); |
---|
22 | } |
---|
23 | else { |
---|
24 | foreach ($this->variable->resolve(TRUE) as $resolved) { |
---|
25 | list($global_scope, $variable) = $resolved; |
---|
26 | if ($global_scope) { |
---|
27 | $this->global_scope = TRUE; |
---|
28 | } |
---|
29 | $this->resolved_variables[] = $variable; |
---|
30 | } |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | public function value() { |
---|
35 | if (!isset($this->resolved_variables)) { |
---|
36 | $this->resolve(); |
---|
37 | } |
---|
38 | return count($this->resolved_variables) ? $this->resolved_variables[0] : NULL; |
---|
39 | } |
---|
40 | |
---|
41 | public function values() { |
---|
42 | if (!isset($this->resolved_variables)) { |
---|
43 | $this->resolve(); |
---|
44 | } |
---|
45 | return is_array($this->resolved_variables) ? $this->resolved_variables : array(); |
---|
46 | } |
---|
47 | |
---|
48 | public function type() { |
---|
49 | // TODO: Things that receive this value should reassign to aliases as appropriate |
---|
50 | return 'variable'; |
---|
51 | } |
---|
52 | |
---|
53 | public function is_global () { |
---|
54 | if (!isset($this->global_scope)) { |
---|
55 | $this->resolve(); |
---|
56 | } |
---|
57 | return !!$this->global_scope; |
---|
58 | } |
---|
59 | |
---|
60 | public function __toString() { |
---|
61 | return '(' . $this->type() . ')'; |
---|
62 | } |
---|
63 | } |
---|