1
|
|
|
<?php |
|
|
|
|
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Compose\Lazy; |
5
|
|
|
|
6
|
|
|
use Innmind\Compose\Lazy; |
7
|
|
|
use Innmind\Immutable\{ |
8
|
|
|
StreamInterface, |
9
|
|
|
Stream as Base, |
10
|
|
|
MapInterface, |
11
|
|
|
Str, |
12
|
|
|
Type, |
13
|
|
|
Exception\LogicException |
14
|
|
|
}; |
15
|
|
|
|
16
|
|
|
final class Stream implements StreamInterface |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
use Type; |
19
|
|
|
|
20
|
|
|
private $type; |
21
|
|
|
private $spec; |
22
|
|
|
private $values; |
23
|
|
|
private $loaded; |
24
|
|
|
|
25
|
44 |
|
public function __construct(string $type, ...$values) |
26
|
|
|
{ |
27
|
44 |
|
$this->type = Str::of($type); |
28
|
44 |
|
$this->spec = $this->getSpecificationFor($type); |
29
|
44 |
|
$this->values = Base::of('mixed', ...$values); |
30
|
44 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
25 |
|
public function type(): Str |
36
|
|
|
{ |
37
|
25 |
|
return $this->type; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
1 |
|
public function size(): int |
44
|
|
|
{ |
45
|
1 |
|
return $this->values->size(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
5 |
|
public function count(): int |
52
|
|
|
{ |
53
|
5 |
|
return $this->values->size(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
30 |
|
public function toPrimitive() |
60
|
|
|
{ |
61
|
30 |
|
if ($this->loaded) { |
62
|
11 |
|
return $this->loaded->toPrimitive(); |
63
|
|
|
} |
64
|
|
|
|
65
|
30 |
|
return $this->values->reduce( |
66
|
30 |
|
[], |
67
|
30 |
|
function(array $values, $value): array { |
68
|
30 |
|
$values[] = $this->load($value); |
69
|
|
|
|
70
|
30 |
|
return $values; |
71
|
30 |
|
} |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
2 |
|
public function current() |
79
|
|
|
{ |
80
|
2 |
|
return $this->load($this->values->current()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
1 |
|
public function key() |
87
|
|
|
{ |
88
|
1 |
|
return $this->values->key(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
1 |
|
public function next() |
95
|
|
|
{ |
96
|
1 |
|
$this->values->next(); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
1 |
|
public function rewind() |
103
|
|
|
{ |
104
|
1 |
|
$this->values->rewind(); |
105
|
1 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
1 |
|
public function valid() |
111
|
|
|
{ |
112
|
1 |
|
return $this->values->valid(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
1 |
|
public function offsetExists($offset): bool |
119
|
|
|
{ |
120
|
1 |
|
return $this->values->offsetExists($offset); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
1 |
|
public function offsetGet($offset) |
127
|
|
|
{ |
128
|
1 |
|
return $this->get($offset); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
1 |
|
public function offsetSet($offset, $value) |
135
|
|
|
{ |
136
|
1 |
|
throw new LogicException('You can\'t modify a stream'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
1 |
|
public function offsetUnset($offset) |
143
|
|
|
{ |
144
|
1 |
|
throw new LogicException('You can\'t modify a stream'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
3 |
|
public function get(int $index) |
151
|
|
|
{ |
152
|
3 |
|
return $this->load($this->values->get($index)); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
1 |
|
public function diff(StreamInterface $stream): StreamInterface |
159
|
|
|
{ |
160
|
1 |
|
return $this->stream()->diff($stream); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
1 |
|
public function distinct(): StreamInterface |
167
|
|
|
{ |
168
|
1 |
|
return $this->stream()->distinct(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* {@inheritdoc} |
173
|
|
|
*/ |
174
|
1 |
|
public function drop(int $size): StreamInterface |
175
|
|
|
{ |
176
|
1 |
|
$self = clone $this; |
177
|
1 |
|
$self->values = $self->values->drop($size); |
178
|
|
|
|
179
|
1 |
|
return $self; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritdoc} |
184
|
|
|
*/ |
185
|
1 |
|
public function dropEnd(int $size): StreamInterface |
186
|
|
|
{ |
187
|
1 |
|
$self = clone $this; |
188
|
1 |
|
$self->values = $self->values->dropEnd($size); |
189
|
|
|
|
190
|
1 |
|
return $self; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* {@inheritdoc} |
195
|
|
|
*/ |
196
|
2 |
|
public function equals(StreamInterface $stream): bool |
197
|
|
|
{ |
198
|
2 |
|
return $this->stream()->equals($stream); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* {@inheritdoc} |
203
|
|
|
*/ |
204
|
1 |
|
public function filter(callable $predicate): StreamInterface |
205
|
|
|
{ |
206
|
1 |
|
return $this->stream()->filter($predicate); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* {@inheritdoc} |
211
|
|
|
*/ |
212
|
1 |
|
public function foreach(callable $function): StreamInterface |
213
|
|
|
{ |
214
|
1 |
|
return $this->stream()->foreach($function); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* {@inheritdoc} |
219
|
|
|
*/ |
220
|
1 |
|
public function groupBy(callable $discriminator): MapInterface |
221
|
|
|
{ |
222
|
1 |
|
return $this->stream()->groupBy($discriminator); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* {@inheritdoc} |
227
|
|
|
*/ |
228
|
1 |
|
public function first() |
229
|
|
|
{ |
230
|
1 |
|
return $this->load($this->values->first()); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* {@inheritdoc} |
235
|
|
|
*/ |
236
|
1 |
|
public function last() |
237
|
|
|
{ |
238
|
1 |
|
return $this->load($this->values->last()); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* {@inheritdoc} |
243
|
|
|
*/ |
244
|
1 |
|
public function contains($element): bool |
245
|
|
|
{ |
246
|
1 |
|
return $this->stream()->contains($element); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* {@inheritdoc} |
251
|
|
|
*/ |
252
|
1 |
|
public function indexOf($element): int |
253
|
|
|
{ |
254
|
1 |
|
return $this->stream()->indexOf($element); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* {@inheritdoc} |
259
|
|
|
*/ |
260
|
1 |
|
public function indices(): StreamInterface |
261
|
|
|
{ |
262
|
1 |
|
return $this->values->indices(); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* {@inheritdoc} |
267
|
|
|
*/ |
268
|
1 |
|
public function map(callable $function): StreamInterface |
269
|
|
|
{ |
270
|
1 |
|
return $this->stream()->map($function); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* {@inheritdoc} |
275
|
|
|
*/ |
276
|
2 |
|
public function pad(int $size, $element): StreamInterface |
277
|
|
|
{ |
278
|
2 |
|
$this->spec->validate($element); |
279
|
|
|
|
280
|
1 |
|
$self = clone $this; |
281
|
1 |
|
$self->values = $self->values->pad($size, $element); |
282
|
|
|
|
283
|
1 |
|
return $self; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* {@inheritdoc} |
288
|
|
|
*/ |
289
|
1 |
|
public function partition(callable $predicate): MapInterface |
290
|
|
|
{ |
291
|
1 |
|
return $this->stream()->partition($predicate); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* {@inheritdoc} |
296
|
|
|
*/ |
297
|
1 |
|
public function slice(int $from, int $until): StreamInterface |
298
|
|
|
{ |
299
|
1 |
|
$self = clone $this; |
300
|
1 |
|
$self->values = $self->values->slice($from, $until); |
301
|
|
|
|
302
|
1 |
|
return $self; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* {@inheritdoc} |
307
|
|
|
*/ |
308
|
1 |
|
public function splitAt(int $position): StreamInterface |
309
|
|
|
{ |
310
|
1 |
|
$splitted = $this->values->splitAt($position); |
311
|
1 |
|
$first = clone $this; |
312
|
1 |
|
$last = clone $this; |
313
|
1 |
|
$first->values = $splitted->first(); |
314
|
1 |
|
$last->values = $splitted->last(); |
315
|
|
|
|
316
|
1 |
|
return Base::of(StreamInterface::class, $first, $last); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* {@inheritdoc} |
321
|
|
|
*/ |
322
|
1 |
|
public function take(int $size): StreamInterface |
323
|
|
|
{ |
324
|
1 |
|
$self = clone $this; |
325
|
1 |
|
$self->values = $self->values->take($size); |
326
|
|
|
|
327
|
1 |
|
return $self; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* {@inheritdoc} |
332
|
|
|
*/ |
333
|
1 |
|
public function takeEnd(int $size): StreamInterface |
334
|
|
|
{ |
335
|
1 |
|
$self = clone $this; |
336
|
1 |
|
$self->values = $self->values->takeEnd($size); |
337
|
|
|
|
338
|
1 |
|
return $self; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* {@inheritdoc} |
343
|
|
|
*/ |
344
|
2 |
|
public function append(StreamInterface $stream): StreamInterface |
345
|
|
|
{ |
346
|
2 |
|
if ($stream instanceof self && !$this->loaded && !$stream->loaded) { |
347
|
1 |
|
return new self( |
348
|
1 |
|
(string) $this->type, |
349
|
1 |
|
...$this->values, |
350
|
1 |
|
...$stream->values |
351
|
|
|
); |
352
|
|
|
} |
353
|
|
|
|
354
|
1 |
|
return $this->stream()->append($stream); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* {@inheritdoc} |
359
|
|
|
*/ |
360
|
1 |
|
public function intersect(StreamInterface $stream): StreamInterface |
361
|
|
|
{ |
362
|
1 |
|
return $this->stream()->intersect($stream); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* {@inheritdoc} |
367
|
|
|
*/ |
368
|
1 |
|
public function join(string $separator): Str |
369
|
|
|
{ |
370
|
1 |
|
return $this->stream()->join($separator); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* {@inheritdoc} |
375
|
|
|
*/ |
376
|
1 |
|
public function add($element): StreamInterface |
377
|
|
|
{ |
378
|
1 |
|
$this->spec->validate($element); |
379
|
|
|
|
380
|
1 |
|
$self = clone $this; |
381
|
1 |
|
$self->values = $self->values->add($element); |
382
|
|
|
|
383
|
1 |
|
return $self; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* {@inheritdoc} |
388
|
|
|
*/ |
389
|
1 |
|
public function sort(callable $function): StreamInterface |
390
|
|
|
{ |
391
|
1 |
|
return $this->stream()->sort($function); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* {@inheritdoc} |
396
|
|
|
*/ |
397
|
1 |
|
public function reduce($carry, callable $reducer) |
398
|
|
|
{ |
399
|
1 |
|
return $this->stream()->reduce($carry, $reducer); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* {@inheritdoc} |
404
|
|
|
*/ |
405
|
1 |
|
public function clear(): StreamInterface |
406
|
|
|
{ |
407
|
1 |
|
return new Base((string) $this->type); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Return the same stream but in reverse order |
412
|
|
|
* |
413
|
|
|
* @return self<T> |
414
|
|
|
*/ |
415
|
1 |
|
public function reverse(): StreamInterface |
416
|
|
|
{ |
417
|
1 |
|
$self = clone $this; |
418
|
1 |
|
$self->values = $self->values->reverse(); |
419
|
|
|
|
420
|
1 |
|
return $self; |
421
|
|
|
} |
422
|
|
|
|
423
|
36 |
|
private function load($value) |
424
|
|
|
{ |
425
|
36 |
|
if ($value instanceof Lazy) { |
426
|
33 |
|
return $value->load(); |
427
|
|
|
} |
428
|
|
|
|
429
|
5 |
|
return $value; |
430
|
|
|
} |
431
|
|
|
|
432
|
15 |
|
private function stream(): Base |
433
|
|
|
{ |
434
|
15 |
|
return $this->loaded ?? $this->loaded = Base::of( |
435
|
15 |
|
(string) $this->type, |
436
|
15 |
|
...$this->toPrimitive() |
437
|
|
|
); |
438
|
|
|
} |
439
|
|
|
} |
440
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.