1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Compose\Lazy; |
5
|
|
|
|
6
|
|
|
use Innmind\Compose\Lazy; |
7
|
|
|
use Innmind\Immutable\{ |
8
|
|
|
SetInterface, |
9
|
|
|
Set as Base, |
10
|
|
|
Str, |
11
|
|
|
MapInterface, |
12
|
|
|
StreamInterface |
13
|
|
|
}; |
14
|
|
|
|
15
|
|
|
final class Set implements SetInterface |
16
|
|
|
{ |
17
|
|
|
private $type; |
18
|
|
|
private $values; |
19
|
|
|
private $loaded; |
20
|
|
|
|
21
|
25 |
|
public function __construct(string $type) |
22
|
|
|
{ |
23
|
25 |
|
$this->type = Str::of($type); |
24
|
25 |
|
$this->values = Base::of('mixed'); |
25
|
25 |
|
} |
26
|
|
|
|
27
|
25 |
|
public static function of(string $type, ...$values): self |
28
|
|
|
{ |
29
|
25 |
|
$self = new self($type); |
30
|
25 |
|
$self->values = Base::of('mixed', ...$values); |
31
|
|
|
|
32
|
25 |
|
return $self; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
15 |
|
public function type(): Str |
39
|
|
|
{ |
40
|
15 |
|
return $this->type; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
6 |
|
public function size(): int |
47
|
|
|
{ |
48
|
6 |
|
return $this->set()->size(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
5 |
|
public function count(): int |
55
|
|
|
{ |
56
|
5 |
|
return $this->size(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
23 |
|
public function toPrimitive() |
63
|
|
|
{ |
64
|
23 |
|
if ($this->loaded) { |
65
|
15 |
|
return $this->loaded->toPrimitive(); |
66
|
|
|
} |
67
|
|
|
|
68
|
23 |
|
return $this->values->reduce( |
69
|
23 |
|
[], |
70
|
23 |
|
function(array $values, $value): array { |
71
|
23 |
|
$values[] = $this->load($value); |
72
|
|
|
|
73
|
23 |
|
return $values; |
74
|
23 |
|
} |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
2 |
|
public function current() |
82
|
|
|
{ |
83
|
2 |
|
return $this->load($this->values->current()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
1 |
|
public function key() |
90
|
|
|
{ |
91
|
1 |
|
return $this->values->key(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
1 |
|
public function next() |
98
|
|
|
{ |
99
|
1 |
|
$this->values->next(); |
100
|
1 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
1 |
|
public function rewind() |
106
|
|
|
{ |
107
|
1 |
|
$this->values->rewind(); |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
1 |
|
public function valid() |
114
|
|
|
{ |
115
|
1 |
|
return $this->values->valid(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
1 |
|
public function intersect(SetInterface $set): SetInterface |
122
|
|
|
{ |
123
|
1 |
|
return $this->set()->intersect($set); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
1 |
|
public function add($element): SetInterface |
130
|
|
|
{ |
131
|
1 |
|
return $this->set()->add($element); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
1 |
|
public function contains($element): bool |
138
|
|
|
{ |
139
|
1 |
|
return $this->set()->contains($element); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
1 |
|
public function remove($element): SetInterface |
146
|
|
|
{ |
147
|
1 |
|
return $this->set()->remove($element); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
1 |
|
public function diff(SetInterface $set): SetInterface |
154
|
|
|
{ |
155
|
1 |
|
return $this->set()->diff($set); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
1 |
|
public function equals(SetInterface $set): bool |
162
|
|
|
{ |
163
|
1 |
|
return $this->set()->equals($set); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
1 |
|
public function filter(callable $predicate): SetInterface |
170
|
|
|
{ |
171
|
1 |
|
return $this->set()->filter($predicate); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* {@inheritdoc} |
176
|
|
|
*/ |
177
|
1 |
|
public function foreach(callable $function): SetInterface |
178
|
|
|
{ |
179
|
1 |
|
return $this->set()->foreach($function); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritdoc} |
184
|
|
|
*/ |
185
|
1 |
|
public function groupBy(callable $discriminator): MapInterface |
186
|
|
|
{ |
187
|
1 |
|
return $this->set()->groupBy($discriminator); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritdoc} |
192
|
|
|
*/ |
193
|
1 |
|
public function map(callable $function): SetInterface |
194
|
|
|
{ |
195
|
1 |
|
return $this->set()->map($function); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* {@inheritdoc} |
200
|
|
|
*/ |
201
|
1 |
|
public function partition(callable $predicate): MapInterface |
202
|
|
|
{ |
203
|
1 |
|
return $this->set()->partition($predicate); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* {@inheritdoc} |
208
|
|
|
*/ |
209
|
1 |
|
public function join(string $separator): Str |
210
|
|
|
{ |
211
|
1 |
|
return $this->set()->join($separator); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* {@inheritdoc} |
216
|
|
|
*/ |
217
|
1 |
|
public function sort(callable $function): StreamInterface |
218
|
|
|
{ |
219
|
1 |
|
return $this->set()->sort($function); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* {@inheritdoc} |
224
|
|
|
*/ |
225
|
2 |
|
public function merge(SetInterface $set): SetInterface |
226
|
|
|
{ |
227
|
2 |
|
if ($set instanceof self && !$this->loaded && !$set->loaded) { |
228
|
1 |
|
return self::of( |
229
|
1 |
|
(string) $this->type, |
230
|
1 |
|
...$this->values, |
231
|
1 |
|
...$set->values |
232
|
|
|
); |
233
|
|
|
} |
234
|
|
|
|
235
|
1 |
|
return $this->set()->merge($set); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* {@inheritdoc} |
240
|
|
|
*/ |
241
|
1 |
|
public function reduce($carry, callable $reducer) |
242
|
|
|
{ |
243
|
1 |
|
return $this->set()->reduce($carry, $reducer); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* {@inheritdoc} |
248
|
|
|
*/ |
249
|
1 |
|
public function clear(): SetInterface |
250
|
|
|
{ |
251
|
1 |
|
return Base::of((string) $this->type); |
252
|
|
|
} |
253
|
|
|
|
254
|
24 |
|
private function load($value) |
255
|
|
|
{ |
256
|
24 |
|
if ($value instanceof Lazy) { |
257
|
21 |
|
return $value->load(); |
258
|
|
|
} |
259
|
|
|
|
260
|
3 |
|
return $value; |
261
|
|
|
} |
262
|
|
|
|
263
|
21 |
|
private function set(): Base |
264
|
|
|
{ |
265
|
21 |
|
return $this->loaded ?? $this->loaded = Base::of( |
266
|
21 |
|
(string) $this->type, |
267
|
21 |
|
...$this->toPrimitive() |
268
|
|
|
); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|