1 | <?php |
||
45 | class ItemList implements ItemListInterface |
||
46 | { |
||
47 | /** |
||
48 | * Items |
||
49 | * |
||
50 | * @var ItemInterface[] |
||
51 | */ |
||
52 | protected $items; |
||
53 | |||
54 | /** |
||
55 | * Internal pointer |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | protected $pointer; |
||
60 | |||
61 | /** |
||
62 | * ItemList constructor |
||
63 | * |
||
64 | * @param ItemInterface[] $items Items |
||
65 | */ |
||
66 | 1 | public function __construct(array $items = []) |
|
71 | |||
72 | /** |
||
73 | * Return the current item |
||
74 | * |
||
75 | * @return ItemInterface Item |
||
76 | */ |
||
77 | public function current() |
||
81 | |||
82 | /** |
||
83 | * Move forward to next element |
||
84 | * |
||
85 | * @return void |
||
86 | */ |
||
87 | public function next() |
||
91 | |||
92 | /** |
||
93 | * Return the position of the current element |
||
94 | * |
||
95 | * @return int Position of the current element |
||
96 | */ |
||
97 | public function key() |
||
101 | |||
102 | /** |
||
103 | * Checks if current position is valid |
||
104 | * |
||
105 | * @return boolean The current position is valid |
||
106 | */ |
||
107 | public function valid() |
||
111 | |||
112 | /** |
||
113 | * Rewind the item list to the first element |
||
114 | * |
||
115 | * @return void |
||
116 | */ |
||
117 | public function rewind() |
||
121 | |||
122 | /** |
||
123 | * Return an object representation of the item list |
||
124 | * |
||
125 | * @return \stdClass Micro information items |
||
126 | * @api |
||
127 | */ |
||
128 | public function toObject() |
||
132 | |||
133 | /** |
||
134 | * Return a JSON representation of the item list |
||
135 | * |
||
136 | * @return string Micro information items |
||
137 | * @api |
||
138 | */ |
||
139 | public function toJson() |
||
143 | |||
144 | /** |
||
145 | * Return the first item, optionally of particular types |
||
146 | * |
||
147 | * @param array ...$types Item types |
||
148 | * @return ItemInterface Item |
||
149 | * @api |
||
150 | */ |
||
151 | 1 | public function item(...$types) |
|
152 | { |
||
153 | 1 | return $this->items(...$types)[0]; |
|
154 | } |
||
155 | |||
156 | /** |
||
157 | * Return all items as an array, optionally filtered by item type(s) |
||
158 | * |
||
159 | * @param array ...$types Item types |
||
160 | * @return ItemInterface[] Items matching the requested types |
||
161 | * @api |
||
162 | */ |
||
163 | 1 | public function items(...$types) |
|
164 | { |
||
165 | // If particular item types should be filtered |
||
166 | 1 | if (count($types)) { |
|
167 | return array_filter( |
||
168 | $this->items, |
||
169 | function (ItemInterface $item) use ($types) { |
||
170 | return $item->isOfType(...$types); |
||
171 | } |
||
172 | ); |
||
173 | } |
||
174 | |||
175 | 1 | return $this->items; |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * Filter the items by item type(s) |
||
180 | * |
||
181 | * @param array ...$types Item types |
||
182 | * @return ItemListInterface Items matching the requested types |
||
183 | * @api |
||
184 | */ |
||
185 | public function filter(...$types) |
||
189 | } |
||
190 |