1 | <?php |
||
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 = []) |
|
76 | |||
77 | /** |
||
78 | * Return the current item |
||
79 | * |
||
80 | * @return ItemInterface Item |
||
81 | * @api |
||
82 | */ |
||
83 | 2 | public function current() |
|
87 | |||
88 | /** |
||
89 | * Move forward to next element |
||
90 | * |
||
91 | * @return void |
||
92 | * @api |
||
93 | */ |
||
94 | 2 | public function next() |
|
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() |
|
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() |
|
120 | |||
121 | /** |
||
122 | * Rewind the item list to the first element |
||
123 | * |
||
124 | * @return void |
||
125 | * @api |
||
126 | */ |
||
127 | 2 | public function rewind() |
|
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) |
||
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) |
|
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 | 5 | public function offsetSet($offset, $value) |
|
164 | 4 | { |
|
165 | 5 | 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) |
|
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 | 1 | ) |
|
191 | 1 | ]; |
|
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) |
|
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) |
|
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() |
|
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 | InvalidArgumentException::INVALID_ITEM_INDEX |
||
268 | 1 | ); |
|
269 | } |
||
270 | |||
271 | 2 | $index = $arguments[0]; |
|
272 | 2 | } |
|
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) |
|
300 | } |
||
301 |