|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\Immutable; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\Immutable\Exception\{ |
|
7
|
|
|
LogicException, |
|
8
|
|
|
GroupEmptySequenceException, |
|
9
|
|
|
InvalidArgumentException |
|
10
|
|
|
}; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* {@inheritdoc} |
|
14
|
|
|
*/ |
|
15
|
|
|
class Stream implements StreamInterface |
|
16
|
|
|
{ |
|
17
|
|
|
use Type; |
|
18
|
|
|
|
|
19
|
|
|
private $type; |
|
20
|
|
|
private $spec; |
|
21
|
|
|
private $values; |
|
22
|
|
|
|
|
23
|
50 |
|
public function __construct(string $type) |
|
24
|
|
|
{ |
|
25
|
50 |
|
$this->type = new Str($type); |
|
26
|
50 |
|
$this->spec = $this->getSpecFor($type); |
|
27
|
50 |
|
$this->values = new Sequence; |
|
28
|
50 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
26 |
|
public function type(): Str |
|
34
|
|
|
{ |
|
35
|
26 |
|
return $this->type; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
3 |
|
public function size(): int |
|
42
|
|
|
{ |
|
43
|
3 |
|
return $this->values->size(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
2 |
|
public function count(): int |
|
50
|
|
|
{ |
|
51
|
2 |
|
return $this->values->size(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
19 |
|
public function toPrimitive() |
|
58
|
|
|
{ |
|
59
|
19 |
|
return $this->values->toPrimitive(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
7 |
|
public function current() |
|
66
|
|
|
{ |
|
67
|
7 |
|
return $this->values->current(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
5 |
|
public function key() |
|
74
|
|
|
{ |
|
75
|
5 |
|
return $this->values->key(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
*/ |
|
81
|
7 |
|
public function next() |
|
82
|
|
|
{ |
|
83
|
7 |
|
$this->values->next(); |
|
84
|
7 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* {@inheritdoc} |
|
88
|
|
|
*/ |
|
89
|
7 |
|
public function rewind() |
|
90
|
|
|
{ |
|
91
|
7 |
|
$this->values->rewind(); |
|
92
|
7 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritdoc} |
|
96
|
|
|
*/ |
|
97
|
7 |
|
public function valid() |
|
98
|
|
|
{ |
|
99
|
7 |
|
return $this->values->valid(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritdoc} |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function offsetExists($offset): bool |
|
106
|
|
|
{ |
|
107
|
1 |
|
return $this->values->offsetExists($offset); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritdoc} |
|
112
|
|
|
*/ |
|
113
|
5 |
|
public function offsetGet($offset) |
|
114
|
|
|
{ |
|
115
|
5 |
|
return $this->get($offset); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* {@inheritdoc} |
|
120
|
|
|
*/ |
|
121
|
1 |
|
public function offsetSet($offset, $value) |
|
122
|
|
|
{ |
|
123
|
1 |
|
throw new LogicException('You can\'t modify a stream'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* {@inheritdoc} |
|
128
|
|
|
*/ |
|
129
|
1 |
|
public function offsetUnset($offset) |
|
130
|
|
|
{ |
|
131
|
1 |
|
throw new LogicException('You can\'t modify a stream'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* {@inheritdoc} |
|
136
|
|
|
*/ |
|
137
|
7 |
|
public function get(int $index) |
|
138
|
|
|
{ |
|
139
|
7 |
|
return $this->values->get($index); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* {@inheritdoc} |
|
144
|
|
|
*/ |
|
145
|
1 |
|
public function diff(StreamInterface $stream): StreamInterface |
|
146
|
|
|
{ |
|
147
|
1 |
|
$this->validate($stream); |
|
148
|
|
|
|
|
149
|
1 |
|
$newStream = clone $this; |
|
150
|
1 |
|
$newStream->values = $this->values->diff( |
|
151
|
1 |
|
new Sequence(...$stream) |
|
152
|
|
|
); |
|
153
|
|
|
|
|
154
|
1 |
|
return $newStream; |
|
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* {@inheritdoc} |
|
159
|
|
|
*/ |
|
160
|
1 |
|
public function distinct(): StreamInterface |
|
161
|
|
|
{ |
|
162
|
1 |
|
$stream = clone $this; |
|
163
|
1 |
|
$stream->values = $this->values->distinct(); |
|
164
|
|
|
|
|
165
|
1 |
|
return $stream; |
|
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* {@inheritdoc} |
|
170
|
|
|
*/ |
|
171
|
1 |
|
public function drop(int $size): StreamInterface |
|
172
|
|
|
{ |
|
173
|
1 |
|
$stream = clone $this; |
|
174
|
1 |
|
$stream->values = $this->values->drop($size); |
|
175
|
|
|
|
|
176
|
1 |
|
return $stream; |
|
|
|
|
|
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* {@inheritdoc} |
|
181
|
|
|
*/ |
|
182
|
1 |
|
public function dropEnd(int $size): StreamInterface |
|
183
|
|
|
{ |
|
184
|
1 |
|
$stream = clone $this; |
|
185
|
1 |
|
$stream->values = $this->values->dropEnd($size); |
|
186
|
|
|
|
|
187
|
1 |
|
return $stream; |
|
|
|
|
|
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* {@inheritdoc} |
|
192
|
|
|
*/ |
|
193
|
2 |
|
public function equals(StreamInterface $stream): bool |
|
194
|
|
|
{ |
|
195
|
2 |
|
$this->validate($stream); |
|
196
|
|
|
|
|
197
|
1 |
|
return $this->values->equals( |
|
198
|
1 |
|
new Sequence(...$stream) |
|
199
|
|
|
); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* {@inheritdoc} |
|
204
|
|
|
*/ |
|
205
|
1 |
|
public function filter(callable $predicate): StreamInterface |
|
206
|
|
|
{ |
|
207
|
1 |
|
$stream = clone $this; |
|
208
|
1 |
|
$stream->values = $this->values->filter($predicate); |
|
209
|
|
|
|
|
210
|
1 |
|
return $stream; |
|
|
|
|
|
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* {@inheritdoc} |
|
215
|
|
|
*/ |
|
216
|
|
|
public function foreach(callable $function): StreamInterface |
|
217
|
|
|
{ |
|
218
|
1 |
|
$this->values->foreach($function); |
|
219
|
|
|
|
|
220
|
1 |
|
return $this; |
|
|
|
|
|
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* {@inheritdoc} |
|
225
|
|
|
*/ |
|
226
|
2 |
View Code Duplication |
public function groupBy(callable $discriminator): MapInterface |
|
|
|
|
|
|
227
|
|
|
{ |
|
228
|
2 |
|
if ($this->size() === 0) { |
|
229
|
1 |
|
throw new GroupEmptySequenceException; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
1 |
|
$map = null; |
|
233
|
|
|
|
|
234
|
1 |
|
foreach ($this->values as $value) { |
|
235
|
1 |
|
$key = $discriminator($value); |
|
236
|
|
|
|
|
237
|
1 |
|
if ($map === null) { |
|
238
|
1 |
|
$type = gettype($key); |
|
239
|
1 |
|
$map = new Map( |
|
240
|
1 |
|
$type === 'object' ? get_class($key) : $type, |
|
241
|
1 |
|
StreamInterface::class |
|
242
|
|
|
); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
1 |
|
if ($map->contains($key)) { |
|
246
|
1 |
|
$map = $map->put( |
|
247
|
|
|
$key, |
|
248
|
1 |
|
$map->get($key)->add($value) |
|
249
|
|
|
); |
|
250
|
|
|
} else { |
|
251
|
1 |
|
$map = $map->put( |
|
252
|
|
|
$key, |
|
253
|
1 |
|
(new self((string) $this->type))->add($value) |
|
254
|
|
|
); |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
1 |
|
return $map; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* {@inheritdoc} |
|
263
|
|
|
*/ |
|
264
|
2 |
|
public function first() |
|
265
|
|
|
{ |
|
266
|
2 |
|
return $this->values->first(); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* {@inheritdoc} |
|
271
|
|
|
*/ |
|
272
|
2 |
|
public function last() |
|
273
|
|
|
{ |
|
274
|
2 |
|
return $this->values->last(); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* {@inheritdoc} |
|
279
|
|
|
*/ |
|
280
|
1 |
|
public function contains($element): bool |
|
281
|
|
|
{ |
|
282
|
1 |
|
return $this->values->contains($element); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* {@inheritdoc} |
|
287
|
|
|
*/ |
|
288
|
1 |
|
public function indexOf($element): int |
|
289
|
|
|
{ |
|
290
|
1 |
|
return $this->values->indexOf($element); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* {@inheritdoc} |
|
295
|
|
|
*/ |
|
296
|
1 |
|
public function indices(): StreamInterface |
|
297
|
|
|
{ |
|
298
|
1 |
|
$stream = new self('int'); |
|
299
|
1 |
|
$stream->values = $this->values->indices(); |
|
300
|
|
|
|
|
301
|
1 |
|
return $stream; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* {@inheritdoc} |
|
306
|
|
|
*/ |
|
307
|
3 |
|
public function map(callable $function): StreamInterface |
|
308
|
|
|
{ |
|
309
|
|
|
return $this |
|
310
|
3 |
|
->values |
|
311
|
3 |
|
->reduce( |
|
312
|
3 |
|
new self((string) $this->type), |
|
313
|
3 |
|
function(self $carry, $element) use($function): StreamInterface { |
|
314
|
3 |
|
return $carry->add($function($element)); |
|
315
|
3 |
|
} |
|
316
|
|
|
); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* {@inheritdoc} |
|
321
|
|
|
*/ |
|
322
|
2 |
View Code Duplication |
public function pad(int $size, $element): StreamInterface |
|
|
|
|
|
|
323
|
|
|
{ |
|
324
|
2 |
|
$this->spec->validate($element); |
|
325
|
|
|
|
|
326
|
1 |
|
$stream = clone $this; |
|
327
|
1 |
|
$stream->values = $this->values->pad($size, $element); |
|
328
|
|
|
|
|
329
|
1 |
|
return $stream; |
|
|
|
|
|
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* {@inheritdoc} |
|
334
|
|
|
*/ |
|
335
|
1 |
|
public function partition(callable $predicate): MapInterface |
|
336
|
|
|
{ |
|
337
|
1 |
|
$truthy = new self((string) $this->type); |
|
338
|
1 |
|
$falsy = new self((string) $this->type); |
|
339
|
|
|
|
|
340
|
1 |
|
foreach ($this->values as $value) { |
|
341
|
1 |
|
if ($predicate($value) === true) { |
|
342
|
1 |
|
$truthy = $truthy->add($value); |
|
343
|
|
|
} else { |
|
344
|
1 |
|
$falsy = $falsy->add($value); |
|
345
|
|
|
} |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
1 |
|
return (new Map('bool', StreamInterface::class)) |
|
349
|
1 |
|
->put(true, $truthy) |
|
350
|
1 |
|
->put(false, $falsy); |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* {@inheritdoc} |
|
355
|
|
|
*/ |
|
356
|
1 |
|
public function slice(int $from, int $until): StreamInterface |
|
357
|
|
|
{ |
|
358
|
1 |
|
$stream = clone $this; |
|
359
|
1 |
|
$stream->values = $this->values->slice($from, $until); |
|
360
|
|
|
|
|
361
|
1 |
|
return $stream; |
|
|
|
|
|
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* {@inheritdoc} |
|
366
|
|
|
*/ |
|
367
|
1 |
|
public function splitAt(int $position): StreamInterface |
|
368
|
|
|
{ |
|
369
|
1 |
|
$stream = new self(StreamInterface::class); |
|
370
|
1 |
|
$splitted = $this->values->splitAt($position); |
|
371
|
1 |
|
$first = new self((string) $this->type); |
|
372
|
1 |
|
$second = new self((string) $this->type); |
|
373
|
1 |
|
$first->values = $splitted->first(); |
|
374
|
1 |
|
$second->values = $splitted->last(); |
|
375
|
|
|
|
|
376
|
1 |
|
return $stream->add($first)->add($second); |
|
|
|
|
|
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* {@inheritdoc} |
|
381
|
|
|
*/ |
|
382
|
1 |
|
public function take(int $size): StreamInterface |
|
383
|
|
|
{ |
|
384
|
1 |
|
$stream = clone $this; |
|
385
|
1 |
|
$stream->values = $this->values->take($size); |
|
386
|
|
|
|
|
387
|
1 |
|
return $stream; |
|
|
|
|
|
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
/** |
|
391
|
|
|
* {@inheritdoc} |
|
392
|
|
|
*/ |
|
393
|
1 |
|
public function takeEnd(int $size): StreamInterface |
|
394
|
|
|
{ |
|
395
|
1 |
|
$stream = clone $this; |
|
396
|
1 |
|
$stream->values = $this->values->takeEnd($size); |
|
397
|
|
|
|
|
398
|
1 |
|
return $stream; |
|
|
|
|
|
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
/** |
|
402
|
|
|
* {@inheritdoc} |
|
403
|
|
|
*/ |
|
404
|
2 |
View Code Duplication |
public function append(StreamInterface $stream): StreamInterface |
|
|
|
|
|
|
405
|
|
|
{ |
|
406
|
2 |
|
$this->validate($stream); |
|
407
|
|
|
|
|
408
|
1 |
|
$self = clone $this; |
|
409
|
1 |
|
$self->values = $this->values->append( |
|
410
|
1 |
|
new Sequence(...$stream) |
|
411
|
|
|
); |
|
412
|
|
|
|
|
413
|
1 |
|
return $self; |
|
|
|
|
|
|
414
|
|
|
} |
|
415
|
|
|
|
|
416
|
|
|
/** |
|
417
|
|
|
* {@inheritdoc} |
|
418
|
|
|
*/ |
|
419
|
2 |
View Code Duplication |
public function intersect(StreamInterface $stream): StreamInterface |
|
|
|
|
|
|
420
|
|
|
{ |
|
421
|
2 |
|
$this->validate($stream); |
|
422
|
|
|
|
|
423
|
1 |
|
$self = clone $this; |
|
424
|
1 |
|
$self->values = $this->values->intersect( |
|
425
|
1 |
|
new Sequence(...$stream) |
|
426
|
|
|
); |
|
427
|
|
|
|
|
428
|
1 |
|
return $self; |
|
|
|
|
|
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
|
|
/** |
|
432
|
|
|
* {@inheritdoc} |
|
433
|
|
|
*/ |
|
434
|
2 |
|
public function join(string $separator): Str |
|
435
|
|
|
{ |
|
436
|
2 |
|
return new Str((string) $this->values->join($separator)); |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
|
|
/** |
|
440
|
|
|
* {@inheritdoc} |
|
441
|
|
|
*/ |
|
442
|
40 |
View Code Duplication |
public function add($element): StreamInterface |
|
|
|
|
|
|
443
|
|
|
{ |
|
444
|
40 |
|
$this->spec->validate($element); |
|
445
|
|
|
|
|
446
|
39 |
|
$stream = clone $this; |
|
447
|
39 |
|
$stream->values = $this->values->add($element); |
|
448
|
|
|
|
|
449
|
39 |
|
return $stream; |
|
|
|
|
|
|
450
|
|
|
} |
|
451
|
|
|
|
|
452
|
|
|
/** |
|
453
|
|
|
* {@inheritdoc} |
|
454
|
|
|
*/ |
|
455
|
1 |
|
public function sort(callable $function): StreamInterface |
|
456
|
|
|
{ |
|
457
|
1 |
|
$stream = clone $this; |
|
458
|
1 |
|
$stream->values = $this->values->sort($function); |
|
459
|
|
|
|
|
460
|
1 |
|
return $stream; |
|
|
|
|
|
|
461
|
|
|
} |
|
462
|
|
|
|
|
463
|
|
|
/** |
|
464
|
|
|
* {@inheritdoc} |
|
465
|
|
|
*/ |
|
466
|
1 |
|
public function reduce($carry, callable $reducer) |
|
467
|
|
|
{ |
|
468
|
1 |
|
return $this->values->reduce($carry, $reducer); |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
/** |
|
472
|
|
|
* Make sure the stream is compatible with the current one |
|
473
|
|
|
* |
|
474
|
|
|
* @param StreamInterface $stream |
|
475
|
|
|
* |
|
476
|
|
|
* @throws InvalidArgumentException |
|
477
|
|
|
* |
|
478
|
|
|
* @return void |
|
479
|
|
|
*/ |
|
480
|
7 |
|
private function validate(StreamInterface $stream) |
|
481
|
|
|
{ |
|
482
|
7 |
|
if (!$stream->type()->equals($this->type)) { |
|
|
|
|
|
|
483
|
3 |
|
throw new InvalidArgumentException( |
|
484
|
3 |
|
'The 2 streams does not reference the same type' |
|
485
|
|
|
); |
|
486
|
|
|
} |
|
487
|
4 |
|
} |
|
488
|
|
|
} |
|
489
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.