Completed
Push — master ( 754f5c...d85481 )
by Joschi
05:03
created

ItemList   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 254
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.72%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 3
dl 0
loc 254
ccs 59
cts 61
cp 0.9672
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A current() 0 4 1
A next() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A toObject() 0 10 1
A getFirstItem() 0 14 2
A getItems() 0 14 2
A count() 0 4 1
A __call() 0 18 4
A getItemByTypeAndIndex() 0 14 2
1
<?php
2
3
/**
4
 * micrometa
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\Micrometa
8
 * @subpackage Jkphl\Micrometa\Ports\Item
9
 * @author Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright Copyright © 2017 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 © 2017 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 Jkphl\Micrometa\Ports\Item;
38
39
use Jkphl\Micrometa\Ports\Exceptions\InvalidArgumentException;
40
use Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException;
41
use Jkphl\Micrometa\Ports\Exceptions\RuntimeException;
42
43
/**
44
 * Abstract item list
45
 *
46
 * @package Jkphl\Micrometa
47
 * @subpackage Jkphl\Micrometa\Ports
48
 */
49
class ItemList implements ItemListInterface
50
{
51
    /**
52
     * Items
53
     *
54
     * @var ItemInterface[]
55
     */
56
    protected $items;
57
58
    /**
59
     * Internal pointer
60
     *
61
     * @var int
62
     */
63
    protected $pointer;
64
65
    /**
66
     * ItemList constructor
67
     *
68
     * @param ItemInterface[] $items Items
69
     * @api
70
     */
71 16
    public function __construct(array $items = [])
72
    {
73 16
        $this->items = array_values($items);
74 16
        $this->pointer = 0;
75 16
    }
76
77
    /**
78
     * Return the current item
79
     *
80
     * @return ItemInterface Item
81
     * @api
82
     */
83 2
    public function current()
84
    {
85 2
        return $this->items[$this->pointer];
86
    }
87
88
    /**
89
     * Move forward to next element
90
     *
91
     * @return void
92
     * @api
93
     */
94 2
    public function next()
95
    {
96 2
        ++$this->pointer;
97 2
    }
98
99
    /**
100
     * Return the position of the current element
101
     *
102
     * @return int Position of the current element
103
     * @api
104
     */
105 1
    public function key()
106
    {
107 1
        return $this->pointer;
108
    }
109
110
    /**
111
     * Checks if current position is valid
112
     *
113
     * @return boolean The current position is valid
114
     * @api
115
     */
116 2
    public function valid()
117
    {
118 2
        return isset($this->items[$this->pointer]);
119
    }
120
121
    /**
122
     * Rewind the item list to the first element
123
     *
124
     * @return void
125
     * @api
126
     */
127 2
    public function rewind()
128
    {
129 2
        $this->pointer = 0;
130 2
    }
131
132
    /**
133
     * Test if an offset exists
134
     *
135
     * @param int $offset Offset
136
     * @return boolean Offset exists
137
     * @api
138
     */
139
    public function offsetExists($offset)
140
    {
141
        return isset($this->items[$offset]);
142
    }
143
144
    /**
145
     * Return the item at a particular offset
146
     *
147
     * @param int $offset Offset
148
     * @return ItemInterface Item
149
     * @api
150
     */
151 3
    public function offsetGet($offset)
152
    {
153 3
        return $this->items[$offset];
154
    }
155
156
    /**
157
     * Set an item at a particular offset
158
     *
159
     * @param int $offset Offset
160
     * @param ItemInterface $value Item
161
     * @SuppressWarnings(PHPMD.FnusedFormalParameter)
162
     * @api
163
     */
164 1
    public function offsetSet($offset, $value)
165
    {
166 1
        throw new RuntimeException(RuntimeException::IMMUTABLE_ITEM_LIST_STR, RuntimeException::IMMUTABLE_ITEM_LIST);
167
    }
168
169
    /**
170
     * Delete an item at a particular offset
171
     *
172
     * @param int $offset Offset
173
     * @SuppressWarnings(PHPMD.FnusedFormalParameter)
174
     */
175 1
    public function offsetUnset($offset)
176
    {
177 1
        throw new RuntimeException(RuntimeException::IMMUTABLE_ITEM_LIST_STR, RuntimeException::IMMUTABLE_ITEM_LIST);
178
    }
179
180
    /**
181
     * Return an object representation of the item list
182
     *
183
     * @return \stdClass Micro information items
184
     */
185 1
    public function toObject()
186
    {
187
        return (object)[
188 1
            'items' => array_map(
189
                function (ItemInterface $item) {
190 1
                    return $item->toObject();
191 1
                }, $this->items
192
            )
193
        ];
194
    }
195
196
    /**
197
     * Return the first item, optionally of particular types
198
     *
199
     * @param array ...$types Item types
200
     * @return ItemInterface Item
201
     * @throws OutOfBoundsException If there are no matching items
202
     * @api
203
     */
204 2
    public function getFirstItem(...$types)
205
    {
206 2
        $items = $this->getItems(...$types);
207
208
        // If there are no matching items
209 2
        if (!count($items)) {
210 1
            throw new OutOfBoundsException(
211 1
                OutOfBoundsException::NO_MATCHING_ITEMS_STR,
212 1
                OutOfBoundsException::NO_MATCHING_ITEMS
213
            );
214
        }
215
216 1
        return $items[0];
217
    }
218
219
    /**
220
     * Return all items as an array, optionally filtered by item type(s)
221
     *
222
     * @param array ...$types Item types
223
     * @return ItemInterface[] Items matching the requested types
224
     * @api
225
     */
226 6
    public function getItems(...$types)
227
    {
228
        // If particular item types should be filtered
229 6
        if (count($types)) {
230 4
            return array_filter(
231 4
                $this->items,
232 4
                function (ItemInterface $item) use ($types) {
233 4
                    return $item->isOfType(...$types);
234 4
                }
235
            );
236
        }
237
238 4
        return $this->items;
239
    }
240
241
    /**
242
     * Return the number of items in this list
243
     *
244
     * @return int Number of items
245
     * @api
246
     */
247 2
    public function count()
248
    {
249 2
        return count($this->items);
250
    }
251
252
    /**
253
     * Generic item getter
254
     *
255
     * @param string $type Item type
256
     * @param array $arguments Arguments
257
     * @return ItemInterface Item
258
     * @throws InvalidArgumentException If the item index is invalid
259
     * @api
260
     */
261 2
    public function __call($type, $arguments)
262
    {
263 2
        $index = 0;
264 2
        if (count($arguments)) {
265
            // If the item index is invalid
266 2
            if (!is_int($arguments[0]) || ($arguments[0] < 0)) {
267 1
                throw new InvalidArgumentException(
268 1
                    sprintf(InvalidArgumentException::INVALID_ITEM_INDEX_STR, $arguments[0]),
269 1
                    InvalidArgumentException::INVALID_ITEM_INDEX
270
                );
271
            }
272
273 2
            $index = $arguments[0];
274
        }
275
276
        // Return the item by type and index
277 2
        return $this->getItemByTypeAndIndex($type, $index);
278
    }
279
280
    /**
281
     * Return an item by type and index
282
     *
283
     * @param string $type Item type
284
     * @param int $index Item index
285
     * @return ItemInterface Item
286
     * @throws OutOfBoundsException If the item index is out of bounds
287
     */
288 2
    protected function getItemByTypeAndIndex($type, $index)
289
    {
290 2
        $typeItems = $this->getItems($type);
291
292
        // If the item index is out of bounds
293 2
        if (count($typeItems) <= $index) {
294 1
            throw new OutOfBoundsException(
295 1
                sprintf(OutOfBoundsException::INVALID_ITEM_INDEX_STR, $index),
296 1
                OutOfBoundsException::INVALID_ITEM_INDEX
297
            );
298
        }
299
300 2
        return $typeItems[$index];
301
    }
302
}
303