-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAbstractCollection.php
More file actions
235 lines (185 loc) · 5.21 KB
/
AbstractCollection.php
File metadata and controls
235 lines (185 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
declare(strict_types=1);
namespace Steevanb\PhpCollection;
use Steevanb\PhpCollection\{
Exception\KeyNotFoundException,
Exception\ReadOnlyException,
ScalarCollection\IntegerCollection,
ScalarCollection\StringCollection
};
/**
* @template TValueType
* @implements CollectionInterface<TValueType>
*/
abstract class AbstractCollection implements CollectionInterface
{
abstract protected function assertValueType(mixed $value): static;
/** @var array<TValueType> */
protected array $values = [];
protected bool $readOnly = false;
/** @param iterable<TValueType> $values */
public function __construct(iterable $values = [])
{
$this->replace($values);
}
public function hasKey(string|int $key): bool
{
return array_key_exists($key, $this->values);
}
public function remove(string|int $key): static
{
$this->assertIsNotReadOnly();
if ($this->hasKey($key) === false) {
throw new KeyNotFoundException('Key "' . $key . '" not found.');
}
unset($this->values[$key]);
return $this;
}
public function resetKeys(): static
{
$this->assertIsNotReadOnly();
$this->values = array_values($this->values);
return $this;
}
public function count(): int
{
return count($this->values);
}
public function isEmpty(): bool
{
return $this->count() === 0;
}
public function setReadOnly(): static
{
$this->readOnly = true;
return $this;
}
public function isReadOnly(): bool
{
return $this->readOnly;
}
/** @return array<int, string|int> */
public function getKeys(): array
{
return array_keys($this->values);
}
public function getStringKeys(): StringCollection
{
$return = new StringCollection();
foreach ($this->getKeys() as $key) {
if (is_string($key)) {
$return->add($key);
}
}
return $return;
}
public function getIntegerKeys(): IntegerCollection
{
$return = new IntegerCollection();
foreach ($this->getKeys() as $key) {
if (is_int($key)) {
$return->add($key);
}
}
return $return;
}
public function clear(): static
{
$this->assertIsNotReadOnly();
$this->values = [];
return $this;
}
public function changeKeyCase(KeyCaseEnum $case = KeyCaseEnum::LOWER): static
{
return $this
->assertIsNotReadOnly()
->replace(array_change_key_case($this->toArray(), $case->value));
}
/** @return array<string|int, TValueType> */
public function toArray(): array
{
return $this->values;
}
/** @param TValueType $value */
public function set(string|int $key, mixed $value): static
{
$this
->assertIsNotReadOnly()
->assertValueType($value);
$this->values[$key] = $value;
return $this;
}
/** @param iterable<TValueType> $values */
public function replace(iterable $values): static
{
$this->assertIsNotReadOnly();
$this->clear();
/** @var int|string $key */
foreach ($values as $key => $value) {
$this->set($key, $value);
}
reset($this->values);
return $this;
}
/** @return TValueType */
public function get(string|int $key): mixed
{
if ($this->hasKey($key) === false) {
throw new KeyNotFoundException('Key "' . $key . '" not found.');
}
return $this->values[$key];
}
/**
* @param TValueType $default
* @return TValueType
*/
public function getOrDefault(string|int $key, mixed $default): mixed
{
$this->assertValueType($default);
return $this->hasKey($key) ? $this->get($key) : $default;
}
/** @param TValueType $value */
public function contains(mixed $value): bool
{
return in_array($value, $this->values, true);
}
/** @param TValueType $value */
public function add(mixed $value): static
{
$this
->assertIsNotReadOnly()
->assertValueType($value);
$this->values[] = $value;
return $this;
}
/** @param CollectionInterface<TValueType> $collection */
public function merge(CollectionInterface $collection): static
{
return $this->replace(array_merge($this->values, $collection->toArray()));
}
/** @return TValueType */
public function getFirst(): mixed
{
$key = array_key_first($this->values);
if (is_null($key)) {
throw new KeyNotFoundException('First key not found.');
}
return $this->get($key);
}
/** @return TValueType */
public function getLast(): mixed
{
$key = array_key_last($this->values);
if (is_null($key)) {
throw new KeyNotFoundException('Last key not found.');
}
return $this->get($key);
}
protected function assertIsNotReadOnly(): static
{
if ($this->isReadOnly()) {
throw new ReadOnlyException();
}
return $this;
}
}