Completed
Push — v2 ( 58f91a...3b3087 )
by Joschi
05:06
created

AbstractItemList::valid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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