1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CCT\Component\Rest\Collection; |
4
|
|
|
|
5
|
|
|
class ArrayCollection implements CollectionInterface |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* An array containing the entries of this collection. |
9
|
|
|
* |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
private $elements; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Initializes a new ArrayCollection. |
16
|
|
|
* |
17
|
|
|
* @param array $elements |
18
|
|
|
*/ |
19
|
19 |
|
public function __construct(array $elements = []) |
20
|
|
|
{ |
21
|
19 |
|
$this->elements = $elements; |
22
|
19 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Creates a new instance from the specified elements. |
26
|
|
|
* |
27
|
|
|
* This method is provided for derived classes to specify how a new |
28
|
|
|
* instance should be created when constructor semantics have changed. |
29
|
|
|
* |
30
|
|
|
* @param array $elements Elements. |
31
|
|
|
* |
32
|
|
|
* @return static |
33
|
|
|
*/ |
34
|
|
|
protected function createFrom(array $elements) |
35
|
|
|
{ |
36
|
|
|
return new static($elements); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
5 |
|
public function toArray() |
43
|
|
|
{ |
44
|
5 |
|
return $this->elements; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function first() |
51
|
|
|
{ |
52
|
|
|
return reset($this->elements); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function last() |
59
|
|
|
{ |
60
|
|
|
return end($this->elements); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function key() |
67
|
|
|
{ |
68
|
|
|
return key($this->elements); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function next() |
75
|
|
|
{ |
76
|
|
|
return next($this->elements); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
public function current() |
83
|
|
|
{ |
84
|
|
|
return current($this->elements); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
public function remove($key) |
91
|
|
|
{ |
92
|
|
|
if (!isset($this->elements[$key]) && !array_key_exists($key, $this->elements)) { |
93
|
|
|
return null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$removed = $this->elements[$key]; |
97
|
|
|
unset($this->elements[$key]); |
98
|
|
|
|
99
|
|
|
return $removed; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function removeElement($element) |
106
|
|
|
{ |
107
|
|
|
$key = array_search($element, $this->elements, true); |
108
|
|
|
|
109
|
|
|
if ($key === false) { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
unset($this->elements[$key]); |
114
|
|
|
|
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Required by interface ArrayAccess. |
120
|
|
|
* |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
public function offsetExists($offset) |
124
|
|
|
{ |
125
|
|
|
return $this->containsKey($offset); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Required by interface ArrayAccess. |
130
|
|
|
* |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
|
|
public function offsetGet($offset) |
134
|
|
|
{ |
135
|
|
|
return $this->get($offset); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Required by interface ArrayAccess. |
140
|
|
|
* |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
|
|
public function offsetSet($offset, $value) |
144
|
|
|
{ |
145
|
|
|
if (!isset($offset)) { |
146
|
|
|
$this->add($value); |
147
|
|
|
return; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$this->set($offset, $value); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Required by interface ArrayAccess. |
155
|
|
|
* |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
|
|
public function offsetUnset($offset) |
159
|
|
|
{ |
160
|
|
|
$this->remove($offset); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
19 |
|
public function containsKey($key) |
167
|
|
|
{ |
168
|
19 |
|
return isset($this->elements[$key]) || array_key_exists($key, $this->elements); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* {@inheritdoc} |
173
|
|
|
*/ |
174
|
|
|
public function contains($element) |
175
|
|
|
{ |
176
|
|
|
return in_array($element, $this->elements, true); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritdoc} |
181
|
|
|
*/ |
182
|
|
|
public function exists(\Closure $p) |
183
|
|
|
{ |
184
|
|
|
foreach ($this->elements as $key => $element) { |
185
|
|
|
if ($p($key, $element)) { |
186
|
|
|
return true; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return false; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* {@inheritdoc} |
195
|
|
|
*/ |
196
|
|
|
public function indexOf($element) |
197
|
|
|
{ |
198
|
|
|
return array_search($element, $this->elements, true); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* {@inheritdoc} |
203
|
|
|
*/ |
204
|
19 |
|
public function get($key, $default = null) |
205
|
|
|
{ |
206
|
19 |
|
return $this->containsKey($key) |
207
|
19 |
|
? $this->elements[$key] |
208
|
19 |
|
: $default; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* {@inheritdoc} |
213
|
|
|
*/ |
214
|
|
|
public function getKeys() |
215
|
|
|
{ |
216
|
|
|
return array_keys($this->elements); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* {@inheritdoc} |
221
|
|
|
*/ |
222
|
|
|
public function getValues() |
223
|
|
|
{ |
224
|
|
|
return array_values($this->elements); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* {@inheritdoc} |
229
|
|
|
*/ |
230
|
|
|
public function count() |
231
|
|
|
{ |
232
|
|
|
return count($this->elements); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* {@inheritdoc} |
237
|
|
|
*/ |
238
|
19 |
|
public function set($key, $value) |
239
|
|
|
{ |
240
|
19 |
|
$this->elements[$key] = $value; |
241
|
19 |
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* {@inheritdoc} |
245
|
|
|
*/ |
246
|
|
|
public function add($element) |
247
|
|
|
{ |
248
|
|
|
$this->elements[] = $element; |
249
|
|
|
|
250
|
|
|
return true; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* {@inheritdoc} |
255
|
|
|
*/ |
256
|
|
|
public function merge(array $elements) |
257
|
|
|
{ |
258
|
|
|
$this->elements = array_merge($this->elements, $elements); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* {@inheritdoc} |
263
|
|
|
*/ |
264
|
|
|
public function replace(array $elements) |
265
|
|
|
{ |
266
|
|
|
$this->elements = array_replace($this->elements, $elements); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* {@inheritdoc} |
271
|
|
|
*/ |
272
|
|
|
public function isEmpty() |
273
|
|
|
{ |
274
|
|
|
return empty($this->elements); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Required by interface IteratorAggregate. |
279
|
|
|
* |
280
|
|
|
* {@inheritdoc} |
281
|
|
|
*/ |
282
|
|
|
public function getIterator() |
283
|
|
|
{ |
284
|
|
|
return new \ArrayIterator($this->elements); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* {@inheritdoc} |
289
|
|
|
* |
290
|
|
|
* @return static |
291
|
|
|
*/ |
292
|
|
|
public function map(\Closure $func) |
293
|
|
|
{ |
294
|
|
|
return $this->createFrom(array_map($func, $this->elements)); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* {@inheritdoc} |
299
|
|
|
* |
300
|
|
|
* @return static |
301
|
|
|
*/ |
302
|
|
|
public function filter(\Closure $p) |
303
|
|
|
{ |
304
|
|
|
return $this->createFrom(array_filter($this->elements, $p)); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* {@inheritdoc} |
309
|
|
|
*/ |
310
|
|
|
public function forAll(\Closure $p) |
311
|
|
|
{ |
312
|
|
|
foreach ($this->elements as $key => $element) { |
313
|
|
|
if (!$p($key, $element)) { |
314
|
|
|
return false; |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
return true; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* {@inheritdoc} |
323
|
|
|
*/ |
324
|
|
|
public function partition(\Closure $p) |
325
|
|
|
{ |
326
|
|
|
$matches = $noMatches = []; |
327
|
|
|
|
328
|
|
|
foreach ($this->elements as $key => $element) { |
329
|
|
|
if ($p($key, $element)) { |
330
|
|
|
$matches[$key] = $element; |
331
|
|
|
} else { |
332
|
|
|
$noMatches[$key] = $element; |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
return [$this->createFrom($matches), $this->createFrom($noMatches)]; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* {@inheritdoc} |
341
|
|
|
*/ |
342
|
|
|
public function clear() |
343
|
|
|
{ |
344
|
|
|
$this->elements = []; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* {@inheritdoc} |
349
|
|
|
*/ |
350
|
|
|
public function slice($offset, $length = null) |
351
|
|
|
{ |
352
|
|
|
return array_slice($this->elements, $offset, $length, true); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Returns a string representation of this object. |
357
|
|
|
* |
358
|
|
|
* @return string |
359
|
|
|
*/ |
360
|
|
|
public function __toString() |
361
|
|
|
{ |
362
|
|
|
return __CLASS__ . '@' . spl_object_hash($this); |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|