1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
declare(strict_types=1);
namespace Laminas\ComponentInstaller;
use ArrayAccess;
use ArrayIterator;
use Countable;
use InvalidArgumentException;
use IteratorAggregate;
use OutOfRangeException;
use ReturnTypeWillChange;
use Traversable;
use function array_key_exists;
use function array_merge;
use function array_unique;
use function array_unshift;
use function count;
use function is_array;
use function iterator_to_array;
use function sprintf;
class Collection implements
ArrayAccess,
Countable,
IteratorAggregate
{
/** @var array */
protected $items;
/**
* @param iterable $items
* @throws InvalidArgumentException
*/
public function __construct($items)
{
if ($items instanceof Traversable) {
$items = iterator_to_array($items);
}
if (! is_array($items)) {
throw new InvalidArgumentException('Collections require arrays or Traversable objects');
}
$this->items = $items;
}
/**
* Factory method
*
* @param iterable $items
* @return static
*/
public static function create($items): self
{
return new static($items);
}
/**
* Cast collection to an array.
*
* @return array
*/
public function toArray()
{
return $this->items;
}
/**
* Apply a callback to each item in the collection.
*
* @return self
*/
public function each(callable $callback)
{
foreach ($this->items as $key => $item) {
$callback($item, $key);
}
return $this;
}
/**
* Reduce the collection to a single value.
*
* @param mixed $initial Initial value.
* @return mixed
*/
public function reduce(callable $callback, $initial = null)
{
$accumulator = $initial;
foreach ($this->items as $key => $item) {
$accumulator = $callback($accumulator, $item, $key);
}
return $accumulator;
}
/**
* Filter the collection using a callback.
*
* Filter callback should return true for values to keep.
*
* @return static
*/
public function filter(callable $callback)
{
return $this->reduce(function ($filtered, $item, $key) use ($callback) {
if ($callback($item, $key)) {
$filtered[$key] = $item;
}
return $filtered;
}, new static([]));
}
/**
* Filter the collection using a callback; reject any items matching the callback.
*
* Filter callback should return true for values to reject.
*
* @return static
*/
public function reject(callable $callback)
{
return $this->reduce(function ($filtered, $item, $key) use ($callback) {
if (! $callback($item, $key)) {
$filtered[$key] = $item;
}
return $filtered;
}, new static([]));
}
/**
* Transform each value in the collection.
*
* Callback should return the new value to use.
*
* @return static
*/
public function map(callable $callback)
{
return $this->reduce(function ($results, $item, $key) use ($callback) {
$results[$key] = $callback($item, $key);
return $results;
}, new static([]));
}
/**
* Return a new collection containing only unique items.
*
* @return static
*/
public function unique()
{
return new static(array_unique($this->items));
}
/**
* Merge an array of values with the current collection.
*
* @param array $values
* @return Collection
*/
public function merge(array $values)
{
$this->items = array_merge($this->items, $values);
return $this;
}
/**
* Prepend a value to the collection.
*
* @param mixed $value
* @return Collection
*/
public function prepend($value)
{
array_unshift($this->items, $value);
return $this;
}
/**
* ArrayAccess: isset()
*
* @param string|int $offset
* @return bool
*/
#[ReturnTypeWillChange]
public function offsetExists($offset)
{
return array_key_exists($offset, $this->items);
}
/**
* ArrayAccess: retrieve by key
*
* @param string|int $offset
* @return mixed
* @throws OutOfRangeException
*/
#[ReturnTypeWillChange]
public function offsetGet($offset)
{
if (! $this->offsetExists($offset)) {
throw new OutOfRangeException(sprintf(
'Offset %s does not exist in the collection',
$offset
));
}
return $this->items[$offset];
}
/**
* ArrayAccess: set by key
*
* If $offset is null, pushes the item onto the stack.
*
* @param string|int|null $offset
* @param mixed $value
* @return void
*/
#[ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
if (null === $offset) {
$this->items[] = $value;
return;
}
$this->items[$offset] = $value;
}
/**
* ArrayAccess: unset()
*
* @param string|int $offset
* @return void
*/
#[ReturnTypeWillChange]
public function offsetUnset($offset)
{
if ($this->offsetExists($offset)) {
unset($this->items[$offset]);
}
}
/**
* Countable: number of items in the collection.
*
* @return int
*/
#[ReturnTypeWillChange]
public function count()
{
return count($this->items);
}
/**
* Is the collection empty?
*
* @return bool
*/
public function isEmpty()
{
return 0 === $this->count();
}
/**
* Traversable: Iterate the collection.
*
* @return ArrayIterator
*/
#[ReturnTypeWillChange]
public function getIterator()
{
return new ArrayIterator($this->items);
}
}