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