1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of NACL. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @copyright 2019 Nuglif (2018) Inc. |
9
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
10
|
|
|
* @author Pierrick Charron <[email protected]> |
11
|
|
|
* @author Charle Demers <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
declare(strict_types=1); |
15
|
|
|
|
16
|
|
|
namespace Nuglif\Nacl; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @template-implements \IteratorAggregate<mixed> |
20
|
|
|
* @template-implements \ArrayAccess<string, mixed> |
21
|
|
|
*/ |
22
|
|
|
class ObjectNode extends Node implements \IteratorAggregate, \ArrayAccess, \Countable |
23
|
|
|
{ |
24
|
|
|
private array $value = []; |
25
|
|
|
private bool $isNative = true; |
26
|
|
|
|
27
|
512 |
|
public function __construct(array $values = []) |
28
|
|
|
{ |
29
|
512 |
|
foreach ($values as $k => $v) { |
30
|
387 |
|
$this[$k] = $v; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
14 |
|
public function count(): int |
35
|
|
|
{ |
36
|
14 |
|
return count($this->value); |
37
|
|
|
} |
38
|
|
|
|
39
|
14 |
|
public function merge(ObjectNode $a2): ObjectNode |
40
|
|
|
{ |
41
|
14 |
|
if (0 === count($this)) { |
42
|
10 |
|
return $a2; |
43
|
12 |
|
} elseif (0 === count($a2)) { |
44
|
2 |
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
10 |
|
foreach ($a2 as $key => $value) { |
48
|
10 |
|
if (!isset($this[$key]) || !($this[$key] instanceof ObjectNode) || !($value instanceof ObjectNode)) { |
49
|
10 |
|
$this[$key] = $value; |
50
|
|
|
} else { |
51
|
3 |
|
$this[$key] = $this[$key]->merge($value); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
10 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
10 |
|
public function getIterator(): \Iterator |
59
|
|
|
{ |
60
|
10 |
|
return new \ArrayIterator($this->value); |
61
|
|
|
} |
62
|
|
|
|
63
|
508 |
|
public function offsetSet(mixed $offset, mixed $value): void |
64
|
|
|
{ |
65
|
508 |
|
if ($value instanceof Node) { |
66
|
360 |
|
$value->setParent($this); |
67
|
360 |
|
$this->isNative = false; |
68
|
|
|
} |
69
|
|
|
assert(is_string($offset)); |
70
|
508 |
|
$this->value[$offset] = $value; |
71
|
|
|
} |
72
|
|
|
|
73
|
16 |
|
public function offsetGet(mixed $offset): mixed |
74
|
|
|
{ |
75
|
16 |
|
return $this->value[$offset]; |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
public function has(mixed $offset): bool |
79
|
|
|
{ |
80
|
3 |
|
return array_key_exists($offset, $this->value); |
81
|
|
|
} |
82
|
|
|
|
83
|
305 |
|
public function offsetExists(mixed $offset): bool |
84
|
|
|
{ |
85
|
305 |
|
return isset($this->value[$offset]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function offsetUnset(mixed $offset): void |
89
|
|
|
{ |
90
|
|
|
unset($this->value[$offset]); |
91
|
|
|
} |
92
|
|
|
|
93
|
511 |
|
public function getNativeValue(): array |
94
|
|
|
{ |
95
|
511 |
|
if (!$this->isNative) { |
96
|
359 |
|
$this->resolve(); |
97
|
|
|
} |
98
|
|
|
|
99
|
509 |
|
return $this->value; |
100
|
|
|
} |
101
|
|
|
|
102
|
359 |
|
private function resolve(): void |
103
|
|
|
{ |
104
|
359 |
|
foreach ($this->value as $k => $v) { |
105
|
359 |
|
$this->value[$k] = $v instanceof Node ? $v->getNativeValue() : $v; |
106
|
|
|
} |
107
|
356 |
|
$this->isNative = true; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|