-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataContainer.php
412 lines (349 loc) · 9.28 KB
/
DataContainer.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
/**
* Qubus\Support
*
* @link https://github.com/QubusPHP/support
* @copyright 2022
* @author Joshua Parker <[email protected]>
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types=1);
namespace Qubus\Support;
use ArrayAccess;
use ArrayIterator;
use Closure;
use Countable;
use IteratorAggregate;
use OutOfBoundsException;
use Qubus\Exception\Data\TypeException;
use ReturnTypeWillChange;
use RuntimeException;
use function array_map;
use function array_unshift;
use function call_user_func_array;
use function count;
use function func_get_args;
use function uniqid;
class DataContainer implements ArrayAccess, IteratorAggregate, Countable
{
/** @var DataContainer parent container, for inheritance */
protected DataContainer $parent;
/** @var bool whether we want to use parent cascading */
protected bool $parentEnabled = false;
/** @var array container data */
protected array $data = [];
/** @var bool whether the container is read-only */
protected bool $readOnly = false;
/** @var bool whether the container data has been modified */
protected bool $isModified = false;
/**
* Constructor
*
* @param DataObjectCollection $dataType String or Array data type.
* @param array $data Container data.
* @param bool $readOnly Whether the container is read-only.
*/
public function __construct(
public readonly DataObjectCollection $dataType,
array $data = [],
bool $readOnly = false
) {
$this->data = $data;
$this->readOnly = $readOnly;
}
/**
* Get the parent of this container.
*/
public function getParent(): DataContainer
{
return $this->parent;
}
/**
* Set the parent of this container, to support inheritance.
*
* @param DataContainer|null $parent the parent container object
* @return $this
*/
public function setParent(?DataContainer $parent = null): static
{
$this->parent = $parent;
if ($this->parent) {
$this->enableParent();
} else {
$this->disableParent();
}
return $this;
}
/**
* Enable the use of the parent object, if set.
*
* @return $this
*/
public function enableParent(): static
{
if ($this->parent) {
$this->parentEnabled = true;
}
return $this;
}
/**
* Disable the use of the parent object.
*
* @return $this
*/
public function disableParent(): static
{
$this->parentEnabled = false;
return $this;
}
/**
* Check whether this container has an active parent.
*/
public function hasParent(): bool
{
return $this->parentEnabled;
}
/**
* Retrieve the modified state of the container.
*/
public function isModified(): bool
{
return $this->isModified;
}
/**
* Replace the container's data.
*
* @param array $data new data
* @return $this
* @throws RuntimeException
*/
public function setContents(array $data): static
{
if ($this->readOnly) {
throw new RuntimeException('Changing values on this Data Container is not allowed.');
}
$this->data = $data;
$this->isModified = true;
return $this;
}
/**
* Get the container's data.
*
* @return array container's data
* @throws TypeException
*/
public function getContents(): array
{
if ($this->parentEnabled) {
return $this->dataType->array->merge($this->parent->getContents(), $this->data);
} else {
return $this->data;
}
}
/**
* Set whether the container is read-only.
*
* @param bool $readOnly whether it's a read-only container
* @return $this
*/
public function setReadOnly(bool $readOnly = true): static
{
$this->readOnly = (bool) $readOnly;
return $this;
}
/**
* Merge arrays into the container.
*
* @param array $arg array to merge with
* @return $this
* @throws RuntimeException|TypeException
*/
public function merge(array $arg): static
{
if ($this->readOnly) {
throw new RuntimeException('Changing values on this Data Container is not allowed.');
}
$arguments = array_map(function ($array) {
if ($array instanceof DataContainer) {
return $array->getContents();
}
return $array;
}, func_get_args());
array_unshift($arguments, $this->data);
$this->data = call_user_func_array([$this->dataType->array, 'merge'], $arguments);
$this->isModified = true;
return $this;
}
/**
* Check whether the container is read-only.
*
* @return bool $readOnly whether it's a read-only container
*/
public function isReadOnly(): bool
{
return $this->readOnly;
}
/**
* isset magic method
* @throws TypeException
*/
public function __isset($key)
{
return $this->has($key);
}
/**
* Check if a key was set upon this bag's data
* @throws TypeException
*/
public function has(string $key): bool
{
$result = $this->dataType->array->keyExists($this->data, $key);
if (! $result && $this->parentEnabled) {
$result = $this->parent->has($key);
}
return $result;
}
/**
* get magic method
* @throws TypeException
*/
public function __get($key)
{
return $this->get($key);
}
/**
* Get a key's value from the container.
* @throws TypeException
*/
public function get(?string $key = null, mixed $default = null): mixed
{
$fail = uniqid('__FAIL__', true);
$result = $this->dataType->array->get($this->data, $key, $fail);
if ($result === $fail) {
if ($this->parentEnabled) {
$result = $this->parent->get($key, $default);
} else {
$result = $this->result($default);
}
}
return $result;
}
/**
* set magic method
*/
public function __set($key, $value)
{
$this->set($key, $value);
}
/**
* Set a config value.
*
* @throws RuntimeException
*/
public function set(?string $key, mixed $value): static
{
if ($this->readOnly) {
throw new RuntimeException('Changing values on this Data Container is not allowed.');
}
$this->isModified = true;
if ($key === null) {
$this->data[] = $value;
return $this;
}
$this->dataType->array->set($this->data, $key, $value);
return $this;
}
/**
* Delete data from the container.
*
* @param string $key key to delete.
* @return bool delete success bool
*/
public function delete(string $key): bool
{
if ($this->readOnly) {
throw new RuntimeException('Changing values on this Data Container is not allowed.');
}
$this->isModified = true;
if (($result = $this->dataType->array->delete($this->data, $key)) === false && $this->parentEnabled) {
$result = $this->parent->delete($key);
}
return $result;
}
/**
* Allow usage of isset() on the param bag as an array.
*
* @param string $offset
* @throws TypeException
*/
public function offsetExists($offset): bool
{
return $this->has($offset);
}
/**
* Allow fetching values as an array.
*
* @param string $offset
* @throws OutOfBoundsException
* @throws TypeException
*/
#[ReturnTypeWillChange]
public function offsetGet($offset): mixed
{
return $this->get($offset, function () use ($offset) {
throw new OutOfBoundsException('Access to undefined index: ' . $offset);
});
}
/**
* Disallow setting values like an array.
*
* @param string $offset
*/
public function offsetSet($offset, mixed $value): void
{
$this->set($offset, $value);
}
/**
* Disallow unsetting values like an array.
*
* @param string $offset
* @throws RuntimeException
*/
public function offsetUnset($offset): void
{
$this->delete($offset);
}
/**
* IteratorAggregate implementation.
*
* @return ArrayIterator iterator
* @throws TypeException
*/
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->getContents());
}
/**
* Countable implementation.
*
* @return int number of items stored in the container
* @throws TypeException
*/
public function count(): int
{
return count($this->getContents());
}
/**
* Checks if a return value is a Closure without params, and if
* so executes it before returning it.
*
* @return mixed closure result
*/
public function result(mixed $val): mixed
{
if ($val instanceof Closure) {
return $val();
}
return $val;
}
}