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