1 | <?php |
||
2 | declare(strict_types = 1); |
||
3 | |||
4 | namespace Innmind\Immutable\Set; |
||
5 | |||
6 | use Innmind\Immutable\{ |
||
7 | Map, |
||
8 | Sequence, |
||
9 | Set, |
||
10 | Type, |
||
11 | ValidateArgument, |
||
12 | Str, |
||
13 | Exception\CannotGroupEmptyStructure, |
||
14 | }; |
||
15 | |||
16 | /** |
||
17 | * @template T |
||
18 | */ |
||
19 | final class Primitive implements Implementation |
||
20 | { |
||
21 | private string $type; |
||
22 | private ValidateArgument $validate; |
||
23 | private Sequence $values; |
||
24 | |||
25 | /** |
||
26 | * @param T $values |
||
27 | */ |
||
28 | 89 | public function __construct(string $type, ...$values) |
|
29 | { |
||
30 | 89 | $this->type = $type; |
|
31 | 89 | $this->validate = Type::of($type); |
|
32 | 89 | $this->values = Sequence::of($type, ...$values)->distinct(); |
|
33 | 89 | } |
|
34 | |||
35 | 1 | public function isOfType(string $type): bool |
|
36 | { |
||
37 | 1 | return $this->type === $type; |
|
38 | } |
||
39 | |||
40 | 1 | public function type(): string |
|
41 | { |
||
42 | 1 | return $this->type; |
|
43 | } |
||
44 | |||
45 | 14 | public function size(): int |
|
46 | { |
||
47 | 14 | return $this->values->size(); |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | 1 | public function count(): int |
|
54 | { |
||
55 | 1 | return $this->values->size(); |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * @return list<T> |
||
60 | */ |
||
61 | 60 | public function toArray(): array |
|
62 | { |
||
63 | 60 | return $this->values->toArray(); |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param Implementation<T> $set |
||
68 | * |
||
69 | * @return self<T> |
||
70 | */ |
||
71 | 12 | public function intersect(Implementation $set): self |
|
72 | { |
||
73 | 12 | $self = $this->clear(); |
|
74 | 12 | $self->values = $this->values->intersect( |
|
75 | 12 | Sequence::of($this->type, ...$set->toArray()) |
|
76 | ); |
||
77 | |||
78 | 12 | return $self; |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param T $element |
||
83 | * |
||
84 | * @return self<T> |
||
85 | */ |
||
86 | 36 | public function add($element): self |
|
87 | { |
||
88 | 36 | if ($this->contains($element)) { |
|
89 | 4 | return $this; |
|
90 | } |
||
91 | |||
92 | 36 | $set = clone $this; |
|
93 | 36 | $set->values = ($this->values)($element); |
|
94 | |||
95 | 36 | return $set; |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param T $element |
||
100 | */ |
||
101 | 38 | public function contains($element): bool |
|
102 | { |
||
103 | 38 | return $this->values->contains($element); |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * @param T $element |
||
108 | * |
||
109 | * @return self<T> |
||
110 | */ |
||
111 | 2 | public function remove($element): self |
|
112 | { |
||
113 | 2 | if (!$this->contains($element)) { |
|
114 | 1 | return $this; |
|
115 | } |
||
116 | |||
117 | 2 | $index = $this->values->indexOf($element); |
|
118 | 2 | $set = clone $this; |
|
119 | 2 | $set->values = $this |
|
120 | 2 | ->values |
|
121 | 2 | ->clear() |
|
122 | 2 | ->append($this->values->slice(0, $index)) |
|
123 | 2 | ->append($this->values->slice($index + 1, $this->size())); |
|
124 | |||
125 | 2 | return $set; |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * @param Implementation<T> $set |
||
130 | * |
||
131 | * @return self<T> |
||
132 | */ |
||
133 | 2 | public function diff(Implementation $set): self |
|
134 | { |
||
135 | 2 | $self = clone $this; |
|
136 | 2 | $self->values = $this->values->diff( |
|
137 | 2 | Sequence::of($this->type, ...$set->toArray()) |
|
138 | ); |
||
139 | |||
140 | 2 | return $self; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param Implementation<T> $set |
||
145 | */ |
||
146 | 10 | public function equals(Implementation $set): bool |
|
147 | { |
||
148 | 10 | if ($this->size() !== $set->size()) { |
|
149 | 3 | return false; |
|
150 | } |
||
151 | |||
152 | 10 | return $this->intersect($set)->size() === $this->size(); |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param callable(T): bool $predicate |
||
157 | * |
||
158 | * @return self<T> |
||
159 | */ |
||
160 | 2 | public function filter(callable $predicate): self |
|
161 | { |
||
162 | 2 | $set = clone $this; |
|
163 | 2 | $set->values = $this->values->filter($predicate); |
|
164 | |||
165 | 2 | return $set; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param callable(T): void $function |
||
170 | */ |
||
171 | 2 | public function foreach(callable $function): void |
|
172 | { |
||
173 | 2 | $this->values->foreach($function); |
|
174 | 2 | } |
|
175 | |||
176 | /** |
||
177 | * @template D |
||
178 | * @param callable(T): D $discriminator |
||
179 | * |
||
180 | * @throws CannotGroupEmptyStructure |
||
181 | * |
||
182 | * @return Map<D, Set<T>> |
||
183 | */ |
||
184 | 2 | public function groupBy(callable $discriminator): Map |
|
185 | { |
||
186 | /** @var Map<D, Sequence<T>> */ |
||
187 | 2 | $map = $this->values->groupBy($discriminator); |
|
188 | |||
189 | /** |
||
190 | * @psalm-suppress MixedReturnTypeCoercion |
||
191 | * @var Map<D, Set<T>> |
||
192 | */ |
||
193 | 2 | return $map->reduce( |
|
194 | 2 | Map::of($map->keyType(), Set::class), |
|
195 | function(Map $carry, $key, Sequence $values): Map { |
||
196 | 2 | return ($carry)( |
|
197 | 2 | $key, |
|
198 | 2 | Set::of($this->type, ...$values->toArray()), |
|
199 | ); |
||
200 | 2 | } |
|
201 | ); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @param callable(T): T $function |
||
206 | * |
||
207 | * @return self<T> |
||
208 | */ |
||
209 | 4 | public function map(callable $function): self |
|
210 | { |
||
211 | /** |
||
212 | * @psalm-suppress MissingClosureParamType |
||
213 | * @psalm-suppress MissingClosureReturnType |
||
214 | */ |
||
215 | $function = function($value) use ($function) { |
||
216 | /** @var T $value */ |
||
217 | 4 | $returned = $function($value); |
|
218 | 4 | ($this->validate)($returned, 1); |
|
219 | |||
220 | 2 | return $returned; |
|
221 | 4 | }; |
|
222 | |||
223 | 4 | return $this->reduce( |
|
224 | 4 | $this->clear(), |
|
225 | function(self $carry, $value) use ($function): self { |
||
226 | 4 | return $carry->add($function($value)); |
|
227 | 4 | } |
|
228 | ); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * @param callable(T): bool $predicate |
||
233 | * |
||
234 | * @return Map<bool, Set<T>> |
||
235 | */ |
||
236 | 2 | public function partition(callable $predicate): Map |
|
237 | { |
||
238 | 2 | $partitions = $this->values->partition($predicate); |
|
239 | /** @var Set<T> */ |
||
240 | 2 | $truthy = Set::of($this->type, ...$partitions->get(true)->toArray()); |
|
241 | /** @var Set<T> */ |
||
242 | 2 | $falsy = Set::of($this->type, ...$partitions->get(false)->toArray()); |
|
243 | |||
244 | /** |
||
245 | * @psalm-suppress InvalidScalarArgument |
||
246 | * @psalm-suppress InvalidArgument |
||
247 | */ |
||
248 | 2 | return Map::of('bool', Set::class) |
|
249 | 2 | (true, $truthy) |
|
250 | 2 | (false, $falsy); |
|
251 | } |
||
252 | |||
253 | 2 | public function join(string $separator): Str |
|
254 | { |
||
255 | 2 | return $this->values->join($separator); |
|
256 | } |
||
257 | |||
258 | /** |
||
259 | * @param callable(T, T): int $function |
||
260 | * |
||
261 | * @return Sequence<T> |
||
262 | */ |
||
263 | 2 | public function sort(callable $function): Sequence |
|
264 | { |
||
265 | 2 | return $this->values->sort($function); |
|
266 | } |
||
267 | |||
268 | /** |
||
269 | * @param Implementation<T> $set |
||
270 | * |
||
271 | * @return self<T> |
||
272 | */ |
||
273 | 2 | public function merge(Implementation $set): self |
|
274 | { |
||
275 | 2 | return $set->reduce( |
|
276 | 2 | $this, |
|
277 | function(self $carry, $value): self { |
||
278 | 2 | return $carry->add($value); |
|
279 | 2 | } |
|
280 | ); |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * @template R |
||
285 | * @param R $carry |
||
286 | * @param callable(R, T): R $reducer |
||
287 | * |
||
288 | * @return R |
||
289 | */ |
||
290 | 17 | public function reduce($carry, callable $reducer) |
|
291 | { |
||
292 | 17 | return $this->values->reduce($carry, $reducer); |
|
293 | } |
||
294 | |||
295 | /** |
||
296 | * @return self<T> |
||
297 | */ |
||
298 | 17 | public function clear(): self |
|
299 | { |
||
300 | 17 | return new self($this->type); |
|
301 | } |
||
302 | |||
303 | 2 | public function empty(): bool |
|
304 | { |
||
305 | 2 | return $this->values->empty(); |
|
306 | } |
||
307 | |||
308 | /** |
||
309 | * @template ST |
||
310 | * |
||
311 | * @param callable(T): \Generator<ST> $mapper |
||
312 | * |
||
313 | * @return Set<ST> |
||
314 | */ |
||
315 | 2 | public function toSetOf(string $type, callable $mapper): Set |
|
316 | { |
||
317 | 2 | return $this->values->toSetOf($type, $mapper); |
|
318 | } |
||
319 | } |
||
320 |