Completed
Push — master ( 151e42...9754e0 )
by Joschi
03:37
created

ApparatObjectTrait::uasort()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 9.4285
1
<?php
2
3
/**
4
 * apparat/object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Infrastructure
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Object\Infrastructure\Model\Object\Apparat\Traits;
38
39
use Apparat\Kernel\Tests\Kernel;
40
use Apparat\Object\Application\Model\Object\ApplicationObjectInterface;
41
use Apparat\Object\Domain\Model\Object\ObjectInterface;
42
use Apparat\Object\Domain\Model\Properties\InvalidArgumentException as PropertyInvalidArgumentException;
43
use Apparat\Object\Infrastructure\Model\Object\Apparat\AbstractApparatObject;
44
use Apparat\Object\Infrastructure\Model\Object\Apparat\ApparatObjectIterator;
45
use Apparat\Object\Infrastructure\Model\Object\Object;
46
use Apparat\Object\Ports\Exceptions\InvalidArgumentException;
47
48
/**
49
 * Apparat object trait
50
 *
51
 * @package Apparat\Object
52
 * @subpackage Apparat\Object\Infrastructure
53
 * @property ApplicationObjectInterface $object
54
 * @method string getIteratorClass() Gets the iterator class name for the ArrayObject
55
 * @method int getFlags() Gets the behavior flags
56
 */
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 1
        }
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)
100
    {
101
        // If the property is invalid
102 3
        if (!is_callable([$this->object, $getter])) {
103 1
            throw new InvalidArgumentException(
104 1
                sprintf('Invalid apparat object property "%s"', $property),
105
                InvalidArgumentException::INVALID_APPARAT_OBJECT_PROPERTY
106 1
            );
107
        }
108
        try {
109 2
            return $this->object->$getter(...$arguments);
110 1
        } catch (PropertyInvalidArgumentException $e) {
111 1
            return null;
112
        }
113
    }
114
115
    /**
116
     * Return whether a particular property exists
117
     *
118
     * @param string $offset Property name
119
     * @return boolean Property exists
120
     */
121 1
    public function offsetExists($offset)
122
    {
123 1
        return array_key_exists($offset, $this->mapping);
124
    }
125
126
    /**
127
     * Return a particular property
128
     *
129
     * @param string $offset Property name
130
     * @return mixed Property value
131
     * @throws InvalidArgumentException If the requested property is invalid
132
     */
133 4
    public function offsetGet($offset)
134
    {
135
        // If a known object property has been requested
136 4
        if (array_key_exists($offset, $this->mapping)) {
137 3
            $arguments = (array)$this->mapping[$offset];
138 3
            $property = array_shift($arguments);
139 3
            $getter = 'get'.ucfirst($property);
140 3
            return $this->delegateObjectGetter($property, $getter, $arguments);
141
        }
142
143 1
        throw new InvalidArgumentException(
144 1
            sprintf('Invalid apparat object property "%s"', $offset),
145
            InvalidArgumentException::INVALID_APPARAT_OBJECT_PROPERTY
146 1
        );
147
    }
148
149
    /**
150
     * Set a particular property
151
     *
152
     * @param string $offset Property name
153
     * @param mixed $value Property value
154
     * @throws InvalidArgumentException
155
     */
156 1
    public function offsetSet($offset, $value)
157
    {
158 1
        throw new InvalidArgumentException(
159 1
            sprintf('Cannot set apparat object property "%s" to value "%s"', $offset, $value),
160
            InvalidArgumentException::CANNOT_SET_APPARAT_OBJECT_PROPERTY
161 1
        );
162
    }
163
164
    /**
165
     * Unset a particular property
166
     *
167
     * @param string $offset Property name
168
     * @throws InvalidArgumentException
169
     */
170 1
    public function offsetUnset($offset)
171
    {
172 1
        throw new InvalidArgumentException(
173 1
            sprintf('Cannot unset apparat object property "%s"', $offset),
174
            InvalidArgumentException::CANNOT_UNSET_APPARAT_OBJECT_PROPERTY
175 1
        );
176
    }
177
178
    /**
179
     * Append a value
180
     *
181
     * @param mixed $value Value
182
     * @throws InvalidArgumentException
183
     */
