|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\Immutable; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\Immutable\Exception\InvalidArgumentException; |
|
7
|
|
|
|
|
8
|
|
|
class Set implements SetInterface |
|
9
|
|
|
{ |
|
10
|
|
|
use Type; |
|
11
|
|
|
|
|
12
|
|
|
private $type; |
|
13
|
|
|
private $spec; |
|
14
|
|
|
private $values; |
|
15
|
|
|
|
|
16
|
24 |
|
public function __construct(string $type) |
|
17
|
|
|
{ |
|
18
|
24 |
|
$this->type = new StringPrimitive($type); |
|
|
|
|
|
|
19
|
24 |
|
$this->spec = $this->getSpecFor($type); |
|
20
|
24 |
|
$this->values = new Sequence; |
|
21
|
24 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
13 |
|
public function type(): StringPrimitive |
|
27
|
|
|
{ |
|
28
|
13 |
|
return $this->type; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
2 |
|
public function size(): int |
|
35
|
|
|
{ |
|
36
|
2 |
|
return $this->values->size(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function count(): int |
|
43
|
|
|
{ |
|
44
|
1 |
|
return $this->values->size(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
13 |
|
public function toPrimitive() |
|
51
|
|
|
{ |
|
52
|
13 |
|
return $this->values->toPrimitive(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function current() |
|
59
|
|
|
{ |
|
60
|
1 |
|
return $this->values->current(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function key() |
|
67
|
|
|
{ |
|
68
|
1 |
|
return $this->values->key(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public function next() |
|
75
|
|
|
{ |
|
76
|
1 |
|
$this->values->next(); |
|
77
|
1 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function rewind() |
|
83
|
|
|
{ |
|
84
|
1 |
|
$this->values->rewind(); |
|
85
|
1 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
1 |
|
public function valid() |
|
91
|
|
|
{ |
|
92
|
1 |
|
return $this->values->valid(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* {@inheritdoc} |
|
97
|
|
|
*/ |
|
98
|
2 |
View Code Duplication |
public function intersect(SetInterface $set): SetInterface |
|
|
|
|
|
|
99
|
|
|
{ |
|
100
|
2 |
|
$this->validate($set); |
|
101
|
|
|
|
|
102
|
1 |
|
$newSet = clone $this; |
|
103
|
1 |
|
$newSet->values = $this->values->intersect( |
|
104
|
1 |
|
new Sequence(...$set->toPrimitive()) |
|
105
|
|
|
); |
|
106
|
|
|
|
|
107
|
1 |
|
return $newSet; |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritdoc} |
|
112
|
|
|
*/ |
|
113
|
19 |
|
public function add($element): SetInterface |
|
114
|
|
|
{ |
|
115
|
19 |
|
$this->spec->validate($element); |
|
116
|
|
|
|
|
117
|
18 |
|
if ($this->contains($element)) { |
|
118
|
2 |
|
return $this; |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
18 |
|
$set = clone $this; |
|
122
|
18 |
|
$set->values = $this->values->add($element); |
|
123
|
|
|
|
|
124
|
18 |
|
return $set; |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* {@inheritdoc} |
|
129
|
|
|
*/ |
|
130
|
18 |
|
public function contains($element): bool |
|
131
|
|
|
{ |
|
132
|
18 |
|
return $this->values->contains($element); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* {@inheritdoc} |
|
137
|
|
|
*/ |
|
138
|
1 |
|
public function remove($element): SetInterface |
|
139
|
|
|
{ |
|
140
|
1 |
|
if (!$this->contains($element)) { |
|
141
|
|
|
return $this; |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
1 |
|
$index = $this->values->indexOf($element); |
|
145
|
1 |
|
$set = clone $this; |
|
146
|
1 |
|
$set->values = (new Sequence) |
|
147
|
1 |
|
->append($this->values->slice(0, $index)) |
|
148
|
1 |
|
->append($this->values->slice($index + 1, $this->size())); |
|
149
|
|
|
|
|
150
|
1 |
|
return $set; |
|
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* {@inheritdoc} |
|
155
|
|
|
*/ |
|
156
|
2 |
View Code Duplication |
public function diff(SetInterface $set): SetInterface |
|
|
|
|
|
|
157
|
|
|
{ |
|
158
|
2 |
|
$this->validate($set); |
|
159
|
|
|
|
|
160
|
1 |
|
$newSet = clone $this; |
|
161
|
1 |
|
$newSet->values = $this->values->diff( |
|
162
|
1 |
|
new Sequence(...$set->toPrimitive()) |
|
163
|
|
|
); |
|
164
|
|
|
|
|
165
|
1 |
|
return $newSet; |
|
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* {@inheritdoc} |
|
170
|
|
|
*/ |
|
171
|
3 |
|
public function equals(SetInterface $set): bool |
|
172
|
|
|
{ |
|
173
|
3 |
|
$this->validate($set); |
|
174
|
|
|
|
|
175
|
2 |
|
return $this->values->equals( |
|
176
|
2 |
|
new Sequence(...$set->toPrimitive()) |
|
177
|
|
|
); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* {@inheritdoc} |
|
182
|
|
|
*/ |
|
183
|
1 |
|
public function filter(callable $predicate): SetInterface |
|
184
|
|
|
{ |
|
185
|
1 |
|
$set = clone $this; |
|
186
|
1 |
|
$set->values = $this->values->filter($predicate); |
|
187
|
|
|
|
|
188
|
1 |
|
return $set; |
|
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* {@inheritdoc} |
|
193
|
|
|
*/ |
|
194
|
|
|
public function foreach(callable $function): SetInterface |
|
195
|
|
|
{ |
|
196
|
1 |
|
$this->values->foreach($function); |
|
197
|
|
|
|
|
198
|
1 |
|
return $this; |
|
|
|
|
|
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* {@inheritdoc} |
|
203
|
|
|
*/ |
|
204
|
1 |
|
public function groupBy(callable $discriminator): MapInterface |
|
205
|
|
|
{ |
|
206
|
1 |
|
return $this->values->groupBy($discriminator); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* {@inheritdoc} |
|
211
|
|
|
*/ |
|
212
|
2 |
|
public function map(callable $function): SetInterface |
|
213
|
|
|
{ |
|
214
|
2 |
|
$set = clone $this; |
|
215
|
2 |
|
$set->values = new Sequence; |
|
216
|
|
|
|
|
217
|
2 |
|
return $this->reduce( |
|
218
|
|
|
$set, |
|
219
|
|
|
function(self $carry, $value) use ($function): self { |
|
220
|
2 |
|
return $carry->add($function($value)); |
|
221
|
2 |
|
} |
|
222
|
|
|
); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* {@inheritdoc} |
|
227
|
|
|
*/ |
|
228
|
1 |
|
public function partition(callable $predicate): MapInterface |
|
229
|
|
|
{ |
|
230
|
1 |
|
$truthy = clone $this; |
|
231
|
1 |
|
$falsy = clone $this; |
|
232
|
1 |
|
$partitions = $this->values->partition($predicate); |
|
233
|
1 |
|
$truthy->values = $partitions->get(0); |
|
234
|
1 |
|
$falsy->values = $partitions->get(1); |
|
235
|
|
|
|
|
236
|
1 |
|
return (new Map('bool', SetInterface::class)) |
|
237
|
1 |
|
->put(true, $truthy) |
|
238
|
1 |
|
->put(false, $falsy); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* {@inheritdoc} |
|
243
|
|
|
*/ |
|
244
|
1 |
|
public function join(string $separator): StringPrimitive |
|
245
|
|
|
{ |
|
246
|
1 |
|
return $this->values->join($separator); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* {@inheritdoc} |
|
251
|
|
|
*/ |
|
252
|
1 |
|
public function sort(callable $function): SequenceInterface |
|
253
|
|
|
{ |
|
254
|
1 |
|
return $this->values->sort($function); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* {@inheritdoc} |
|
259
|
|
|
*/ |
|
260
|
2 |
|
public function merge(SetInterface $set): SetInterface |
|
261
|
|
|
{ |
|
262
|
2 |
|
$this->validate($set); |
|
263
|
|
|
|
|
264
|
1 |
|
return $set->reduce( |
|
265
|
1 |
|
clone $this, |
|
266
|
1 |
|
function(self $carry, $value): self { |
|
267
|
1 |
|
return $carry->add($value); |
|
268
|
1 |
|
} |
|
269
|
|
|
); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* {@inheritdoc} |
|
274
|
|
|
*/ |
|
275
|
4 |
|
public function reduce($carry, callable $reducer) |
|
276
|
|
|
{ |
|
277
|
4 |
|
return $this->values->reduce($carry, $reducer); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Make sure the set is compatible with the current one |
|
282
|
|
|
* |
|
283
|
|
|
* @param SetInterface $set |
|
284
|
|
|
* |
|
285
|
|
|
* @throws InvalidArgumentException |
|
286
|
|
|
* |
|
287
|
|
|
* @return void |
|
288
|
|
|
*/ |
|
289
|
8 |
|
private function validate(SetInterface $set) |
|
290
|
|
|
{ |
|
291
|
8 |
|
if (!$set->type()->equals($this->type)) { |
|
292
|
4 |
|
throw new InvalidArgumentException( |
|
293
|
4 |
|
'The 2 sets does not reference the same type' |
|
294
|
|
|
); |
|
295
|
|
|
} |
|
296
|
4 |
|
} |
|
297
|
|
|
} |
|
298
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.