|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Collections\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Collections\Comparer\NumericKeyComparer; |
|
6
|
|
|
use Collections\Generic\ComparerInterface; |
|
7
|
|
|
use Collections\Immutable\ImmVector; |
|
8
|
|
|
use Collections\Immutable\ImmMap; |
|
9
|
|
|
use Collections\Immutable\ImmSet; |
|
10
|
|
|
use Collections\Enumerable; |
|
11
|
|
|
use Collections\Iterator\LazyFilterIterable; |
|
12
|
|
|
use Collections\Iterator\LazyFilterKeyedIterable; |
|
13
|
|
|
use Collections\Iterator\LazyMapIterable; |
|
14
|
|
|
use Collections\Iterator\LazyMapWithKeyIterable; |
|
15
|
|
|
use Collections\Iterator\LazySkipIterable; |
|
16
|
|
|
use Collections\Iterator\LazySkipWhileIterable; |
|
17
|
|
|
use Collections\Iterator\LazySliceIterable; |
|
18
|
|
|
use Collections\Iterator\LazyTakeIterable; |
|
19
|
|
|
use Collections\Iterator\LazyTakeWhileIterable; |
|
20
|
|
|
use Collections\Iterator\LazyZipIterable; |
|
21
|
|
|
use Collections\Map; |
|
22
|
|
|
use Collections\Pair; |
|
23
|
|
|
use Collections\Set; |
|
24
|
|
|
use Collections\Vector; |
|
25
|
|
|
use Collections\VectorInterface; |
|
26
|
|
|
|
|
27
|
|
|
trait CommonContainerMethodsTrait |
|
28
|
|
|
{ |
|
29
|
|
|
use GuardTrait; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var ComparerInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $defaultComparer; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Gets the default comparer for this collection |
|
37
|
|
|
* @return ComparerInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getDefaultComparer() |
|
40
|
|
|
{ |
|
41
|
|
|
if ($this->defaultComparer === null) { |
|
42
|
|
|
$this->defaultComparer = new NumericKeyComparer(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $this->defaultComparer; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Sets the default comparer for this collection |
|
50
|
|
|
* @param ComparerInterface $defaultComparer |
|
51
|
|
|
* @return $this |
|
52
|
|
|
*/ |
|
53
|
|
|
public function setDefaultComparer(ComparerInterface $defaultComparer) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->defaultComparer = $defaultComparer; |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public function __toString() |
|
64
|
|
|
{ |
|
65
|
|
|
return get_class($this); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function equals($obj) |
|
69
|
|
|
{ |
|
70
|
|
|
return ($obj === $this); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function serialize() |
|
77
|
|
|
{ |
|
78
|
|
|
return serialize($this->container); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
public function unserialize($serialized) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->container = unserialize($serialized); |
|
87
|
|
|
|
|
88
|
|
|
return $this->container; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public function jsonSerialize() |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->container; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return array |
|
101
|
|
|
*/ |
|
102
|
|
|
public function toValuesArray() |
|
103
|
|
|
{ |
|
104
|
|
|
$arr = []; |
|
105
|
|
|
foreach ($this as $value) { |
|
|
|
|
|
|
106
|
|
|
if ($value instanceof Enumerable) { |
|
107
|
|
|
$arr[] = $value->toArray(); |
|
108
|
|
|
} else { |
|
109
|
|
|
$arr[] = $value; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $arr; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function toKeysArray() |
|
117
|
|
|
{ |
|
118
|
|
|
$res = []; |
|
119
|
|
|
foreach ($this as $k => $_) { |
|
|
|
|
|
|
120
|
|
|
$res[] = $k; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $res; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* {@inheritdoc} |
|
128
|
|
|
*/ |
|
129
|
|
|
public function toArray() |
|
130
|
|
|
{ |
|
131
|
|
|
$arr = []; |
|
132
|
|
|
foreach ($this as $key => $value) { |
|
|
|
|
|
|
133
|
|
|
if ($value instanceof Enumerable) { |
|
134
|
|
|
$arr[$key] = $value->toArray(); |
|
135
|
|
|
} else { |
|
136
|
|
|
$arr[$key] = $value; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $arr; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* {@inheritdoc} |
|
145
|
|
|
*/ |
|
146
|
|
|
public static function fromArray(array $arr) |
|
147
|
|
|
{ |
|
148
|
|
|
$map = new static(); |
|
149
|
|
|
foreach ($arr as $k => $v) { |
|
150
|
|
|
if (is_array($v)) { |
|
151
|
|
|
$map[$k] = new static($v); |
|
|
|
|
|
|
152
|
|
|
} else { |
|
153
|
|
|
$map[$k] = $v; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $map; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* {@inheritdoc} |
|
162
|
|
|
*/ |
|
163
|
|
|
public function toVector() |
|
164
|
|
|
{ |
|
165
|
|
|
return new Vector($this); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* {@inheritdoc} |
|
170
|
|
|
*/ |
|
171
|
|
|
public function toImmVector() |
|
172
|
|
|
{ |
|
173
|
|
|
return new ImmVector($this); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* {@inheritdoc} |
|
178
|
|
|
*/ |
|
179
|
|
|
public function toSet() |
|
180
|
|
|
{ |
|
181
|
|
|
return new Set($this); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* {@inheritdoc} |
|
186
|
|
|
*/ |
|
187
|
|
|
public function toImmSet() |
|
188
|
|
|
{ |
|
189
|
|
|
return new ImmSet($this); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* {@inheritdoc} |
|
194
|
|
|
*/ |
|
195
|
|
|
public function toMap() |
|
196
|
|
|
{ |
|
197
|
|
|
return new Map($this); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* {@inheritdoc} |
|
202
|
|
|
*/ |
|
203
|
|
|
public function toImmMap() |
|
204
|
|
|
{ |
|
205
|
|
|
return new ImmMap($this); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* {@inheritDoc} |
|
210
|
|
|
* @return $this |
|
211
|
|
|
*/ |
|
212
|
|
|
public function map(callable $callable) |
|
213
|
|
|
{ |
|
214
|
|
|
return new static(new LazyMapIterable($this, $callable)); |
|
|
|
|
|
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* {@inheritDoc} |
|
219
|
|
|
* @return $this |
|
220
|
|
|
*/ |
|
221
|
|
|
public function mapWithKey($callback) |
|
222
|
|
|
{ |
|
223
|
|
|
return new static(new LazyMapWithKeyIterable($this, $callback)); |
|
|
|
|
|
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* {@inheritDoc} |
|
228
|
|
|
* @return $this |
|
229
|
|
|
*/ |
|
230
|
|
|
public function filter(callable $callable) |
|
231
|
|
|
{ |
|
232
|
|
|
return new static(new LazyFilterIterable($this, $callable)); |
|
|
|
|
|
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* {@inheritDoc} |
|
237
|
|
|
* @return $this |
|
238
|
|
|
*/ |
|
239
|
|
|
public function filterWithKey($callback) |
|
240
|
|
|
{ |
|
241
|
|
|
return new static(new LazyFilterKeyedIterable($this, $callback)); |
|
|
|
|
|
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* {@inheritDoc} |
|
246
|
|
|
* @return $this |
|
247
|
|
|
*/ |
|
248
|
|
View Code Duplication |
public function zip($traversable) |
|
|
|
|
|
|
249
|
|
|
{ |
|
250
|
|
|
if (is_array($traversable)) { |
|
251
|
|
|
$traversable = new ImmVector($traversable); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
if ($traversable instanceof \Traversable) { |
|
255
|
|
|
return new static(new LazyZipIterable($this, $traversable)); |
|
|
|
|
|
|
256
|
|
|
} else { |
|
257
|
|
|
throw new \InvalidArgumentException('Parameter must be an array or an instance of Traversable'); |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* {@inheritDoc} |
|
263
|
|
|
* @return $this |
|
264
|
|
|
*/ |
|
265
|
|
|
public function take($size = 1) |
|
266
|
|
|
{ |
|
267
|
|
|
if (!is_int($size)) { |
|
268
|
|
|
throw new \InvalidArgumentException('Parameter n must be an integer'); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
return new static(new LazyTakeIterable($this, $size)); |
|
|
|
|
|
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
public function takeWhile(callable $callable) |
|
275
|
|
|
{ |
|
276
|
|
|
return new static(new LazyTakeWhileIterable($this, $callable)); |
|
|
|
|
|
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
public function skip($n) |
|
280
|
|
|
{ |
|
281
|
|
|
if (!is_int($n)) { |
|
282
|
|
|
throw new \InvalidArgumentException('Parameter n must be an integer'); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
return new static(new LazySkipIterable($this, $n)); |
|
|
|
|
|
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
public function skipWhile(callable $callable) |
|
289
|
|
|
{ |
|
290
|
|
|
return new static(new LazySkipWhileIterable($this, $callable)); |
|
|
|
|
|
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
public function slice($start, $length) |
|
294
|
|
|
{ |
|
295
|
|
|
if ($start < 0) { |
|
296
|
|
|
throw new \InvalidArgumentException('Parameter start must be a non-negative integer'); |
|
297
|
|
|
} |
|
298
|
|
|
if ($length < 0) { |
|
299
|
|
|
throw new \InvalidArgumentException('Parameter len must be a non-negative integer'); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
return new static(new LazySliceIterable($this, $start, $length)); |
|
|
|
|
|
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* {@inheritDoc} |
|
307
|
|
|
* @return $this |
|
308
|
|
|
*/ |
|
309
|
|
|
public function first() |
|
310
|
|
|
{ |
|
311
|
|
|
if ($this->isEmpty()) { |
|
312
|
|
|
return null; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
return current($this->container); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* {@inheritDoc} |
|
320
|
|
|
* @return $this |
|
321
|
|
|
*/ |
|
322
|
|
|
public function last() |
|
323
|
|
|
{ |
|
324
|
|
|
if ($this->isEmpty()) { |
|
325
|
|
|
return null; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
$lastItem = array_slice($this->container, -1, 1); |
|
329
|
|
|
|
|
330
|
|
|
return current($lastItem); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* {@inheritdoc} |
|
335
|
|
|
*/ |
|
336
|
|
|
public function exists(callable $fn) |
|
337
|
|
|
{ |
|
338
|
|
|
foreach ($this as $element) { |
|
|
|
|
|
|
339
|
|
|
if ($fn($element)) { |
|
340
|
|
|
return true; |
|
341
|
|
|
} |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
return false; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
View Code Duplication |
public function concatAll() |
|
|
|
|
|
|
348
|
|
|
{ |
|
349
|
|
|
/** @var VectorInterface $results */ |
|
350
|
|
|
$results = new static(); |
|
351
|
|
|
$this->each(function (Enumerable $subArray) use ($results) { |
|
|
|
|
|
|
352
|
|
|
$subArray->each(function ($item) use ($results) { |
|
353
|
|
|
$results->add($item); |
|
354
|
|
|
}); |
|
355
|
|
|
}); |
|
356
|
|
|
|
|
357
|
|
|
return $results; |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
/** |
|
361
|
|
|
* {@inheritDoc} |
|
362
|
|
|
* @return $this |
|
363
|
|
|
*/ |
|
364
|
|
View Code Duplication |
public function groupBy($callback) |
|
|
|
|
|
|
365
|
|
|
{ |
|
366
|
|
|
$group = new Map(); |
|
367
|
|
|
foreach ($this as $value) { |
|
|
|
|
|
|
368
|
|
|
$key = $callback($value); |
|
369
|
|
|
if (!$group->containsKey($key)) { |
|
370
|
|
|
$element = $this instanceof VectorInterface ? new static([$value]) : new Vector([$value]); |
|
|
|
|
|
|
371
|
|
|
$group->add(new Pair($key, $element)); |
|
372
|
|
|
} else { |
|
373
|
|
|
$value = $group->get($key)->add($value); |
|
374
|
|
|
$group->set($key, $value); |
|
375
|
|
|
} |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
return $group; |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
/** |
|
382
|
|
|
* {@inheritDoc} |
|
383
|
|
|
* @return $this |
|
384
|
|
|
*/ |
|
385
|
|
View Code Duplication |
public function indexBy($callback) |
|
|
|
|
|
|
386
|
|
|
{ |
|
387
|
|
|
$group = new Map(); |
|
388
|
|
|
foreach ($this as $value) { |
|
|
|
|
|
|
389
|
|
|
$key = $callback($value); |
|
390
|
|
|
$group->set($key, $value); |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
return $group; |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* {@inheritdoc} |
|
398
|
|
|
*/ |
|
399
|
|
|
public function reduce(callable $callback, $initial = null) |
|
400
|
|
|
{ |
|
401
|
|
|
foreach ($this as $element) { |
|
|
|
|
|
|
402
|
|
|
$initial = $callback($initial, $element); |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
|
|
return $initial; |
|
406
|
|
|
} |
|
407
|
|
|
} |
|
408
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: