Completed
Push — master ( 316a87...f7834c )
by Joschi
03:28
created

ItemList::offsetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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.UnusedFormalParameter)
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.UnusedFormalParameter)
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
                },
192 1
                $this->items
193
            )
194
        ];
195
    }
196
197
    /**
198
     * Return the first item, optionally of particular types
199
     *
200
     * @param array ...$types Item types
201
     * @return ItemInterface Item
202
     * @throws OutOfBoundsException If there are no matching items
203
     * @api
204
     */
205 2
    public function getFirstItem(...$types)
206
    {
207 2
        $items = $this->getItems(...$types);
208
209
        // If there are no matching items
210 2
        if (!count($items)) {
211 1
            throw new OutOfBoundsException(
212 1
                OutOfBoundsException::NO_MATCHING_ITEMS_STR,
213 1
                OutOfBoundsException::NO_MATCHING_ITEMS
214
            );
215
        }
216
217 1
        return $items[0];
218
    }
219
220
    /**
221
     * Return all items as an array, optionally filtered by item type(s)
222
     *
223
     * @param array ...$types Item types
224
     * @return ItemInterface[] Items matching the requested types
225
     * @api
226
     */
227 6
    public function getItems(...$types)
228
    {
229
        // If particular item types should be filtered
230 6
        if (count($types)) {
231 4
            return array_filter(
232 4
                $this->items,
233 4
                function (ItemInterface $item) use ($types) {
234 4
                    return $item->isOfType(...$types);
235 4
                }
236
            );
237
        }
238
239 4
        return $this->items;
240
    }
241
242
    /**
243
     * Return the number of items in this list
244
     *
245
     * @return int Number of items
246
     * @api
247
     */
248 2
    public function count()
249
    {
250 2
        return count($this->items);
251
    }
252
253
    /**
254
     * Generic item getter
255
     *
256
     * @param string $type Item type
257
     * @param array $arguments Arguments
258
     * @return ItemInterface Item
259
     * @throws InvalidArgumentException If the item index is invalid
260
     * @api
261
     */
262 2
    public function __call($type, $arguments)
263
    {
264 2
        $index = 0;
265 2
        if (count($arguments)) {
266
            // If the item index is invalid
267 2
            if (!is_int($arguments[0]) || ($arguments[0] < 0)) {
268 1
                throw new InvalidArgumentException(
269 1
                    sprintf(InvalidArgumentException::INVALID_ITEM_INDEX_STR, $arguments[0]),
270 1
                    InvalidArgumentException::INVALID_ITEM_INDEX
271
                );
272
            }
273
274 2
            $index = $arguments[0];
275
        }
276
277
        // Return the item by type and index
278 2
        return $this->getItemByTypeAndIndex($type, $index);
279
    }
280
281
    /**
282
     * Return an item by type and index
283
     *
284
     * @param string $type Item type
285
     * @param int $index Item index
286
     * @return ItemInterface Item
287
     * @throws OutOfBoundsException If the item index is out of bounds
288
     */
289 2
    protected function getItemByTypeAndIndex($type, $index)
290
    {
291 2
        $typeItems = $this->getItems($type);
292
293
        // If the item index is out of bounds
294 2
        if (count($typeItems) <= $index) {
295 1
            throw new OutOfBoundsException(
296 1
                sprintf(OutOfBoundsException::INVALID_ITEM_INDEX_STR, $index),
297 1
                OutOfBoundsException::INVALID_ITEM_INDEX
298
            );
299
        }
300
301 2
        return $typeItems[$index];
302
    }
303
}
304