1 | <?php |
||
5 | class Variable implements \ArrayAccess, \Iterator, \Countable { |
||
6 | |||
7 | private $keys; |
||
8 | private $position; |
||
9 | private $data; |
||
10 | private $iteratable = false; |
||
11 | |||
12 | 25 | public static function initialize($data) { |
|
|
|||
13 | 25 | $type = gettype($data); |
|
14 | switch ($type) { |
||
15 | 25 | case 'object': |
|
16 | 25 | case 'string': |
|
17 | 24 | return new Variable($data); |
|
18 | |||
19 | 23 | case 'array': |
|
20 | 23 | return new Variable($data, array_keys($data)); |
|
21 | |||
22 | 21 | case 'NULL': |
|
23 | 20 | case 'boolean': |
|
24 | 1 | case 'integer': |
|
25 | case 'double': |
||
26 | 21 | return $data; |
|
27 | |||
28 | default: |
||
29 | throw new \ntentan\honam\ViewException("Cannot handle the $type type in templates"); |
||
30 | } |
||
31 | } |
||
32 | |||
33 | 25 | public function __construct($data, $keys = array()) { |
|
34 | 25 | if ($data instanceof Variable) { |
|
35 | 17 | $this->data = $data->getData(); |
|
36 | 17 | $this->keys = $data->getKeys(); |
|
37 | } else { |
||
38 | 25 | $this->data = $data; |
|
39 | 25 | $this->keys = $keys; |
|
40 | } |
||
41 | |||
42 | 25 | if ($this->data instanceof \Iterator) { |
|
43 | $this->iteratable = true; |
||
44 | } |
||
45 | 25 | } |
|
46 | |||
47 | 24 | public function __toString() { |
|
50 | |||
51 | 11 | public function u() { |
|
54 | |||
55 | 4 | public function count() { |
|
58 | |||
59 | 19 | public function unescape() { |
|
62 | |||
63 | 23 | public function rewind() { |
|
64 | 23 | if ($this->iteratable) { |
|
65 | $this->data->rewind(); |
||
66 | } else { |
||
67 | 23 | $this->position = 0; |
|
68 | } |
||
69 | 23 | } |
|
70 | |||
71 | 23 | public function valid() { |
|
78 | |||
79 | 22 | public function current() { |
|
86 | |||
87 | 17 | public function key() { |
|
94 | |||
95 | 22 | public function next() { |
|
96 | 22 | if ($this->iteratable) { |
|
97 | $this->data->next(); |
||
98 | } else { |
||
99 | 22 | $this->position++; |
|
100 | } |
||
101 | 22 | } |
|
102 | |||
103 | 5 | public function offsetExists($offset) { |
|
106 | |||
107 | 9 | public function offsetGet($offset) { |
|
114 | |||
115 | 1 | public function offsetSet($offset, $value) { |
|
116 | 1 | if (is_null($offset)) { |
|
117 | 1 | $this->data[] = $value; |
|
118 | } else { |
||
119 | 1 | $this->data[$offset] = $value; |
|
120 | } |
||
121 | 1 | } |
|
122 | |||
123 | public function offsetUnset($offset) { |
||
126 | |||
127 | 12 | public function __call($method, $arguments) { |
|
130 | |||
131 | public function __get($name) { |
||
134 | |||
135 | public function __debugInfo() { |
||
136 | return is_array($this->data) ? $this->data : [$this->data]; |
||
137 | } |
||
138 | |||
139 | 17 | public function getData() { |
|
142 | |||
143 | 17 | public function getKeys() { |
|
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
@return
annotation as described here.