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 | 4 | 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 | 5 | 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) |
|
162 | { |
||
163 | 1 | throw new InvalidArgumentException( |
|
164 | 1 | sprintf('Cannot set apparat object property "%s" to value "%s"', $offset, $value), |
|
165 | 1 | InvalidArgumentException::CANNOT_SET_APPARAT_OBJECT_PROPERTY |
|
166 | ); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Unset a particular property |
||
171 | * |
||
172 | * @param string $offset Property name |
||
173 | * @throws InvalidArgumentException |
||
174 | */ |
||
175 | 1 | public function offsetUnset($offset) |
|
176 | { |
||
177 | 1 | throw new InvalidArgumentException( |
|
178 | 1 | sprintf('Cannot unset apparat object property "%s"', $offset), |
|
179 | 1 | InvalidArgumentException::CANNOT_UNSET_APPARAT_OBJECT_PROPERTY |
|
180 | ); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Append a value |
||
185 | * |
||
186 | * @param mixed $value Value |
||
187 | * @throws InvalidArgumentException |
||
188 | */ |
||
189 | 1 | public function append($value) |
|
190 | { |
||
191 | 1 | throw new InvalidArgumentException( |
|
192 | 1 | sprintf('Cannot append apparat object value "%s"', $value), |
|
193 | 1 | InvalidArgumentException::CANNOT_APPEND_APPARAT_OBJECT_VALUE |
|
194 | ); |
||
195 | } |
||
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() |
|
222 | { |
||
223 | 1 | return count($this->mapping); |
|
224 | } |
||
225 | |||
226 | /** |
||
227 | * Sort the object properties by value |
||
228 | * |
||
229 | * @return void |
||
230 | */ |
||
231 | 1 | public function asort() |
|
232 | { |
||
233 | // Do nothing |
||
234 | 1 | } |
|
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() |
|
248 | { |
||
249 | // Do nothing |
||
250 | 1 | } |
|
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) |
|
259 | { |
||
260 | // Do nothing |
||
261 | 1 | if (is_callable($compareFunction())) { |
|
262 | return; |
||
263 | } |
||
264 | 1 | } |
|
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) |
|
273 | { |
||
274 | // Do nothing |
||
275 | 1 | if (is_callable($compareFunction())) { |
|
276 | return; |
||
277 | } |
||
278 | 1 | } |
|
279 | |||
280 | /** |
||
281 | * Sort the object properties using a "natural order" algorithm |
||
282 | * |
||
283 | * @return void |
||
284 | */ |
||
285 | 1 | public function natsort() |
|
286 | { |
||
287 | // Do nothing |
||
288 | 1 | } |
|
289 | |||
290 | /** |
||
291 | * Sort the object properties using a case insensitive "natural order" algorithm |
||
292 | * |
||
293 | * @return void |
||
294 | */ |
||
295 | 1 | public function natcasesort() |
|
296 | { |
||
297 | // Do nothing |
||
298 | 1 | } |
|
299 | |||
300 | /** |
||
301 | * Unserialize the apparat object |
||
302 | * |
||
303 | * @param string $serialized Serialized apparat object |
||
304 | */ |
||
305 | 1 | public function unserialize($serialized) |
|
306 | { |
||
307 | 1 | $objectUrl = unserialize($serialized); |
|
308 | 1 | $this->object = Object::load($objectUrl); |
|
309 | 1 | } |
|
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) |
|
318 | { |
||
319 | // If a valid option was given |
||
320 | 1 | if ($object instanceof ObjectInterface) { |
|
321 | 1 | $formerObject = $this->object; |
|
322 | 1 | $this->object = $object; |
|
323 | 1 | return $formerObject; |
|
324 | } |
||
325 | |||
326 | 1 | throw new InvalidArgumentException( |
|
327 | 1 | sprintf('Invalid exchange object'), |
|
328 | 1 | InvalidArgumentException::INVALID_EXCHANGE_OBJECT |
|
329 | ); |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * Serialize the apparat object |
||
334 | * |
||
335 | * @return AbstractApparatObject Serialized apparat object |
||
336 | */ |
||
337 | 1 | public function serialize() |
|
338 | { |
||
339 | 1 | return serialize($this->object->getAbsoluteUrl()); |
|
340 | } |
||
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 |