|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ntentan\honam\engines\php; |
|
4
|
|
|
|
|
5
|
|
|
class Variable implements \ArrayAccess, \Iterator, \Countable { |
|
6
|
|
|
|
|
7
|
|
|
private $keys; |
|
8
|
|
|
private $position; |
|
9
|
|
|
private $data; |
|
10
|
|
|
private $iteratable = false; |
|
11
|
|
|
|
|
12
|
|
|
public static function initialize($data) { |
|
|
|
|
|
|
13
|
|
|
$type = gettype($data); |
|
14
|
|
|
switch ($type) { |
|
15
|
|
|
case 'object': |
|
16
|
|
|
case 'string': |
|
17
|
|
|
return new Variable($data); |
|
18
|
|
|
|
|
19
|
|
|
case 'array': |
|
20
|
|
|
return new Variable($data, array_keys($data)); |
|
21
|
|
|
|
|
22
|
|
|
case 'NULL': |
|
23
|
|
|
case 'boolean': |
|
24
|
|
|
case 'integer': |
|
25
|
|
|
case 'double': |
|
26
|
|
|
return $data; |
|
27
|
|
|
|
|
28
|
|
|
default: |
|
29
|
|
|
throw new \ntentan\honam\ViewException("Cannot handle the $type type in templates"); |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function __construct($data, $keys = array()) { |
|
34
|
|
|
if ($data instanceof Variable) { |
|
35
|
|
|
$this->data = $data->getData(); |
|
36
|
|
|
$this->keys = $data->getKeys(); |
|
37
|
|
|
} else { |
|
38
|
|
|
$this->data = $data; |
|
39
|
|
|
$this->keys = $keys; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if ($this->data instanceof \Iterator) { |
|
43
|
|
|
$this->iteratable = true; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function __toString() { |
|
48
|
|
|
return Janitor::cleanHtml($this->data); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function u() { |
|
52
|
|
|
return $this->unescape(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function count() { |
|
56
|
|
|
return count($this->data); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function unescape() { |
|
60
|
|
|
return $this->data; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function rewind() { |
|
64
|
|
|
if ($this->iteratable) { |
|
65
|
|
|
$this->data->rewind(); |
|
|
|
|
|
|
66
|
|
|
} else { |
|
67
|
|
|
$this->position = 0; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function valid() { |
|
72
|
|
|
if ($this->iteratable) { |
|
73
|
|
|
return $this->data->valid(); |
|
|
|
|
|
|
74
|
|
|
} else { |
|
75
|
|
|
return @isset($this->data[$this->keys[$this->position]]); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function current() { |
|
80
|
|
|
if ($this->iteratable) { |
|
81
|
|
|
return $this->data->current(); |
|
|
|
|
|
|
82
|
|
|
} else { |
|
83
|
|
|
return Variable::initialize($this->data[$this->keys[$this->position]]); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function key() { |
|
88
|
|
|
if ($this->iteratable) { |
|
89
|
|
|
return $this->data->key(); |
|
|
|
|
|
|
90
|
|
|
} else { |
|
91
|
|
|
return $this->keys[$this->position]; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function next() { |
|
96
|
|
|
if ($this->iteratable) { |
|
97
|
|
|
$this->data->next(); |
|
|
|
|
|
|
98
|
|
|
} else { |
|
99
|
|
|
$this->position++; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function offsetExists($offset) { |
|
104
|
|
|
return isset($this->data[$offset]); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function offsetGet($offset) { |
|
108
|
|
|
if (isset($this->data[$offset])) { |
|
109
|
|
|
return Variable::initialize($this->data[$offset]); |
|
110
|
|
|
} else { |
|
111
|
|
|
return null; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function offsetSet($offset, $value) { |
|
116
|
|
|
if (is_null($offset)) { |
|
117
|
|
|
$this->data[] = $value; |
|
118
|
|
|
} else { |
|
119
|
|
|
$this->data[$offset] = $value; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function offsetUnset($offset) { |
|
124
|
|
|
unset($this->data[$offset]); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function __call($method, $arguments) { |
|
|
|
|
|
|
128
|
|
|
return Variable::initialize(call_user_func_array([$this->data, $method], $arguments)); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function __get($name) { |
|
|
|
|
|
|
132
|
|
|
return Variable::initialize($this->data->$name); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function __debugInfo() { |
|
136
|
|
|
return is_array($this->data) ? $this->data : [$this->data]; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function getData() { |
|
140
|
|
|
return $this->data; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function getKeys() { |
|
144
|
|
|
return $this->keys; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.