1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Immutable; |
5
|
|
|
|
6
|
|
|
use Innmind\Immutable\{ |
7
|
|
|
Exception\InvalidArgumentException, |
8
|
|
|
Exception\LogicException, |
9
|
|
|
Exception\ElementNotFoundException, |
10
|
|
|
Exception\GroupEmptyMapException |
11
|
|
|
}; |
12
|
|
|
|
13
|
|
|
final class Map implements MapInterface |
14
|
|
|
{ |
15
|
|
|
use Type; |
16
|
|
|
|
17
|
|
|
private $keyType; |
18
|
|
|
private $valueType; |
19
|
|
|
private $keySpecification; |
20
|
|
|
private $valueSpecification; |
21
|
|
|
private $keys; |
22
|
|
|
private $values; |
23
|
|
|
private $pairs; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
120 |
|
public function __construct(string $keyType, string $valueType) |
29
|
|
|
{ |
30
|
120 |
|
$this->keySpecification = $this->getSpecificationFor($keyType); |
31
|
120 |
|
$this->valueSpecification = $this->getSpecificationFor($valueType); |
32
|
120 |
|
$this->keyType = new Str($keyType); |
33
|
120 |
|
$this->valueType = new Str($valueType); |
34
|
120 |
|
$this->keys = new Stream($keyType); |
35
|
120 |
|
$this->values = new Stream($valueType); |
36
|
120 |
|
$this->pairs = new Stream(Pair::class); |
37
|
120 |
|
} |
38
|
|
|
|
39
|
5 |
|
public static function of( |
40
|
|
|
string $key, |
41
|
|
|
string $value, |
42
|
|
|
array $keys, |
43
|
|
|
array $values |
44
|
|
|
): self { |
45
|
5 |
|
$keys = \array_values($keys); |
46
|
5 |
|
$values = \array_values($values); |
47
|
|
|
|
48
|
5 |
|
if (\count($keys) !== \count($values)) { |
49
|
3 |
|
throw new LogicException('Different sizes of keys and values'); |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
$self = new self($key, $value); |
53
|
|
|
|
54
|
2 |
|
foreach ($keys as $i => $key) { |
55
|
2 |
|
$self = $self->put($key, $values[$i]); |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
return $self; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
51 |
|
public function keyType(): Str |
65
|
|
|
{ |
66
|
51 |
|
return $this->keyType; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
48 |
|
public function valueType(): Str |
73
|
|
|
{ |
74
|
48 |
|
return $this->valueType; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
24 |
|
public function size(): int |
81
|
|
|
{ |
82
|
24 |
|
return $this->keys->size(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
9 |
|
public function count(): int |
89
|
|
|
{ |
90
|
9 |
|
return $this->keys->count(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
3 |
|
public function current() |
97
|
|
|
{ |
98
|
3 |
|
return $this->values->current(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
3 |
|
public function key() |
105
|
|
|
{ |
106
|
3 |
|
return $this->keys->current(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
3 |
|
public function next() |
113
|
|
|
{ |
114
|
3 |
|
$this->keys->next(); |
115
|
3 |
|
$this->values->next(); |
116
|
3 |
|
$this->pairs->next(); |
117
|
3 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
3 |
|
public function rewind() |
123
|
|
|
{ |
124
|
3 |
|
$this->keys->rewind(); |
125
|
3 |
|
$this->values->rewind(); |
126
|
3 |
|
$this->pairs->rewind(); |
127
|
3 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
3 |
|
public function valid() |
133
|
|
|
{ |
134
|
3 |
|
return $this->keys->valid(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
3 |
|
public function offsetExists($offset): bool |
141
|
|
|
{ |
142
|
3 |
|
return $this->keys->contains($offset); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
12 |
|
public function offsetGet($offset) |
149
|
|
|
{ |
150
|
12 |
|
return $this->get($offset); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
3 |
|
public function offsetSet($offset, $value) |
157
|
|
|
{ |
158
|
3 |
|
throw new LogicException('You can\'t modify a map'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritdoc} |
163
|
|
|
*/ |
164
|
3 |
|
public function offsetUnset($offset) |
165
|
|
|
{ |
166
|
3 |
|
throw new LogicException('You can\'t modify a map'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
99 |
|
public function put($key, $value): MapInterface |
173
|
|
|
{ |
174
|
99 |
|
$this->keySpecification->validate($key); |
175
|
96 |
|
$this->valueSpecification->validate($value); |
176
|
|
|
|
177
|
90 |
|
$map = clone $this; |
178
|
|
|
|
179
|
90 |
|
if ($this->keys->contains($key)) { |
180
|
20 |
|
$index = $this->keys->indexOf($key); |
181
|
20 |
|
$map->values = $this->values->take($index) |
182
|
20 |
|
->add($value) |
183
|
20 |
|
->append($this->values->drop($index + 1)); |
184
|
20 |
|
$map->pairs = $this->pairs->take($index) |
185
|
20 |
|
->add(new Pair($key, $value)) |
186
|
20 |
|
->append($this->pairs->drop($index + 1)); |
187
|
|
|
} else { |
188
|
90 |
|
$map->keys = $this->keys->add($key); |
189
|
90 |
|
$map->values = $this->values->add($value); |
190
|
90 |
|
$map->pairs = $this->pairs->add(new Pair($key, $value)); |
191
|
|
|
} |
192
|
|
|
|
193
|
90 |
|
return $map; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inheritdoc} |
198
|
|
|
*/ |
199
|
53 |
|
public function get($key) |
200
|
|
|
{ |
201
|
53 |
|
if (!$this->keys->contains($key)) { |
202
|
6 |
|
throw new ElementNotFoundException; |
203
|
|
|
} |
204
|
|
|
|
205
|
47 |
|
return $this->values->get( |
206
|
47 |
|
$this->keys->indexOf($key) |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* {@inheritdoc} |
212
|
|
|
*/ |
213
|
21 |
|
public function contains($key): bool |
214
|
|
|
{ |
215
|
21 |
|
return $this->keys->contains($key); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* {@inheritdoc} |
220
|
|
|
*/ |
221
|
21 |
|
public function clear(): MapInterface |
222
|
|
|
{ |
223
|
21 |
|
$map = clone $this; |
224
|
21 |
|
$map->keys = $this->keys->clear(); |
225
|
21 |
|
$map->values = $this->values->clear(); |
226
|
21 |
|
$map->pairs = $this->pairs->clear(); |
227
|
|
|
|
228
|
21 |
|
return $map; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* {@inheritdoc} |
233
|
|
|
*/ |
234
|
6 |
|
public function equals(MapInterface $map): bool |
235
|
|
|
{ |
236
|
6 |
|
if (!$map->keys()->equals($this->keys())) { |
237
|
2 |
|
return false; |
238
|
|
|
} |
239
|
|
|
|
240
|
6 |
|
foreach ($this->pairs as $pair) { |
241
|
6 |
|
if ($map->get($pair->key()) !== $pair->value()) { |
242
|
6 |
|
return false; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
4 |
|
return true; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* {@inheritdoc} |
251
|
|
|
*/ |
252
|
3 |
|
public function filter(callable $predicate): MapInterface |
253
|
|
|
{ |
254
|
3 |
|
$map = $this->clear(); |
255
|
|
|
|
256
|
3 |
|
foreach ($this->pairs as $pair) { |
257
|
3 |
|
if ($predicate($pair->key(), $pair->value()) === true) { |
258
|
3 |
|
$map->keys = $map->keys->add($pair->key()); |
259
|
3 |
|
$map->values = $map->values->add($pair->value()); |
260
|
3 |
|
$map->pairs = $map->pairs->add($pair); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
3 |
|
return $map; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* {@inheritdoc} |
269
|
|
|
*/ |
270
|
3 |
|
public function foreach(callable $function): MapInterface |
271
|
|
|
{ |
272
|
3 |
|
foreach ($this->pairs as $pair) { |
273
|
3 |
|
$function($pair->key(), $pair->value()); |
274
|
|
|
} |
275
|
|
|
|
276
|
3 |
|
return $this; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* {@inheritdoc} |
281
|
|
|
*/ |
282
|
6 |
|
public function groupBy(callable $discriminator): MapInterface |
283
|
|
|
{ |
284
|
6 |
|
if ($this->size() === 0) { |
285
|
3 |
|
throw new GroupEmptyMapException; |
286
|
|
|
} |
287
|
|
|
|
288
|
3 |
|
$map = null; |
289
|
|
|
|
290
|
3 |
|
foreach ($this->pairs as $pair) { |
291
|
3 |
|
$key = $discriminator($pair->key(), $pair->value()); |
292
|
|
|
|
293
|
3 |
|
if ($map === null) { |
294
|
3 |
|
$map = new self( |
295
|
3 |
|
$this->determineType($key), |
296
|
3 |
|
MapInterface::class |
297
|
|
|
); |
298
|
|
|
} |
299
|
|
|
|
300
|
3 |
|
if ($map->contains($key)) { |
301
|
3 |
|
$map = $map->put( |
302
|
3 |
|
$key, |
303
|
3 |
|
$map->get($key)->put( |
304
|
3 |
|
$pair->key(), |
305
|
3 |
|
$pair->value() |
306
|
|
|
) |
307
|
|
|
); |
308
|
|
|
} else { |
309
|
3 |
|
$map = $map->put( |
310
|
3 |
|
$key, |
311
|
3 |
|
$this->clear()->put( |
312
|
3 |
|
$pair->key(), |
313
|
3 |
|
$pair->value() |
314
|
|
|
) |
315
|
|
|
); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
319
|
3 |
|
return $map; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* {@inheritdoc} |
324
|
|
|
*/ |
325
|
27 |
|
public function keys(): SetInterface |
326
|
|
|
{ |
327
|
27 |
|
return Set::of((string) $this->keyType, ...$this->keys); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* {@inheritdoc} |
332
|
|
|
*/ |
333
|
13 |
|
public function values(): StreamInterface |
334
|
|
|
{ |
335
|
13 |
|
return $this->values; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* {@inheritdoc} |
340
|
|
|
*/ |
341
|
9 |
|
public function map(callable $function): MapInterface |
342
|
|
|
{ |
343
|
9 |
|
$map = $this->clear(); |
344
|
|
|
|
345
|
9 |
|
foreach ($this->pairs as $pair) { |
346
|
9 |
|
$return = $function( |
347
|
9 |
|
$pair->key(), |
348
|
9 |
|
$pair->value() |
349
|
|
|
); |
350
|
|
|
|
351
|
9 |
|
if ($return instanceof Pair) { |
352
|
6 |
|
$key = $return->key(); |
353
|
6 |
|
$value = $return->value(); |
354
|
|
|
} else { |
355
|
6 |
|
$key = $pair->key(); |
356
|
6 |
|
$value = $return; |
357
|
|
|
} |
358
|
|
|
|
359
|
9 |
|
$map = $map->put($key, $value); |
360
|
|
|
} |
361
|
|
|
|
362
|
3 |
|
return $map; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* {@inheritdoc} |
367
|
|
|
*/ |
368
|
3 |
|
public function join(string $separator): Str |
369
|
|
|
{ |
370
|
3 |
|
return $this->values->join($separator); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* {@inheritdoc} |
375
|
|
|
*/ |
376
|
3 |
|
public function remove($key): MapInterface |
377
|
|
|
{ |
378
|
3 |
|
if (!$this->contains($key)) { |
379
|
3 |
|
return $this; |
380
|
|
|
} |
381
|
|
|
|
382
|
2 |
|
$index = $this->keys->indexOf($key); |
383
|
2 |
|
$map = clone $this; |
384
|
2 |
|
$map->keys = $this |
385
|
2 |
|
->keys |
386
|
2 |
|
->slice(0, $index) |
387
|
2 |
|
->append($this->keys->slice($index + 1, $this->keys->size())); |
388
|
2 |
|
$map->values = $this |
389
|
2 |
|
->values |
390
|
2 |
|
->slice(0, $index) |
391
|
2 |
|
->append($this->values->slice($index + 1, $this->values->size())); |
392
|
2 |
|
$map->pairs = $this |
393
|
2 |
|
->pairs |
394
|
2 |
|
->slice(0, $index) |
395
|
2 |
|
->append($this->pairs->slice($index + 1, $this->pairs->size())); |
396
|
|
|
|
397
|
2 |
|
return $map; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* {@inheritdoc} |
402
|
|
|
*/ |
403
|
6 |
|
public function merge(MapInterface $map): MapInterface |
404
|
|
|
{ |
405
|
|
|
if ( |
406
|
6 |
|
!$this->keyType()->equals($map->keyType()) || |
407
|
6 |
|
!$this->valueType()->equals($map->valueType()) |
408
|
|
|
) { |
409
|
3 |
|
throw new InvalidArgumentException( |
410
|
3 |
|
'The 2 maps does not reference the same types' |
411
|
|
|
); |
412
|
|
|
} |
413
|
|
|
|
414
|
3 |
|
return $map->reduce( |
415
|
3 |
|
$this, |
416
|
3 |
|
function(self $carry, $key, $value): self { |
417
|
3 |
|
return $carry->put($key, $value); |
418
|
3 |
|
} |
419
|
|
|
); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* {@inheritdoc} |
424
|
|
|
*/ |
425
|
3 |
|
public function partition(callable $predicate): MapInterface |
426
|
|
|
{ |
427
|
3 |
|
$truthy = $this->clear(); |
428
|
3 |
|
$falsy = $this->clear(); |
429
|
|
|
|
430
|
3 |
|
foreach ($this->pairs as $pair) { |
431
|
3 |
|
$return = $predicate( |
432
|
3 |
|
$pair->key(), |
433
|
3 |
|
$pair->value() |
434
|
|
|
); |
435
|
|
|
|
436
|
3 |
|
if ($return === true) { |
437
|
3 |
|
$truthy = $truthy->put($pair->key(), $pair->value()); |
438
|
|
|
} else { |
439
|
3 |
|
$falsy = $falsy->put($pair->key(), $pair->value()); |
440
|
|
|
} |
441
|
|
|
} |
442
|
|
|
|
443
|
3 |
|
return (new self('bool', MapInterface::class)) |
444
|
3 |
|
->put(true, $truthy) |
445
|
3 |
|
->put(false, $falsy); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* {@inheritdoc} |
450
|
|
|
*/ |
451
|
9 |
|
public function reduce($carry, callable $reducer) |
452
|
|
|
{ |
453
|
9 |
|
foreach ($this->pairs as $pair) { |
454
|
9 |
|
$carry = $reducer($carry, $pair->key(), $pair->value()); |
455
|
|
|
} |
456
|
|
|
|
457
|
9 |
|
return $carry; |
458
|
|
|
} |
459
|
|
|
} |
460
|
|
|
|