1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Smoren\ArrayView\Views; |
6
|
|
|
|
7
|
|
|
use Smoren\ArrayView\Exceptions\IndexError; |
8
|
|
|
use Smoren\ArrayView\Exceptions\KeyError; |
9
|
|
|
use Smoren\ArrayView\Exceptions\LengthError; |
10
|
|
|
use Smoren\ArrayView\Exceptions\NotSupportedError; |
11
|
|
|
use Smoren\ArrayView\Exceptions\ReadonlyError; |
12
|
|
|
use Smoren\ArrayView\Exceptions\ValueError; |
13
|
|
|
use Smoren\ArrayView\Interfaces\ArraySelectorInterface; |
14
|
|
|
use Smoren\ArrayView\Interfaces\ArrayViewInterface; |
15
|
|
|
use Smoren\ArrayView\Selectors\MaskSelector; |
16
|
|
|
use Smoren\ArrayView\Selectors\SliceSelector; |
17
|
|
|
use Smoren\ArrayView\Structs\Slice; |
18
|
|
|
use Smoren\ArrayView\Util; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @template T |
22
|
|
|
*/ |
23
|
|
|
class ArrayView implements ArrayViewInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var array<T>|ArrayView<T> |
27
|
|
|
*/ |
28
|
|
|
protected $source; |
29
|
|
|
/** |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
protected bool $readonly; |
33
|
|
|
/** |
34
|
|
|
* @var ArrayView<T>|null |
35
|
|
|
*/ |
36
|
|
|
protected ?ArrayView $parentView; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritDoc} |
40
|
|
|
*/ |
41
|
|
|
public static function toView(&$source, ?bool $readonly = null): ArrayView |
42
|
|
|
{ |
43
|
|
|
if (!($source instanceof ArrayViewInterface)) { |
44
|
|
|
return new ArrayView($source, $readonly); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (!$source->isReadonly() && $readonly) { |
48
|
|
|
return new ArrayView($source, $readonly); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $source; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array<T>|ArrayViewInterface<T> $source |
56
|
|
|
* @param bool|null $readonly |
57
|
|
|
* @throws ReadonlyError |
58
|
|
|
*/ |
59
|
|
|
public function __construct(&$source, ?bool $readonly = null) |
60
|
|
|
{ |
61
|
|
|
if (is_array($source) && !Util::isArraySequential($source)) { |
62
|
|
|
throw new ValueError('Cannot create view for non-sequential array.'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->source = &$source; |
66
|
|
|
$this->readonly = $readonly ?? (($source instanceof ArrayViewInterface) ? $source->isReadonly() : false); |
|
|
|
|
67
|
|
|
$this->parentView = ($source instanceof ArrayViewInterface) ? $source : null; |
|
|
|
|
68
|
|
|
|
69
|
|
|
if (($source instanceof ArrayViewInterface) && $source->isReadonly() && !$this->isReadonly()) { |
70
|
|
|
throw new ReadonlyError("Cannot create non-readonly view for readonly source."); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritDoc} |
76
|
|
|
*/ |
77
|
|
|
public function toArray(): array |
78
|
|
|
{ |
79
|
|
|
return [...$this]; |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritDoc} |
84
|
|
|
*/ |
85
|
|
|
public function filter(callable $predicate): ArrayViewInterface |
86
|
|
|
{ |
87
|
|
|
return $this->is($predicate)->select($this); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritDoc} |
92
|
|
|
*/ |
93
|
|
|
public function is(callable $predicate): ArraySelectorInterface |
94
|
|
|
{ |
95
|
|
|
return new MaskSelector(array_map($predicate, $this->toArray())); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritDoc} |
100
|
|
|
*/ |
101
|
|
|
public function subview($selector, bool $readonly = null): ArrayViewInterface |
102
|
|
|
{ |
103
|
|
|
return is_string($selector) |
104
|
|
|
? (new SliceSelector($selector))->select($this, $readonly) |
105
|
|
|
: $selector->select($this, $readonly); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritDoc} |
110
|
|
|
*/ |
111
|
|
|
public function apply(callable $mapper): self |
112
|
|
|
{ |
113
|
|
|
for ($i = 0; $i < \count($this); $i++) { |
|
|
|
|
114
|
|
|
$this[$i] = $mapper($this[$i], $i); |
115
|
|
|
} |
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritDoc} |
121
|
|
|
*/ |
122
|
|
|
public function applyWith($data, callable $mapper): ArrayViewInterface |
123
|
|
|
{ |
124
|
|
|
[$dataSize, $thisSize] = [\count($data), \count($this)]; |
125
|
|
|
if ($dataSize !== $thisSize) { |
126
|
|
|
throw new LengthError("Length of values array not equal to view length ({$dataSize} != {$thisSize})."); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$dataView = ArrayView::toView($data); |
130
|
|
|
|
131
|
|
|
for ($i = 0; $i < \count($this); $i++) { |
|
|
|
|
132
|
|
|
$this[$i] = $mapper($this[$i], $dataView[$i], $i); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritDoc} |
140
|
|
|
*/ |
141
|
|
|
public function set($newValues): ArrayViewInterface |
142
|
|
|
{ |
143
|
|
|
[$dataSize, $thisSize] = [\count($newValues), \count($this)]; |
144
|
|
|
if ($dataSize !== $thisSize) { |
145
|
|
|
throw new LengthError("Length of values array not equal to view length ({$dataSize} != {$thisSize})."); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$newValuesView = ArrayView::toView($newValues); |
149
|
|
|
|
150
|
|
|
for ($i = 0; $i < \count($this); $i++) { |
|
|
|
|
151
|
|
|
$this[$i] = $newValuesView[$i]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return \Generator<T> |
159
|
|
|
*/ |
160
|
|
|
public function getIterator(): \Generator |
161
|
|
|
{ |
162
|
|
|
for ($i = 0; $i < \count($this); $i++) { |
|
|
|
|
163
|
|
|
yield $this[$i]; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return bool |
169
|
|
|
*/ |
170
|
|
|
public function isReadonly(): bool |
171
|
|
|
{ |
172
|
|
|
return $this->readonly; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param numeric|string|ArraySelectorInterface $offset |
|
|
|
|
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
|
|
public function offsetExists($offset): bool |
180
|
|
|
{ |
181
|
|
|
if (\is_numeric($offset)) { |
182
|
|
|
return $this->numericOffsetExists($offset); |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if (\is_string($offset) && Slice::isSlice($offset)) { |
186
|
|
|
return true; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if ($offset instanceof ArraySelectorInterface) { |
190
|
|
|
return true; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return false; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param numeric|string|ArraySelectorInterface $offset |
198
|
|
|
* @return T|array<T> |
199
|
|
|
*/ |
200
|
|
|
public function offsetGet($offset) |
201
|
|
|
{ |
202
|
|
|
if (\is_numeric($offset)) { |
203
|
|
|
if (!$this->numericOffsetExists($offset)) { |
|
|
|
|
204
|
|
|
throw new IndexError("Index {$offset} is out of range."); |
205
|
|
|
} |
206
|
|
|
return $this->source[$this->convertIndex($offset)]; |
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
if (\is_string($offset) && Slice::isSlice($offset)) { |
210
|
|
|
return $this->subview(new SliceSelector($offset))->toArray(); |
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
if ($offset instanceof ArraySelectorInterface) { |
214
|
|
|
return $this->subview($offset)->toArray(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
throw new KeyError("Invalid key: \"{$offset}\"."); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param numeric|string|ArraySelectorInterface $offset |
222
|
|
|
* @param T|array<T>|ArrayView<T> $value |
|
|
|
|
223
|
|
|
* @return void |
224
|
|
|
*/ |
225
|
|
|
public function offsetSet($offset, $value): void |
226
|
|
|
{ |
227
|
|
|
if ($this->isReadonly()) { |
228
|
|
|
throw new ReadonlyError("Cannot modify a readonly view."); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
if (\is_numeric($offset) && $this->numericOffsetExists($offset)) { |
|
|
|
|
232
|
|
|
$this->source[$this->convertIndex($offset)] = $value; |
|
|
|
|
233
|
|
|
return; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if (\is_string($offset) && Slice::isSlice($offset)) { |
237
|
|
|
$this->subview(new SliceSelector($offset))->set($value); |
238
|
|
|
return; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
if ($offset instanceof ArraySelectorInterface) { |
242
|
|
|
$this->subview($offset)->set($value); |
243
|
|
|
return; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
throw new KeyError("Invalid key: \"{$offset}\"."); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param numeric|string|ArraySelectorInterface $offset |
251
|
|
|
* @return void |
252
|
|
|
* @throws NotSupportedError |
253
|
|
|
*/ |
254
|
|
|
public function offsetUnset($offset): void |
255
|
|
|
{ |
256
|
|
|
throw new NotSupportedError(); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @return int |
261
|
|
|
*/ |
262
|
|
|
public function count(): int |
263
|
|
|
{ |
264
|
|
|
return $this->getParentSize(); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
protected function getParentSize(): int |
268
|
|
|
{ |
269
|
|
|
return ($this->parentView !== null) |
270
|
|
|
? \count($this->parentView) |
271
|
|
|
: \count($this->source); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @param int $i |
276
|
|
|
* @return int |
277
|
|
|
*/ |
278
|
|
|
protected function convertIndex(int $i): int |
279
|
|
|
{ |
280
|
|
|
return Util::normalizeIndex($i, \count($this->source)); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @param numeric $offset |
285
|
|
|
* @return bool |
286
|
|
|
*/ |
287
|
|
|
private function numericOffsetExists($offset): bool |
288
|
|
|
{ |
289
|
|
|
try { |
290
|
|
|
$index = $this->convertIndex($offset); |
|
|
|
|
291
|
|
|
} catch (IndexError $e) { |
292
|
|
|
return false; |
293
|
|
|
} |
294
|
|
|
return \is_array($this->source) |
295
|
|
|
? \array_key_exists($index, $this->source) |
296
|
|
|
: $this->source->offsetExists($index); |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|