Completed
Push — v2 ( 141320...507fed )
by Joschi
05:01
created

ItemList::offsetUnset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
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
     */
137
    public function offsetExists($offset)
138
    {
139
        return isset($this->items[$offset]);
140
    }
141
142
    /**
143
     * Return the item at a particular offset
144
     *
145
     * @param int $offset Offset
146
     * @return ItemInterface Item
147
     */
148 3
    public function offsetGet($offset)
149
    {
150 3
        return $this->items[$offset];
151
    }
152
153
    /**
154
     * Set an item at a particular offset
155
     *
156
     * @param int $offset Offset
157
     * @param ItemInterface $value Item
158
     */
159 1
    public function offsetSet($offset, $value)
160
    {
161 1
        throw new RuntimeException(RuntimeException::IMMUTABLE_ITEM_LIST_STR, RuntimeException::IMMUTABLE_ITEM_LIST);
162
    }
163
164
    /**
165
     * Delete an item at a particular offset
166
     *
167
     * @param int $offset Offset
168
     * @param ItemInterface $value Item
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
169
     */
170 1
    public function offsetUnset($offset)
171
    {
172 1
        throw new RuntimeException(RuntimeException::IMMUTABLE_ITEM_LIST_STR, RuntimeException::IMMUTABLE_ITEM_LIST);
173
    }
174
175
    /**
176
     * Return an object representation of the item list
177
     *
178
     * @return \stdClass Micro information items
179
     * @api
180
     */
181 1
    public function toObject()
182
    {
183
        return (object)[
184 1
            'items' => array_map(
185
                function (ItemInterface $item) {
186 1
                    return $item->toObject();
187 1
                }, $this->items
188
            )
189
        ];
190
    }
191
192
    /**
193
     * Return the first item, optionally of particular types
194
     *
195
     * @param array ...$types Item types
196
     * @return ItemInterface Item
197
     * @throws OutOfBoundsException If there are no matching items
198
     * @api
199
     */
200 2
    public function getFirstItem(...$types)
201
    {
202 2
        $items = $this->getItems(...$types);
203
204
        // If there are no matching items
205 2
        if (!count($items)) {
206 1
            throw new OutOfBoundsException(
207 1
                OutOfBoundsException::NO_MATCHING_ITEMS_STR,
208 1
                OutOfBoundsException::NO_MATCHING_ITEMS
209
            );
210
        }
211
212 1
        return $items[0];
213
    }
214
215
    /**
216
     * Return all items as an array, optionally filtered by item type(s)
217
     *
218
     * @param array ...$types Item types
219
     * @return ItemInterface[] Items matching the requested types
220
     * @api
221
     */
222 6
    public function getItems(...$types)
223
    {
224
        // If particular item types should be filtered
225 6
        if (count($types)) {
226 4
            return array_filter(
227 4
                $this->items,
228 4
                function (ItemInterface $item) use ($types) {
229 4
                    return $item->isOfType(...$types);
230 4
                }
231
            );
232
        }
233
234 4
        return $this->items;
235
    }
236
237
    /**
238
     * Return the number of items in this list
239
     *
240
     * @return int Number of items
241
     * @api
242
     */
243 2
    public function count()
244
    {
245 2
        return count($this->items);
246
    }
247
248
    /**
249
     * Generic item getter
250
     *
251
     * @param string $type Item type
252
     * @param array $arguments Arguments
253
     * @return ItemInterface Item
254
     * @throws InvalidArgumentException If the item index is invalid
255
     * @api
256
     */
257 2
    public function __call($type, $arguments)
258
    {
259 2
        $index = 0;
260 2
        if (count($arguments)) {
261
            // If the item index is invalid
262 2
            if (!is_int($arguments[0]) || ($arguments[0] < 0)) {
263 1
                throw new InvalidArgumentException(
264 1
                    sprintf(InvalidArgumentException::INVALID_ITEM_INDEX_STR, $arguments[0]),
265 1
                    InvalidArgumentException::INVALID_ITEM_INDEX
266
                );
267
            }
268
269 2
            $index = $arguments[0];
270
        }
271
272
        // Return the item by type and index
273 2
        return $this->getItemByTypeAndIndex($type, $index);
274
    }
275
276
    /**
277
     * Return an item by type and index
278
     *
279
     * @param string $type Item type
280
     * @param int $index Item index
281
     * @return ItemInterface Item
282
     * @throws OutOfBoundsException If the item index is out of bounds
283
     */
284 2
    protected function getItemByTypeAndIndex($type, $index)
285
    {
286 2
        $typeItems = $this->getItems($type);
287
288
        // If the item index is out of bounds
289 2
        if (count($typeItems) <= $index) {
290 1
            throw new OutOfBoundsException(
291 1
                sprintf(OutOfBoundsException::INVALID_ITEM_INDEX_STR, $index),
292 1
                OutOfBoundsException::INVALID_ITEM_INDEX
293
            );
294
        }
295
296 2
        return $typeItems[$index];
297
    }
298
}
299