184 1
    public function append($value)
185
    {
186 1
        throw new InvalidArgumentException(
187 1
            sprintf('Cannot append apparat object value "%s"', $value),
188
            InvalidArgumentException::CANNOT_APPEND_APPARAT_OBJECT_VALUE
189 1
        );
190
    }
191
192
    /**
193
     * Return an array copy of all object properties
194
     *
195
     * @return array Object properties
196
     */
197 1
    public function getArrayCopy()
198
    {
199 1
        $properties = array_keys($this->mapping);
200 1
        return array_combine(
201 1
            $properties,
202 1
            array_map(
203 1
                function ($property) {
204 1
                    return $this[$property];
205 1
                },
206
                $properties
207 1
            )
208 1
        );
209
    }
210
211
    /**
212
     * Return the number of object properties
213
     *
214
     * @return int Number of object properties
215
     */
216 1
    public function count()
217
    {
218 1
        return count($this->mapping);
219
    }
220
221
    /**
222
     * Sort the object properties by value
223
     *
224
     * @return void
225
     */
226 1
    public function asort()
227
    {
228
        // Do nothing
229 1
    }
230
231
    /**
232
     * Sort the entries by key
233
     * @link http://php.net/manual/en/arrayobject.ksort.php
234
     * @return void
235
     * @since 5.2.0
236
     */
237
    /**
238
     * Sort the object properties by key
239
     *
240
     * @return void
241
     */
242 1
    public function ksort()
243
    {
244
        // Do nothing
245 1
    }
246
247
    /**
248
     * Sort the object properties by user function
249
     *
250
     * @param \Closure|\Callable $compareFunction User function
251
     * @return void
252
     */
253 1
    public function uasort($compareFunction)
254
    {
255
        // Do nothing
256 1
        if (is_callable($compareFunction())) {
257
            return;
258
        }
259 1
    }
260
261
    /**
262
     * Sort the object properties by name and user function
263
     *
264
     * @param \Closure|\Callable $compareFunction User function
265
     * @return void
266
     */
267 1
    public function uksort($compareFunction)
268
    {
269
        // Do nothing
270 1
        if (is_callable($compareFunction())) {
271
            return;
272
        }
273 1
    }
274
275
    /**
276
     * Sort the object properties using a "natural order" algorithm
277
     *
278
     * @return void
279
     */
280 1
    public function natsort()
281
    {
282
        // Do nothing
283 1
    }
284
285
    /**
286
     * Sort the object properties using a case insensitive "natural order" algorithm
287
     *
288
     * @return void
289
     */
290 1
    public function natcasesort()
291
    {
292
        // Do nothing
293 1
    }
294
295
    /**
296
     * Unserialize the apparat object
297
     *
298
     * @param string $serialized Serialized apparat object
299
     */
300 1
    public function unserialize($serialized)
301
    {
302 1
        $objectUrl = unserialize($serialized);
303 1
        $this->object = Object::load($objectUrl);
304 1
    }
305
306
    /**
307
     * Exchange the associated object
308
     *
309
     * @param mixed $object Object
310
     * @return ObjectInterface Former object
311
     */
312 1
    public function exchangeArray($object)
313
    {
314
        // If a valid option was given
315 1
        if ($object instanceof ObjectInterface) {
316 1
            $formerObject = $this->object;
317 1
            $this->object = $object;
318 1
            return $formerObject;
319
        }
320
321 1
        throw new InvalidArgumentException(
322 1
            sprintf('Invalid exchange object'),
323
            InvalidArgumentException::INVALID_EXCHANGE_OBJECT
324 1
        );
325
    }
326
327
    /**
328
     * Serialize the apparat object
329
     *
330
     * @return AbstractApparatObject Serialized apparat object
331
     */
332 1
    public function serialize()
333
    {
334 1
        return serialize($this->object->getAbsoluteUrl());
335
    }
336
337
    /**
338
     * Create and return a new object iterator instance
339
     *
340
     * @return ApparatObjectIterator Object iterator instance
341
     */
342 1
    public function getIterator()
343
    {
344 1
        return Kernel::create($this->getIteratorClass(), [$this->mapping, $this->getFlags(), $this]);
345
    }
346
}
347