1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Collection\Stream; |
4
|
|
|
|
5
|
|
|
use Bdf\Collection\Stream\Accumulator\AccumulatorInterface; |
6
|
|
|
use Bdf\Collection\Stream\Collector\CollectorInterface; |
7
|
|
|
use Bdf\Collection\Util\Optional; |
8
|
|
|
use Bdf\Collection\Util\OptionalInterface; |
9
|
|
|
use EmptyIterator; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Null object for streams |
13
|
|
|
*/ |
14
|
|
|
final class EmptyStream extends EmptyIterator implements StreamInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var EmptyStream|null |
18
|
|
|
*/ |
19
|
|
|
private static $instance; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
1 |
|
public function map(callable $transformer): StreamInterface |
26
|
|
|
{ |
27
|
1 |
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
1 |
|
public function mapKey(callable $function): StreamInterface |
34
|
|
|
{ |
35
|
1 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
1 |
|
public function filter(callable $predicate): StreamInterface |
42
|
|
|
{ |
43
|
1 |
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
1 |
|
public function distinct(callable $hashFunction = null): StreamInterface |
50
|
|
|
{ |
51
|
1 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
1 |
|
public function sort(callable $comparator = null, bool $preserveKeys = false): StreamInterface |
58
|
|
|
{ |
59
|
1 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
2 |
|
public function concat(StreamInterface $stream, bool $preserveKeys = true): StreamInterface |
66
|
|
|
{ |
67
|
2 |
|
return $preserveKeys ? $stream : new ConcatStream([$stream], false); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
1 |
|
public function flatMap(callable $transformer, bool $preserveKeys = false): StreamInterface |
74
|
|
|
{ |
75
|
1 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
1 |
|
public function skip(int $count): StreamInterface |
82
|
|
|
{ |
83
|
1 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
1 |
|
public function limit(int $count, int $offset = 0): StreamInterface |
90
|
|
|
{ |
91
|
1 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
1 |
|
public function forEach(callable $consumer): void |
98
|
|
|
{ |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
1 |
|
public function toArray(bool $preserveKeys = true): array |
105
|
|
|
{ |
106
|
1 |
|
return []; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
2 |
|
public function first(): OptionalInterface |
113
|
|
|
{ |
114
|
2 |
|
return Optional::empty(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
1 |
|
public function reduce(callable $accumulator, $initial = null) |
121
|
|
|
{ |
122
|
1 |
|
return $initial === null && $accumulator instanceof AccumulatorInterface |
123
|
1 |
|
? $accumulator->initial() |
124
|
1 |
|
: $initial |
125
|
|
|
; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
1 |
|
public function collect(CollectorInterface $collector) |
132
|
|
|
{ |
133
|
1 |
|
return $collector->finalize(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
1 |
|
public function matchAll(callable $predicate): bool |
140
|
|
|
{ |
141
|
1 |
|
return true; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
1 |
|
public function matchOne(callable $predicate): bool |
148
|
|
|
{ |
149
|
1 |
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get the Empty stream instance |
154
|
|
|
* |
155
|
|
|
* @return EmptyStream |
156
|
|
|
*/ |
157
|
9 |
|
public static function instance(): EmptyStream |
158
|
|
|
{ |
159
|
9 |
|
if (self::$instance) { |
160
|
9 |
|
return self::$instance; |
161
|
|
|
} |
162
|
|
|
|
163
|
1 |
|
return self::$instance = new EmptyStream(); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|