Completed
Push — master ( 513b92...79c3ce )
by Richard van
15s queued 11s
created

ItemList   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 91.11%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 210
ccs 41
cts 45
cp 0.9111
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A current() 0 4 1
A next() 0 4 1
A valid() 0 4 1
A rewind() 0 4 1
A key() 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 11 1
A getFirstItem() 0 14 2
A getItems() 0 14 2
A count() 0 4 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 © 2018 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 © 2018 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\OutOfBoundsException;
40
use Jkphl\Micrometa\Ports\Exceptions\RuntimeException;
41
42
/**
43
 * Item list
44
 *
45
 * @package    Jkphl\Micrometa
46
 * @subpackage Jkphl\Micrometa\Ports
47
 */
48
class ItemList extends Collection implements ItemListInterface
49
{
50
    /**
51
     * Items
52
     *
53
     * @var ItemInterface[]
54
     */
55
    protected $items;
56
57
    /**
58
     * Internal pointer
59
     *
60
     * @var int
61
     */
62
    protected $pointer;
63
64
    /**
65
     * ItemList constructor
66
     *
67
     * @param ItemInterface[] $items Items
68
     *
69
     * @api
70
     */
71 22
    public function __construct(array $items = [])
72
    {
73 22
        $this->items   = array_values($items);
74 22
        $this->pointer = 0;
75 22
    }
76
77
    /**
78
     * Return the current item
79
     *
80
     * @return ItemInterface Item
81
     * @api
82
     */
83 1
    public function current()
84
    {
85 1
        return $this->items[$this->pointer];
86
    }
87
88
    /**
89
     * Move forward to next element
90
     *
91
     * @return void
92
     * @api
93
     */
94 1
    public function next()
95
    {
96 1
        ++$this->pointer;
97 1
    }
98
99
    /**
100
     * Return the position of the current element
101
     *
102
     * @return int Position of the current element
103
     * @api
104
     */
105
    public function key()
106
    {
107
        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 1
    public function valid()
117
    {
118 1
        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 1
    public function rewind()
128
    {
129 1
        $this->pointer = 0;
130 1
    }
131
132
    /**
133
     * Test if an offset exists
134
     *
135
     * @param int $offset Offset
136
     *
137
     * @return boolean Offset exists
138
     * @api
139
     */
140
    public function offsetExists($offset)
141
    {
142
        return isset($this->items[$offset]);
143
    }
144
145
    /**
146
     * Return the item at a particular offset
147
     *
148
     * @param int $offset Offset
149
     *
150
     * @return ItemInterface Item
151
     * @api
152
     */
153 5
    public function offsetGet($offset)
154
    {
155 5
        return $this->items[$offset];
156
    }
157
158
    /**
159
     * Set an item at a particular offset
160
     *
161
     * @param int $offset          Offset
162
     * @param ItemInterface $value Item
163
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
164
     *
165
     * @api
166
     */
167 1
    public function offsetSet($offset, $value)
168
    {
169 1
        throw new RuntimeException(RuntimeException::IMMUTABLE_ITEM_LIST_STR, RuntimeException::IMMUTABLE_ITEM_LIST);
170
    }
171
172
    /**
173
     * Delete an item at a particular offset
174
     *
175
     * @param int $offset Offset
176
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
177
     */
178 1
    public function offsetUnset($offset)
179
    {
180 1
        throw new RuntimeException(RuntimeException::IMMUTABLE_ITEM_LIST_STR, RuntimeException::IMMUTABLE_ITEM_LIST);
181
    }
182
183
    /**
184
     * Return an object representation of the item list
185
     *
186
     * @return \stdClass Micro information items
187
     */
188 1
    public function toObject()
189
    {
190
        return (object)[
191 1
            'items' => array_map(
192
                function (ItemInterface $item) {
193 1
                    return $item->toObject();
194 1
                },
195 1
                $this->items
196
            )
197
        ];
198
    }
199
200
    /**
201
     * Return the first item, optionally of particular types
202
     *
203
     * @param array ...$types Item types
204
     *
205
     * @return ItemInterface Item
206
     * @throws OutOfBoundsException If there are no matching items
207
     * @api
208
     */
209 5
    public function getFirstItem(...$types)
210
    {
211 5
        $items = $this->getItems(...$types);
212
213
        // If there are no matching items
214 5
        if (!count($items)) {
215 1
            throw new OutOfBoundsException(
216 1
                OutOfBoundsException::NO_MATCHING_ITEMS_STR,
217 1
                OutOfBoundsException::NO_MATCHING_ITEMS
218
            );
219
        }
220
221 4
        return $items[0];
222
    }
223
224
    /**
225
     * Return all items as an array, optionally filtered by item type(s)
226
     *
227
     * @param array ...$types Item types
228
     *
229
     * @return ItemInterface[] Items matching the requested types
230
     * @api
231
     */
232 12
    public function getItems(...$types)
233
    {
234
        // If particular item types should be filtered
235 12
        if (count($types)) {
236 8
            return array_filter(
237 8
                $this->items,
238
                function (ItemInterface $item) use ($types) {
239 8
                    return $item->isOfType(...$types);
240 8
                }
241
            );
242
        }
243
244 8
        return $this->items;
245
    }
246
247
    /**
248
     * Return the number of items in this list
249
     *
250
     * @return int Number of items
251
     * @api
252
     */
253 3
    public function count()
254
    {
255 3
        return count($this->items);
256
    }
257
}
258