1 | <?php |
||
5 | abstract class Base implements \ArrayAccess, DataWrapperInterface |
||
6 | { |
||
7 | /** |
||
8 | * Base constructor - you may use an array to initialize the dataWrapper |
||
9 | * |
||
10 | * @param array $data |
||
11 | */ |
||
12 | 11 | public function __construct(array $data = array()) |
|
13 | { |
||
14 | 11 | if (!empty($data)) { |
|
15 | 7 | $ref = new \ReflectionObject($this); |
|
16 | 7 | foreach ($ref->getProperties() as $property) { |
|
17 | try { |
||
18 | 7 | $value = $this->getValue($data, $property->getName()); |
|
19 | 7 | if ($this->isDataWrapper($property)) { |
|
20 | 1 | $value = $this->toDataWrapper($value, $property); |
|
21 | 1 | } |
|
22 | 7 | $property->setAccessible(true); |
|
23 | 7 | $property->setValue($this, $value); |
|
24 | 7 | } catch (\Exception $e) { |
|
25 | // does nothing when we don't have a certain property in |
||
26 | // the $data array |
||
27 | } |
||
28 | 7 | } |
|
29 | 7 | } |
|
30 | 11 | } |
|
31 | |||
32 | /** |
||
33 | * Getter and Setters |
||
34 | * |
||
35 | * @param string $name |
||
36 | * @param array $args |
||
37 | */ |
||
38 | 5 | public function __call($name, array $args) |
|
64 | |||
65 | /** |
||
66 | * Export dataWrapper values as an array |
||
67 | * |
||
68 | * @return array |
||
69 | */ |
||
70 | 6 | public function toArray() |
|
91 | |||
92 | /** |
||
93 | * ToArray with unset null fields |
||
94 | * |
||
95 | * @return array |
||
96 | */ |
||
97 | 1 | public function toCleanArray() |
|
110 | |||
111 | /** |
||
112 | * Extract dataWrapper $property value from $data |
||
113 | * |
||
114 | * @param array $data |
||
115 | * @param string $property |
||
116 | * @return |
||
117 | */ |
||
118 | 7 | private function getValue(array $data, $property) |
|
131 | |||
132 | /** |
||
133 | * Convert from undescore to camel case |
||
134 | * |
||
135 | * @param string $name |
||
136 | * @return void |
||
137 | */ |
||
138 | 8 | private function toCamelCase($name) |
|
144 | |||
145 | /** |
||
146 | * Convert from undescore to studly caps |
||
147 | * |
||
148 | * @param string $name |
||
149 | * @return void |
||
150 | */ |
||
151 | 8 | private function toStudlyCaps($name) |
|
155 | |||
156 | /** |
||
157 | * @param string $offset |
||
158 | * @return mixed |
||
159 | */ |
||
160 | 1 | public function offsetGet($offset) |
|
165 | |||
166 | /** |
||
167 | * @param string $offset |
||
168 | * @param mixed $value |
||
169 | * @return mixed |
||
170 | */ |
||
171 | 1 | public function offsetSet($offset, $value) |
|
176 | |||
177 | /** |
||
178 | * @param string |
||
179 | * @return mixed |
||
180 | */ |
||
181 | 1 | public function offsetExists($offset) |
|
186 | |||
187 | /** |
||
188 | * @param string offset |
||
189 | * @return void |
||
190 | */ |
||
191 | 1 | public function offsetUnset($offset) |
|
195 | |||
196 | /** |
||
197 | * @param \ReflectionProperty $property |
||
198 | * @return bool |
||
199 | */ |
||
200 | 7 | public function isDataWrapper(\ReflectionProperty $property) |
|
214 | |||
215 | /** |
||
216 | * @param mixed $value |
||
217 | * @param \ReflectionProperty $property |
||
218 | * @return BaseCollection |
||
219 | */ |
||
220 | 1 | public function toDataWrapper($value, \ReflectionProperty $property) |
|
225 | |||
226 | /** |
||
227 | * @param \ReflectionProperty $property |
||
228 | * @return string |
||
229 | */ |
||
230 | 7 | private function findType(\ReflectionProperty $property) |
|
246 | } |
||
247 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: