1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Immutable\Map; |
5
|
|
|
|
6
|
|
|
use Innmind\Immutable\{ |
7
|
|
|
Map, |
8
|
|
|
Type, |
9
|
|
|
Str, |
10
|
|
|
Sequence, |
11
|
|
|
Set, |
12
|
|
|
Pair, |
13
|
|
|
ValidateArgument, |
14
|
|
|
ValidateArgument\ClassType, |
15
|
|
|
Exception\LogicException, |
16
|
|
|
Exception\ElementNotFound, |
17
|
|
|
Exception\CannotGroupEmptyStructure, |
18
|
|
|
}; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
final class ObjectKeys implements Implementation |
24
|
|
|
{ |
25
|
|
|
private string $keyType; |
26
|
|
|
private string $valueType; |
27
|
|
|
private ValidateArgument $validateKey; |
28
|
|
|
private ValidateArgument $validateValue; |
29
|
|
|
private \SplObjectStorage $values; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
26 |
|
public function __construct(string $keyType, string $valueType) |
35
|
|
|
{ |
36
|
26 |
|
$this->validateKey = Type::of($keyType); |
37
|
|
|
|
38
|
26 |
|
if (!$this->validateKey instanceof ClassType && $keyType !== 'object') { |
39
|
1 |
|
throw new LogicException; |
40
|
|
|
} |
41
|
|
|
|
42
|
25 |
|
$this->validateValue = Type::of($valueType); |
43
|
25 |
|
$this->keyType = $keyType; |
44
|
25 |
|
$this->valueType = $valueType; |
45
|
25 |
|
$this->values = new \SplObjectStorage; |
46
|
25 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
6 |
|
public function keyType(): string |
52
|
|
|
{ |
53
|
6 |
|
return $this->keyType; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
5 |
|
public function valueType(): string |
60
|
|
|
{ |
61
|
5 |
|
return $this->valueType; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
7 |
|
public function size(): int |
68
|
|
|
{ |
69
|
7 |
|
return $this->values->count(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function count(): int |
76
|
|
|
{ |
77
|
|
|
return $this->size(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
21 |
|
public function put($key, $value): Implementation |
84
|
|
|
{ |
85
|
21 |
|
($this->validateKey)($key, 1); |
86
|
20 |
|
($this->validateValue)($value, 2); |
87
|
|
|
|
88
|
19 |
|
$map = clone $this; |
89
|
19 |
|
$map->values = clone $this->values; |
90
|
19 |
|
$map->values[$key] = $value; |
91
|
19 |
|
$map->values->rewind(); |
92
|
|
|
|
93
|
19 |
|
return $map; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
5 |
|
public function get($key) |
100
|
|
|
{ |
101
|
5 |
|
if (!$this->contains($key)) { |
102
|
1 |
|
throw new ElementNotFound($key); |
103
|
|
|
} |
104
|
|
|
|
105
|
4 |
|
return $this->values->offsetGet($key); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
8 |
|
public function contains($key): bool |
112
|
|
|
{ |
113
|
8 |
|
if (!is_object($key)) { |
114
|
1 |
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
8 |
|
return $this->values->offsetExists($key); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
5 |
|
public function clear(): Implementation |
124
|
|
|
{ |
125
|
5 |
|
$map = clone $this; |
126
|
5 |
|
$map->values = new \SplObjectStorage; |
127
|
|
|
|
128
|
5 |
|
return $map; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
2 |
|
public function equals(Implementation $map): bool |
135
|
|
|
{ |
136
|
2 |
|
if ($map->size() !== $this->size()) { |
137
|
1 |
|
return false; |
138
|
|
|
} |
139
|
|
|
|
140
|
2 |
|
foreach ($this->values as $k) { |
141
|
2 |
|
$v = $this->values[$k]; |
142
|
|
|
|
143
|
2 |
|
if (!$map->contains($k)) { |
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
|
147
|
2 |
|
if ($map->get($k) !== $v) { |
148
|
2 |
|
return false; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
return true; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
1 |
|
public function filter(callable $predicate): Implementation |
159
|
|
|
{ |
160
|
1 |
|
$map = $this->clear(); |
161
|
|
|
|
162
|
1 |
|
foreach ($this->values as $k) { |
163
|
1 |
|
$v = $this->values[$k]; |
164
|
|
|
|
165
|
1 |
|
if ($predicate($k, $v) === true) { |
166
|
1 |
|
$map->values[$k] = $v; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
1 |
|
$map->values->rewind(); |
171
|
|
|
|
172
|
1 |
|
return $map; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* {@inheritdoc} |
177
|
|
|
*/ |
178
|
1 |
|
public function foreach(callable $function): void |
179
|
|
|
{ |
180
|
1 |
|
foreach ($this->values as $k) { |
181
|
1 |
|
$v = $this->values[$k]; |
182
|
|
|
|
183
|
1 |
|
$function($k, $v); |
184
|
|
|
} |
185
|
1 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* {@inheritdoc} |
189
|
|
|
*/ |
190
|
2 |
|
public function groupBy(callable $discriminator): Map |
191
|
|
|
{ |
192
|
2 |
|
if ($this->size() === 0) { |
193
|
1 |
|
throw new CannotGroupEmptyStructure; |
194
|
|
|
} |
195
|
|
|
|
196
|
1 |
|
$map = null; |
197
|
|
|
|
198
|
1 |
|
foreach ($this->values as $k) { |
199
|
1 |
|
$v = $this->values[$k]; |
200
|
|
|
|
201
|
1 |
|
$key = $discriminator($k, $v); |
202
|
|
|
|
203
|
1 |
|
if ($map === null) { |
204
|
1 |
|
$map = Map::of( |
205
|
1 |
|
Type::determine($key), |
206
|
1 |
|
Map::class |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
|
210
|
1 |
|
if ($map->contains($key)) { |
211
|
1 |
|
$map = $map->put( |
212
|
1 |
|
$key, |
213
|
1 |
|
$map->get($key)->put($k, $v) |
214
|
|
|
); |
215
|
|
|
} else { |
216
|
1 |
|
$map = $map->put( |
217
|
1 |
|
$key, |
218
|
1 |
|
$this->clearMap()->put($k, $v) |
219
|
|
|
); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
1 |
|
return $map; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* {@inheritdoc} |
228
|
|
|
*/ |
229
|
5 |
|
public function keys(): Set |
230
|
|
|
{ |
231
|
5 |
|
return $this->reduce( |
232
|
5 |
|
Set::of($this->keyType), |
233
|
|
|
static function(Set $keys, $key): Set { |
234
|
5 |
|
return $keys->add($key); |
235
|
5 |
|
} |
236
|
|
|
); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* {@inheritdoc} |
241
|
|
|
*/ |
242
|
6 |
|
public function values(): Sequence |
243
|
|
|
{ |
244
|
6 |
|
return $this->reduce( |
245
|
6 |
|
Sequence::of($this->valueType), |
246
|
|
|
static function(Sequence $values, $key, $value): Sequence { |
247
|
6 |
|
return $values->add($value); |
248
|
6 |
|
} |
249
|
|
|
); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* {@inheritdoc} |
254
|
|
|
*/ |
255
|
3 |
|
public function map(callable $function): Implementation |
256
|
|
|
{ |
257
|
3 |
|
$map = $this->clear(); |
258
|
|
|
|
259
|
3 |
|
foreach ($this->values as $k) { |
260
|
3 |
|
$v = $this->values[$k]; |
261
|
|
|
|
262
|
3 |
|
$return = $function($k, $v); |
263
|
|
|
|
264
|
3 |
|
if ($return instanceof Pair) { |
265
|
2 |
|
($this->validateKey)($return->key(), 1); |
266
|
|
|
|
267
|
1 |
|
$key = $return->key(); |
268
|
1 |
|
$value = $return->value(); |
269
|
|
|
} else { |
270
|
2 |
|
$key = $k; |
271
|
2 |
|
$value = $return; |
272
|
|
|
} |
273
|
|
|
|
274
|
2 |
|
($this->validateValue)($value, 2); |
275
|
|
|
|
276
|
1 |
|
$map->values[$key] = $value; |
277
|
|
|
} |
278
|
|
|
|
279
|
1 |
|
$map->values->rewind(); |
280
|
|
|
|
281
|
1 |
|
return $map; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* {@inheritdoc} |
286
|
|
|
*/ |
287
|
1 |
|
public function join(string $separator): Str |
288
|
|
|
{ |
289
|
1 |
|
return $this->values()->join($separator); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* {@inheritdoc} |
294
|
|
|
*/ |
295
|
1 |
|
public function remove($key): Implementation |
296
|
|
|
{ |
297
|
1 |
|
if (!$this->contains($key)) { |
298
|
1 |
|
return $this; |
299
|
|
|
} |
300
|
|
|
|
301
|
1 |
|
$map = clone $this; |
302
|
1 |
|
$map->values = clone $this->values; |
303
|
1 |
|
$map->values->detach($key); |
304
|
1 |
|
$map->values->rewind(); |
305
|
|
|
|
306
|
1 |
|
return $map; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* {@inheritdoc} |
311
|
|
|
*/ |
312
|
1 |
|
public function merge(Implementation $map): Implementation |
313
|
|
|
{ |
314
|
1 |
|
return $map->reduce( |
315
|
1 |
|
$this, |
316
|
|
|
function(self $carry, $key, $value): self { |
317
|
1 |
|
return $carry->put($key, $value); |
318
|
1 |
|
} |
319
|
|
|
); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* {@inheritdoc} |
324
|
|
|
*/ |
325
|
1 |
|
public function partition(callable $predicate): Map |
326
|
|
|
{ |
327
|
1 |
|
$truthy = $this->clearMap(); |
328
|
1 |
|
$falsy = $this->clearMap(); |
329
|
|
|
|
330
|
1 |
|
foreach ($this->values as $k) { |
331
|
1 |
|
$v = $this->values[$k]; |
332
|
|
|
|
333
|
1 |
|
$return = $predicate($k, $v); |
334
|
|
|
|
335
|
1 |
|
if ($return === true) { |
336
|
1 |
|
$truthy = $truthy->put($k, $v); |
337
|
|
|
} else { |
338
|
1 |
|
$falsy = $falsy->put($k, $v); |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|
342
|
1 |
|
return Map::of('bool', Map::class) |
343
|
1 |
|
(true, $truthy) |
344
|
1 |
|
(false, $falsy); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* {@inheritdoc} |
349
|
|
|
*/ |
350
|
7 |
|
public function reduce($carry, callable $reducer) |
351
|
|
|
{ |
352
|
7 |
|
foreach ($this->values as $k) { |
353
|
7 |
|
$v = $this->values[$k]; |
354
|
|
|
|
355
|
7 |
|
$carry = $reducer($carry, $k, $v); |
356
|
|
|
} |
357
|
|
|
|
358
|
7 |
|
return $carry; |
359
|
|
|
} |
360
|
|
|
|
361
|
1 |
|
public function empty(): bool |
362
|
|
|
{ |
363
|
1 |
|
$this->values->rewind(); |
364
|
|
|
|
365
|
1 |
|
return !$this->values->valid(); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* @return Map<T, S> |
370
|
|
|
*/ |
371
|
2 |
|
private function clearMap(): Map |
372
|
|
|
{ |
373
|
2 |
|
return Map::of($this->keyType, $this->valueType); |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|