1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Immutable; |
5
|
|
|
|
6
|
|
|
use Innmind\Immutable\{ |
7
|
|
|
Specification\ClassType, |
8
|
|
|
Exception\InvalidArgumentException, |
9
|
|
|
Exception\LogicException, |
10
|
|
|
Exception\ElementNotFoundException, |
11
|
|
|
Exception\GroupEmptyMapException |
12
|
|
|
}; |
13
|
|
|
|
14
|
|
|
final class Map implements MapInterface |
15
|
|
|
{ |
16
|
|
|
private $implementation; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
98 |
|
public function __construct(string $keyType, string $valueType) |
22
|
|
|
{ |
23
|
98 |
|
$type = Type::of($keyType); |
24
|
|
|
|
25
|
98 |
|
if ($type instanceof ClassType || $keyType === 'object') { |
26
|
4 |
|
$this->implementation = new Map\ObjectKeys($keyType, $valueType); |
27
|
94 |
|
} else if (\in_array($keyType, ['int', 'integer', 'string'], true)) { |
28
|
76 |
|
$this->implementation = new Map\Primitive($keyType, $valueType); |
29
|
|
|
} else { |
30
|
22 |
|
$this->implementation = new Map\DoubleIndex($keyType, $valueType); |
31
|
|
|
} |
32
|
98 |
|
} |
33
|
|
|
|
34
|
22 |
|
public static function of( |
35
|
|
|
string $key, |
36
|
|
|
string $value, |
37
|
|
|
array $keys = [], |
38
|
|
|
array $values = [] |
39
|
|
|
): self { |
40
|
22 |
|
$keys = \array_values($keys); |
41
|
22 |
|
$values = \array_values($values); |
42
|
|
|
|
43
|
22 |
|
if (\count($keys) !== \count($values)) { |
44
|
2 |
|
throw new LogicException('Different sizes of keys and values'); |
45
|
|
|
} |
46
|
|
|
|
47
|
20 |
|
$self = new self($key, $value); |
48
|
|
|
|
49
|
20 |
|
foreach ($keys as $i => $key) { |
50
|
2 |
|
$self = $self->put($key, $values[$i]); |
51
|
|
|
} |
52
|
|
|
|
53
|
20 |
|
return $self; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
48 |
|
public function keyType(): Str |
60
|
|
|
{ |
61
|
48 |
|
return $this->implementation->keyType(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
46 |
|
public function valueType(): Str |
68
|
|
|
{ |
69
|
46 |
|
return $this->implementation->valueType(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
22 |
|
public function size(): int |
76
|
|
|
{ |
77
|
22 |
|
return $this->implementation->size(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
6 |
|
public function count(): int |
84
|
|
|
{ |
85
|
6 |
|
return $this->implementation->count(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
2 |
|
public function current() |
92
|
|
|
{ |
93
|
2 |
|
return $this->implementation->current(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
2 |
|
public function key() |
100
|
|
|
{ |
101
|
2 |
|
return $this->implementation->key(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
2 |
|
public function next() |
108
|
|
|
{ |
109
|
2 |
|
$this->implementation->next(); |
110
|
2 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
2 |
|
public function rewind() |
116
|
|
|
{ |
117
|
2 |
|
$this->implementation->rewind(); |
118
|
2 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
2 |
|
public function valid() |
124
|
|
|
{ |
125
|
2 |
|
return $this->implementation->valid(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
2 |
|
public function offsetExists($offset): bool |
132
|
|
|
{ |
133
|
2 |
|
return $this->implementation->offsetExists($offset); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
8 |
|
public function offsetGet($offset) |
140
|
|
|
{ |
141
|
8 |
|
return $this->implementation->offsetGet($offset); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
2 |
|
public function offsetSet($offset, $value) |
148
|
|
|
{ |
149
|
2 |
|
throw new LogicException('You can\'t modify a map'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* {@inheritdoc} |
154
|
|
|
*/ |
155
|
2 |
|
public function offsetUnset($offset) |
156
|
|
|
{ |
157
|
2 |
|
throw new LogicException('You can\'t modify a map'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
*/ |
163
|
84 |
|
public function put($key, $value): MapInterface |
164
|
|
|
{ |
165
|
84 |
|
$map = clone $this; |
166
|
84 |
|
$map->implementation = $this->implementation->put($key, $value); |
167
|
|
|
|
168
|
78 |
|
return $map; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Alias for put method in order to have a syntax similar to a true tuple |
173
|
|
|
* when constructing the map |
174
|
|
|
* |
175
|
|
|
* Example: |
176
|
|
|
* <code> |
177
|
|
|
* Map::of('int', 'int') |
178
|
|
|
* (1, 2) |
179
|
|
|
* (3, 4) |
180
|
|
|
* </code> |
181
|
|
|
* |
182
|
|
|
* @param T $key |
183
|
|
|
* @param S $value |
184
|
|
|
* |
185
|
|
|
* @return self<T, S> |
186
|
|
|
*/ |
187
|
16 |
|
public function __invoke($key, $value): self |
188
|
|
|
{ |
189
|
16 |
|
return $this->put($key, $value); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* {@inheritdoc} |
194
|
|
|
*/ |
195
|
48 |
|
public function get($key) |
196
|
|
|
{ |
197
|
48 |
|
return $this->implementation->get($key); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* {@inheritdoc} |
202
|
|
|
*/ |
203
|
28 |
|
public function contains($key): bool |
204
|
|
|
{ |
205
|
28 |
|
return $this->implementation->contains($key); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* {@inheritdoc} |
210
|
|
|
*/ |
211
|
10 |
|
public function clear(): MapInterface |
212
|
|
|
{ |
213
|
10 |
|
$map = clone $this; |
214
|
10 |
|
$map->implementation = $this->implementation->clear(); |
215
|
|
|
|
216
|
10 |
|
return $map; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* {@inheritdoc} |
221
|
|
|
*/ |
222
|
12 |
|
public function equals(MapInterface $map): bool |
223
|
|
|
{ |
224
|
12 |
|
return $this->implementation->equals($map); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* {@inheritdoc} |
229
|
|
|
*/ |
230
|
2 |
|
public function filter(callable $predicate): MapInterface |
231
|
|
|
{ |
232
|
2 |
|
$map = $this->clear(); |
233
|
2 |
|
$map->implementation = $this->implementation->filter($predicate); |
234
|
|
|
|
235
|
2 |
|
return $map; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* {@inheritdoc} |
240
|
|
|
*/ |
241
|
2 |
|
public function foreach(callable $function): MapInterface |
242
|
|
|
{ |
243
|
2 |
|
$this->implementation->foreach($function); |
244
|
|
|
|
245
|
2 |
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* {@inheritdoc} |
250
|
|
|
*/ |
251
|
4 |
|
public function groupBy(callable $discriminator): MapInterface |
252
|
|
|
{ |
253
|
4 |
|
return $this->implementation->groupBy($discriminator); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* {@inheritdoc} |
258
|
|
|
*/ |
259
|
22 |
|
public function keys(): SetInterface |
260
|
|
|
{ |
261
|
22 |
|
return $this->implementation->keys(); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* {@inheritdoc} |
266
|
|
|
*/ |
267
|
10 |
|
public function values(): StreamInterface |
268
|
|
|
{ |
269
|
10 |
|
return $this->implementation->values(); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* {@inheritdoc} |
274
|
|
|
*/ |
275
|
6 |
|
public function map(callable $function): MapInterface |
276
|
|
|
{ |
277
|
6 |
|
$map = $this->clear(); |
278
|
6 |
|
$map->implementation = $this->implementation->map($function); |
279
|
|
|
|
280
|
2 |
|
return $map; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* {@inheritdoc} |
285
|
|
|
*/ |
286
|
2 |
|
public function join(string $separator): Str |
287
|
|
|
{ |
288
|
2 |
|
return $this->implementation->join($separator); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* {@inheritdoc} |
293
|
|
|
*/ |
294
|
2 |
|
public function remove($key): MapInterface |
295
|
|
|
{ |
296
|
2 |
|
$map = clone $this; |
297
|
2 |
|
$map->implementation = $this->implementation->remove($key); |
298
|
|
|
|
299
|
2 |
|
return $map; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* {@inheritdoc} |
304
|
|
|
*/ |
305
|
4 |
|
public function merge(MapInterface $map): MapInterface |
306
|
|
|
{ |
307
|
4 |
|
$self = clone $this; |
308
|
4 |
|
$self->implementation = $this->implementation->merge($map); |
309
|
|
|
|
310
|
2 |
|
return $self; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* {@inheritdoc} |
315
|
|
|
*/ |
316
|
2 |
|
public function partition(callable $predicate): MapInterface |
317
|
|
|
{ |
318
|
2 |
|
return $this->implementation->partition($predicate); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* {@inheritdoc} |
323
|
|
|
*/ |
324
|
6 |
|
public function reduce($carry, callable $reducer) |
325
|
|
|
{ |
326
|
6 |
|
return $this->implementation->reduce($carry, $reducer); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
public function empty(): bool |
330
|
|
|
{ |
331
|
|
|
return $this->implementation->empty(); |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
|