1 | <?php |
||
57 | trait ApparatObjectTrait |
||
58 | { |
||
59 | /** |
||
60 | * Property mapping |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $mapping = []; |
||
65 | |||
66 | /** |
||
67 | * Generic getter |
||
68 | * |
||
69 | * @param string $method Method name |
||
70 | * @param array $arguments Arguments |
||
71 | * @return mixed Object property value |
||
72 | * @throws \BadMethodCallException If the method is unknown |
||
73 | */ |
||
74 | 2 | public function __call($method, array $arguments) |
|
75 | { |
||
76 | // If a getter was called |
||
77 | 2 | if (!strncmp('get', $method, 3)) { |
|
78 | 2 | $property = lcfirst(substr($method, 3)); |
|
79 | 2 | if (array_key_exists($property, $this->mapping)) { |
|
80 | 1 | $arguments = (array)$this->mapping[$property]; |
|
81 | 1 | $getter = 'get'.ucfirst(array_shift($arguments)); |
|
82 | 1 | return $this->delegateObjectGetter($property, $getter, $arguments); |
|
83 | } |
||
84 | } |
||
85 | |||
86 | // If the method is unknown |
||
87 | 1 | throw new \BadMethodCallException(sprintf('Unknown apparat object method "%s()"', $method)); |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Delegate the mapped object getter |
||
92 | * |
||
93 | * @param string $property Property name |
||
94 | * @param string $getter Getter name |
||
95 | * @param array $arguments Getter arguments |
||
96 | * @return mixed Property value |
||
97 | * @throws InvalidArgumentException If the property is invalid |
||
98 | */ |
||
99 | 3 | protected function delegateObjectGetter($property, $getter, array $arguments) |
|
119 | |||
120 | /** |
||
121 | * Return whether a particular property exists |
||
122 | * |
||
123 | * @param string $offset Property name |
||
124 | * @return boolean Property exists |
||
125 | */ |
||
126 | 1 | public function offsetExists($offset) |
|
130 | |||
131 | /** |
||
132 | * Return a particular property |
||
133 | * |
||
134 | * @param string $offset Property name |
||
135 | * @return mixed Property value |
||
136 | * @throws InvalidArgumentException If the requested property is invalid |
||
137 | */ |
||
138 | 4 | public function offsetGet($offset) |
|
153 | |||
154 | /** |
||
155 | * Set a particular property |
||
156 | * |
||
157 | * @param string $offset Property name |
||
158 | * @param mixed $value Property value |
||
159 | * @throws InvalidArgumentException |
||
160 | */ |
||
161 | 1 | public function offsetSet($offset, $value) |
|
168 | |||
169 | /** |
||
170 | * Unset a particular property |
||
171 | * |
||
172 | * @param string $offset Property name |
||
173 | * @throws InvalidArgumentException |
||
174 | */ |
||
175 | 1 | public function offsetUnset($offset) |
|
182 | |||
183 | /** |
||
184 | * Append a value |
||
185 | * |
||
186 | * @param mixed $value Value |
||
187 | * @throws InvalidArgumentException |
||
188 | */ |
||
189 | 1 | public function append($value) |
|
196 | |||
197 | /** |
||
198 | * Return an array copy of all object properties |
||
199 | * |
||
200 | * @return array Object properties |
||
201 | */ |
||
202 | 1 | public function getArrayCopy() |
|
203 | { |
||
204 | 1 | $properties = array_keys($this->mapping); |
|
205 | 1 | return array_combine( |
|
206 | $properties, |
||
207 | array_map( |
||
208 | 1 | function ($property) { |
|
209 | 1 | return $this[$property]; |
|
210 | 1 | }, |
|
211 | $properties |
||
212 | ) |
||
213 | ); |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Return the number of object properties |
||
218 | * |
||
219 | * @return int Number of object properties |
||
220 | */ |
||
221 | 1 | public function count() |
|
225 | |||
226 | /** |
||
227 | * Sort the object properties by value |
||
228 | * |
||
229 | * @return void |
||
230 | */ |
||
231 | 1 | public function asort() |
|
235 | |||
236 | /** |
||
237 | * Sort the entries by key |
||
238 | * @link http://php.net/manual/en/arrayobject.ksort.php |
||
239 | * @return void |
||
240 | * @since 5.2.0 |
||
241 | */ |
||
242 | /** |
||
243 | * Sort the object properties by key |
||
244 | * |
||
245 | * @return void |
||
246 | */ |
||
247 | 1 | public function ksort() |
|
251 | |||
252 | /** |
||
253 | * Sort the object properties by user function |
||
254 | * |
||
255 | * @param \Closure|\Callable $compareFunction User function |
||
256 | * @return void |
||
257 | */ |
||
258 | 1 | public function uasort($compareFunction) |
|
265 | |||
266 | /** |
||
267 | * Sort the object properties by name and user function |
||
268 | * |
||
269 | * @param \Closure|\Callable $compareFunction User function |
||
270 | * @return void |
||
271 | */ |
||
272 | 1 | public function uksort($compareFunction) |
|
279 | |||
280 | /** |
||
281 | * Sort the object properties using a "natural order" algorithm |
||
282 | * |
||
283 | * @return void |
||
284 | */ |
||
285 | 1 | public function natsort() |
|
289 | |||
290 | /** |
||
291 | * Sort the object properties using a case insensitive "natural order" algorithm |
||
292 | * |
||
293 | * @return void |
||
294 | */ |
||
295 | 1 | public function natcasesort() |
|
299 | |||
300 | /** |
||
301 | * Unserialize the apparat object |
||
302 | * |
||
303 | * @param string $serialized Serialized apparat object |
||
304 | */ |
||
305 | 1 | public function unserialize($serialized) |
|
310 | |||
311 | /** |
||
312 | * Exchange the associated object |
||
313 | * |
||
314 | * @param mixed $object Object |
||
315 | * @return ObjectInterface Former object |
||
316 | */ |
||
317 | 1 | public function exchangeArray($object) |
|
331 | |||
332 | /** |
||
333 | * Serialize the apparat object |
||
334 | * |
||
335 | * @return AbstractApparatObject Serialized apparat object |
||
336 | */ |
||
337 | 1 | public function serialize() |
|
341 | |||
342 | /** |
||
343 | * Create and return a new object iterator instance |
||
344 | * |
||
345 | * @return ApparatObjectIterator Object iterator instance |
||
346 | */ |
||
347 | 1 | public function getIterator() |
|
351 | } |
||
352 |