|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class ArrayAccessV2 |
|
5
|
|
|
* |
|
6
|
|
|
* Simple data container |
|
7
|
|
|
* Features: |
|
8
|
|
|
* - translates property operation to container elements operation |
|
9
|
|
|
* - implements access to properties as to array |
|
10
|
|
|
* - class is traversable |
|
11
|
|
|
* - clone options: deep, shallow, none; |
|
12
|
|
|
*/ |
|
13
|
|
|
class ArrayAccessV2 implements ArrayAccess, Iterator { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* If container data need to be additionally cloned |
|
17
|
|
|
* |
|
18
|
|
|
* @var int |
|
19
|
|
|
*/ |
|
20
|
|
|
public static $_clonable = HelperArray::CLONE_DEEP; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Data container |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
public $_container = array(); |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return stdClass |
|
32
|
|
|
* |
|
33
|
|
|
* @version 41a50.9 |
|
34
|
|
|
*/ |
|
35
|
|
|
public function _createElement() { |
|
36
|
|
|
return new stdClass(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param $name |
|
42
|
|
|
* |
|
43
|
|
|
* @return bool |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __isset($name) { |
|
46
|
|
|
return array_key_exists($name, $this->_container); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param $name |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __unset($name) { |
|
53
|
|
|
unset($this->_container[$name]); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param mixed $name |
|
58
|
|
|
* |
|
59
|
|
|
* @return mixed|null |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __get($name) { |
|
62
|
|
|
return $this->__isset($name) ? $this->_container[$name] : null; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param $name |
|
67
|
|
|
* @param $value |
|
68
|
|
|
*/ |
|
69
|
|
|
public function __set($name, $value) { |
|
70
|
|
|
$this->_container[$name] = $value; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function __clone() { |
|
74
|
|
|
if (static::$_clonable == HelperArray::CLONE_NONE) { |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
HelperArray::cloneDeep($this->_container, static::$_clonable); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Whether a offset exists |
|
83
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
84
|
|
|
* |
|
85
|
|
|
* @param mixed $offset <p> |
|
86
|
|
|
* An offset to check for. |
|
87
|
|
|
* </p> |
|
88
|
|
|
* |
|
89
|
|
|
* @return boolean true on success or false on failure. |
|
90
|
|
|
* </p> |
|
91
|
|
|
* <p> |
|
92
|
|
|
* The return value will be casted to boolean if non-boolean was returned. |
|
93
|
|
|
* @since 5.0.0 |
|
94
|
|
|
*/ |
|
95
|
|
|
public function offsetExists($offset) { |
|
96
|
|
|
return $this->__isset($offset); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Offset to retrieve |
|
101
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
102
|
|
|
* |
|
103
|
|
|
* @param mixed $offset <p> |
|
104
|
|
|
* The offset to retrieve. |
|
105
|
|
|
* </p> |
|
106
|
|
|
* |
|
107
|
|
|
* @return mixed Can return all value types. |
|
108
|
|
|
* @since 5.0.0 |
|
109
|
|
|
*/ |
|
110
|
|
|
public function offsetGet($offset) { |
|
111
|
|
|
return $this->__get[$offset]; |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Offset to set |
|
116
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
117
|
|
|
* |
|
118
|
|
|
* @param mixed $offset <p> |
|
119
|
|
|
* The offset to assign the value to. |
|
120
|
|
|
* </p> |
|
121
|
|
|
* @param mixed $value <p> |
|
122
|
|
|
* The value to set. |
|
123
|
|
|
* </p> |
|
124
|
|
|
* |
|
125
|
|
|
* @return void |
|
126
|
|
|
* @since 5.0.0 |
|
127
|
|
|
*/ |
|
128
|
|
|
public function offsetSet($offset, $value) { |
|
129
|
|
|
$this->__set($offset, $value); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Offset to unset |
|
134
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
135
|
|
|
* |
|
136
|
|
|
* @param mixed $offset <p> |
|
137
|
|
|
* The offset to unset. |
|
138
|
|
|
* </p> |
|
139
|
|
|
* |
|
140
|
|
|
* @return void |
|
141
|
|
|
* @since 5.0.0 |
|
142
|
|
|
*/ |
|
143
|
|
|
public function offsetUnset($offset) { |
|
144
|
|
|
$this->__unset($offset); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Get element count in container |
|
150
|
|
|
* |
|
151
|
|
|
* @return int |
|
152
|
|
|
*/ |
|
153
|
|
|
public function count() { |
|
154
|
|
|
return count($this->_container); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Return the current element |
|
159
|
|
|
* @link http://php.net/manual/en/iterator.current.php |
|
160
|
|
|
* @return mixed Can return any type. |
|
161
|
|
|
* @since 5.0.0 |
|
162
|
|
|
*/ |
|
163
|
|
|
public function current() { |
|
164
|
|
|
return current($this->_container); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Move forward to next element |
|
169
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
|
170
|
|
|
* @return void Any returned value is ignored. |
|
171
|
|
|
* @since 5.0.0 |
|
172
|
|
|
*/ |
|
173
|
|
|
public function next() { |
|
174
|
|
|
next($this->_container); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Return the key of the current element |
|
179
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
|
180
|
|
|
* @return mixed scalar on success, or null on failure. |
|
181
|
|
|
* @since 5.0.0 |
|
182
|
|
|
*/ |
|
183
|
|
|
public function key() { |
|
184
|
|
|
return key($this->_container); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Checks if current position is valid |
|
189
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
|
190
|
|
|
* @return boolean The return value will be casted to boolean and then evaluated. |
|
191
|
|
|
* Returns true on success or false on failure. |
|
192
|
|
|
* @since 5.0.0 |
|
193
|
|
|
*/ |
|
194
|
|
|
public function valid() { |
|
195
|
|
|
return false !== current($this->_container); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Rewind the Iterator to the first element |
|
200
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
|
201
|
|
|
* @return void Any returned value is ignored. |
|
202
|
|
|
* @since 5.0.0 |
|
203
|
|
|
*/ |
|
204
|
|
|
public function rewind() { |
|
205
|
|
|
reset($this->_container); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
} |
|
209
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.