-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractList.php
325 lines (269 loc) · 7.1 KB
/
AbstractList.php
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
/**
* This file is part of the quant project.
*
* (c) 2023 Thorsten Suckow-Homberg <[email protected]>
*
* For full copyright and license information, please consult the LICENSE-file distributed
* with this source code.
*/
declare(strict_types=1);
namespace Quant\Core;
use ArrayAccess;
use Quant\Core\Contract\Arrayable;
use Countable;
use Iterator;
use OutOfBoundsException;
use Quant\Core\Contract\Comparable;
use Quant\Core\Contract\Equatable;
use TypeError;
/**
* List supporting Generic types.
* Each class deriving from AbstractList must provide information about the type maintained
* with instances of this list via `getType`.
* In addition to the interfaces implemented by this class, additional methods are provided
* that help with filtering or looking up entries: #findBy, #peek#, #map
*
* @template TValue
* @implements Iterator<int, TValue>
* @implements ArrayAccess<int, TValue>
*/
abstract class AbstractList implements Arrayable, ArrayAccess, Iterator, Countable, Equatable
{
/**
* @var array<int, TValue>
*/
protected array $data = [];
/**
* \Iterator Interface
* @var int
*/
protected int $position = 0;
/**
* Constructor.
* Final to allow new static();
*
* @see make
*/
final public function __construct()
{
}
/**
* Factory method for easily creating instances of the implementing class.
*
* @param mixed ...$items
*
* @return static
*/
public static function make(...$items): static
{
$self = new static();
foreach ($items as $item) {
$self[] = $item;
}
return $self;
}
/**
* Returns the class name of the entity-type this list should maintain
* entries of.
*
* @return string
*/
abstract public function getType(): string;
/**
* Applies the map function to this data and returns **this** list.
*
* @param callable $mapFn The callable to pass to the callback submitted to
* array_map()
*
* @return static
*/
public function map(callable $mapFn): static
{
array_map($mapFn, $this->data);
return $this;
}
/**
* Returns the entry in this list given the callback function.
*
* @param callable $findFn A callback. Return true in the function to indicate a match. First match will
* be returned. The callback is passed the current entry.
*
* @return ?TValue
*/
public function findBy(callable $findFn): mixed
{
foreach ($this->data as $resource) {
if ($findFn($resource) === true) {
return $resource;
}
}
return null;
}
/**
* Returns the element at the head of the AbstractList, or null if the list is empty.
*
* @return ?TValue
*/
public function peek(): mixed
{
$count = count($this->data);
return !$count ? null : $this->data[$count - 1];
}
public function equals(Equatable $target): bool
{
$thisClass = get_class($this);
if (!($target instanceof $thisClass)) {
return false;
}
/**
* @var AbstractList<TValue> $td
*/
$td = $target->toArray();
if (count($td) !== count($this)) {
return false;
}
$type = $this->getType();
$isEquatable = is_a($type, Equatable::class, true);
$isComparable = is_a($type, Comparable::class, true);
foreach ($td as $i => $entity) {
if ($isEquatable) {
if ($entity->equals($this[$i]) === false) {
return false;
}
} elseif ($isComparable) {
if ($entity->compareTo($this[$i]) !== 0) {
return false;
}
} else {
if (!$this->compareItems($this[$i], $entity)) {
return false;
}
}
}
return true;
}
/**
* Method called by the abstract list if containing items are neither Equatable nor Comparable.
* Override to implement comparator.
*
* @param mixed $a
* @param mixed $b
* @return bool
*/
protected function compareItems(mixed $a, mixed $b): bool
{
return $a === $b;
}
/**
* @param mixed $offset
* @param mixed $value
* @return void
*
* @throws OutOfBoundsException
*/
protected function doInsert(mixed $offset, mixed $value)
{
if (!is_null($offset) && !is_int($offset)) {
throw new OutOfBoundsException(
"expected integer key for \"offset\", " .
"but got type: " . (gettype($offset))
);
}
if (is_null($offset)) {
$this->data[] = $value;
} else {
$this->data[$offset] = $value;
}
}
/**
* @param mixed $value
* @return bool
*
* @throws TypeError
*/
protected function assertTypeFor(mixed $value): bool
{
$entityType = $this->getType();
// instanceof has higher precedence, so
// (!$value instanceof $entityType)
// would also be a valid expression
if (!($value instanceof $entityType)) {
/** @var object $value */
throw new TypeError(
"Expected type \"$entityType\" for value-argument, got " . gettype($value)
);
}
return true;
}
// -------------------------
// ArrayAccess Interface
// -------------------------
/**
* @throws TypeError|OutOfBoundsException if $value is not of the type defined
* with this getType, or f $offset is not an int
*/
public function offsetSet(mixed $offset, mixed $value): void
{
$this->assertTypeFor($value);
$this->doInsert($offset, $value);
}
public function offsetExists($offset): bool
{
return isset($this->data[$offset]);
}
public function offsetUnset($offset): void
{
unset($this->data[$offset]);
}
public function offsetGet($offset): mixed
{
return $this->data[$offset] ?? null;
}
// --------------------------
// Iterator Interface
// --------------------------
public function rewind(): void
{
$this->position = 0;
}
public function key(): int
{
return $this->position;
}
public function current(): mixed
{
return $this->data[$this->position];
}
public function next(): void
{
$this->position++;
}
/**
* @inheritdoc
*/
public function valid(): bool
{
return isset($this->data[$this->position]);
}
// --------------------------
// Iterator Interface
// --------------------------
/**
* @return int
*/
public function count(): int
{
return count($this->data);
}
// --------------------------
// Arrayable interface
// --------------------------
/**
* @return array<mixed, TValue>
*/
public function toArray(): array
{
return $this->data;
}
}