Completed
Push — v2 ( 3b3087...c6f6d7 )
by Joschi
04:41
created

ItemList::current()   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
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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
42
/**
43
 * Abstract item list
44
 *
45
 * @package Jkphl\Micrometa
46
 * @subpackage Jkphl\Micrometa\Ports
47
 */
48
class ItemList 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
     * @api
69
     */
70 8
    public function __construct(array $items = [])
71
    {
72 8
        $this->items = array_values($items);
73 8
        $this->pointer = 0;
74 8
    }
75
76
    /**
77
     * Return the current item
78
     *
79
     * @return ItemInterface Item
80
     * @api
81
     */
82 1
    public function current()
83
    {
84 1
        return $this->items[$this->pointer];
85
    }
86
87
    /**
88
     * Move forward to next element
89
     *
90
     * @return void
91
     * @api
92
     */
93 1
    public function next()
94
    {
95 1
        ++$this->pointer;
96 1
    }
97
98
    /**
99
     * Return the position of the current element
100
     *
101
     * @return int Position of the current element
102
     * @api
103
     */
104
    public function key()
105
    {
106
        return $this->pointer;
107
    }
108
109
    /**
110
     * Checks if current position is valid
111
     *
112
     * @return boolean The current position is valid
113
     * @api
114
     */
115 1
    public function valid()
116
    {
117 1
        return isset($this->items[$this->pointer]);
118
    }
119
120
    /**
121
     * Rewind the item list to the first element
122
     *
123
     * @return void
124
     * @api
125
     */
126 1
    public function rewind()
127
    {
128 1
        $this->pointer = 0;
129 1
    }
130
131
    /**
132
     * Return a JSON representation of the item list
133
     *
134
     * @return string Item list JSON
135
     * @api
136
     */
137
    public function toJson()
138
    {
139
        return json_encode($this->toObject(), JSON_PRETTY_PRINT);
140
    }
141
142
    /**
143
     * Return an object representation of the item list
144
     *
145
     * @return \stdClass Micro information items
146
     * @api
147
     */
148
    public function toObject()
149
    {
150
        return (object)[
151
            'items' => array_map(
152
                function (ItemInterface $item) {
153
                    return $item->toObject();
154
                }, $this->items
155
            )
156
        ];
157
    }
158
159
    /**
160
     * Return the first item, optionally of particular types
161
     *
162
     * @param array ...$types Item types
163
     * @return ItemInterface Item
164
     * @throws OutOfBoundsException If there are no matching items
165
     * @api
166
     */
167 2
    public function getFirstItem(...$types)
168
    {
169 2
        $items = $this->getItems(...$types);
170
171
        // If there are no matching items
172 2
        if (!count($items)) {
173
            throw new OutOfBoundsException(
174
                OutOfBoundsException::NO_MATCHING_ITEMS_STR,
175
                OutOfBoundsException::NO_MATCHING_ITEMS
176
            );
177
        }
178
179 2
        return $items[0];
180
    }
181
182
    /**
183
     * Return all items as an array, optionally filtered by item type(s)
184
     *
185
     * @param array ...$types Item types
186
     * @return ItemInterface[] Items matching the requested types
187
     * @api
188
     */
189 2
    public function getItems(...$types)
190
    {
191
        // If particular item types should be filtered
192 2
        if (count($types)) {
193 1
            return array_filter(
194 1
                $this->items,
195 1
                function (ItemInterface $item) use ($types) {
196 1
                    return $item->isOfType(...$types);
0 ignored issues
show
Documentation introduced by
$types is of type array<integer,array>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
197 1
                }
198
            );
199
        }
200
201 1
        return $this->items;
202
    }
203
204
    /**
205
     * Return the number of items in this list
206
     *
207
     * @return int Number of items
208
     * @api
209
     */
210 1
    public function count()
211
    {
212 1
        return count($this->items);
213
    }
214
215
    /**
216
     * Generic item getter
217
     *
218
     * @param string $type Item type
219
     * @param array $arguments Arguments
220
     * @return ItemInterface Item
221
     * @throws InvalidArgumentException If the item index is invalid
222
     * @throws OutOfBoundsException If the item index is out of bounds
223
     * @api
224
     */
225
    public function __call($type, $arguments)
226
    {
227
        $index = 0;
228
        if (count($arguments)) {
229
            // If the item index is invalid
230
            if (!is_int($arguments[0]) || ($arguments[0] < 0)) {
231
                throw new InvalidArgumentException(
232
                    sprintf(InvalidArgumentException::INVALID_ITEM_INDEX_STR, $arguments[0]),
233
                    InvalidArgumentException::INVALID_ITEM_INDEX
234
                );
235
            }
236
237
            $index = $arguments[0];
238
        }
239
240
        $typeItems = $this->getItems($type);
241
242
        // If the item index is out of bounds
243
        if (count($typeItems) <= $index) {
244
            throw new OutOfBoundsException(
245
                sprintf(OutOfBoundsException::INVALID_ITEM_INDEX_STR, $index),
246
                OutOfBoundsException::INVALID_ITEM_INDEX
247
            );
248
        }
249
250
        return $typeItems[$index];
251
    }
252
}
253