1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/data-structure project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\DataStructure; |
10
|
|
|
|
11
|
|
|
use Daikon\Interop\Assert; |
12
|
|
|
use Daikon\Interop\Assertion; |
13
|
|
|
use Ds\Map as DsMap; |
14
|
|
|
use Traversable; |
15
|
|
|
|
16
|
|
|
abstract class Map implements MapInterface |
17
|
|
|
{ |
18
|
|
|
protected DsMap $compositeMap; |
19
|
|
|
|
20
|
25 |
|
protected function init(iterable $values): void |
21
|
|
|
{ |
22
|
25 |
|
Assertion::false(isset($this->compositeMap), 'Cannot reinitialize map.'); |
23
|
|
|
|
24
|
25 |
|
foreach ($values as $key => $value) { |
25
|
24 |
|
$this->assertValidKey($key); |
26
|
23 |
|
$this->assertValidType($value); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** @var Traversable $values */ |
30
|
23 |
|
$this->compositeMap = new DsMap($values); |
31
|
23 |
|
} |
32
|
|
|
|
33
|
|
|
/** @return static */ |
34
|
1 |
|
public function empty(): self |
35
|
|
|
{ |
36
|
1 |
|
$this->assertInitialized(); |
37
|
1 |
|
$copy = clone $this; |
38
|
1 |
|
$copy->compositeMap->clear(); |
39
|
1 |
|
return $copy; |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public function keys(): array |
43
|
|
|
{ |
44
|
1 |
|
$this->assertInitialized(); |
45
|
1 |
|
return $this->compositeMap->keys()->toArray(); |
46
|
|
|
} |
47
|
|
|
|
48
|
7 |
|
public function has(string $key): bool |
49
|
|
|
{ |
50
|
7 |
|
$this->assertInitialized(); |
51
|
7 |
|
return $this->compositeMap->hasKey($key); |
52
|
|
|
} |
53
|
|
|
|
54
|
7 |
|
public function get(string $key, $default = null) |
55
|
|
|
{ |
56
|
7 |
|
$this->assertInitialized(); |
57
|
7 |
|
if (func_num_args() === 1) { |
58
|
4 |
|
Assertion::satisfy($key, [$this, 'has'], "Key '$key' not found and no default provided."); |
59
|
2 |
|
return $this->compositeMap->get($key); |
60
|
|
|
} else { |
61
|
3 |
|
if (!is_null($default)) { |
62
|
2 |
|
$this->assertValidType($default); |
63
|
|
|
} |
64
|
2 |
|
return $this->compositeMap->get($key, $default); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** @return static */ |
69
|
2 |
|
public function with(string $key, $value): self |
70
|
|
|
{ |
71
|
2 |
|
$this->assertInitialized(); |
72
|
2 |
|
$this->assertValidType($value); |
73
|
1 |
|
$copy = clone $this; |
74
|
1 |
|
$copy->compositeMap->put($key, $value); |
75
|
1 |
|
return $copy; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** @return static */ |
79
|
2 |
|
public function without(string $key): self |
80
|
|
|
{ |
81
|
2 |
|
$this->assertInitialized(); |
82
|
2 |
|
Assertion::satisfy($key, [$this, 'has'], "Key '$key' not found."); |
83
|
1 |
|
$copy = clone $this; |
84
|
1 |
|
$copy->compositeMap->remove($key); |
85
|
1 |
|
return $copy; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public function first() |
89
|
|
|
{ |
90
|
1 |
|
$this->assertInitialized(); |
91
|
|
|
/** @psalm-suppress MissingPropertyType */ |
92
|
1 |
|
return $this->compositeMap->first()->value; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
public function last() |
96
|
|
|
{ |
97
|
1 |
|
$this->assertInitialized(); |
98
|
|
|
/** @psalm-suppress MissingPropertyType */ |
99
|
1 |
|
return $this->compositeMap->last()->value; |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
public function isEmpty(): bool |
103
|
|
|
{ |
104
|
2 |
|
$this->assertInitialized(); |
105
|
2 |
|
return $this->compositeMap->isEmpty(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** @param static $comparator */ |
109
|
1 |
|
public function equals($comparator): bool |
110
|
|
|
{ |
111
|
1 |
|
$this->assertValidMap($comparator); |
112
|
1 |
|
return $this->unwrap() === $comparator->unwrap(); |
113
|
|
|
} |
114
|
|
|
|
115
|
3 |
|
public function count(): int |
116
|
|
|
{ |
117
|
3 |
|
$this->assertInitialized(); |
118
|
3 |
|
return $this->compositeMap->count(); |
119
|
|
|
} |
120
|
|
|
|
121
|
6 |
|
public function unwrap(): array |
122
|
|
|
{ |
123
|
6 |
|
$this->assertInitialized(); |
124
|
6 |
|
return $this->compositeMap->toArray(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** @psalm-suppress ImplementedReturnTypeMismatch */ |
128
|
2 |
|
public function getIterator(): DsMap |
129
|
|
|
{ |
130
|
2 |
|
return $this->compositeMap; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** @psalm-suppress RedundantPropertyInitializationCheck */ |
134
|
21 |
|
protected function assertInitialized(): void |
135
|
|
|
{ |
136
|
21 |
|
Assertion::true(isset($this->compositeMap), 'Map is not initialized.'); |
137
|
21 |
|
} |
138
|
|
|
|
139
|
|
|
/** @param mixed $key */ |
140
|
24 |
|
protected function assertValidKey($key): void |
141
|
|
|
{ |
142
|
24 |
|
Assert::that($key, 'Key must be a valid string.')->string()->notEmpty(); |
143
|
23 |
|
} |
144
|
|
|
|
145
|
|
|
/** @param mixed $value */ |
146
|
23 |
|
protected function assertValidType($value): void |
147
|
|
|
{ |
148
|
23 |
|
Assertion::true( |
149
|
23 |
|
is_array($value) || is_scalar($value), |
150
|
|
|
sprintf( |
151
|
23 |
|
"Invalid value type given to '%s', expected scalar or array but was given '%s'.", |
152
|
23 |
|
static::class, |
153
|
23 |
|
is_object($value) ? get_class($value) : @gettype($value) |
154
|
|
|
) |
155
|
|
|
); |
156
|
22 |
|
} |
157
|
|
|
|
158
|
|
|
/** @param mixed $map */ |
159
|
1 |
|
protected function assertValidMap($map): void |
160
|
|
|
{ |
161
|
1 |
|
Assertion::isInstanceOf( |
162
|
1 |
|
$map, |
163
|
1 |
|
static::class, |
164
|
1 |
|
sprintf("Map operation must be on same type as '%s'.", static::class) |
165
|
|
|
); |
166
|
1 |
|
} |
167
|
|
|
|
168
|
2 |
|
public function __get(string $key) |
169
|
|
|
{ |
170
|
2 |
|
return $this->get($key); |
171
|
|
|
} |
172
|
|
|
|
173
|
4 |
|
public function __clone() |
174
|
|
|
{ |
175
|
4 |
|
$this->compositeMap = clone $this->compositeMap; |
176
|
4 |
|
} |
177
|
|
|
} |
178
|
|
|
|