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