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
|
|
|
namespace Nuglif\Nacl; |
15
|
|
|
|
16
|
|
|
class ObjectNode extends Node implements \IteratorAggregate, \ArrayAccess, \Countable |
17
|
|
|
{ |
18
|
|
|
private $value = []; |
19
|
|
|
private $isNative = true; |
20
|
|
|
|
21
|
510 |
|
public function __construct(array $values = []) |
22
|
|
|
{ |
23
|
510 |
|
foreach ($values as $k => $v) { |
24
|
385 |
|
$this[$k] = $v; |
25
|
510 |
|
} |
26
|
510 |
|
} |
27
|
|
|
|
28
|
13 |
|
public function count() |
29
|
|
|
{ |
30
|
13 |
|
return count($this->value); |
31
|
|
|
} |
32
|
|
|
|
33
|
13 |
|
public function merge(ObjectNode $a2) |
34
|
|
|
{ |
35
|
13 |
|
if (0 === count($this)) { |
36
|
9 |
|
return $a2; |
37
|
11 |
|
} elseif (0 === count($a2)) { |
38
|
1 |
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
10 |
|
foreach ($a2 as $key => $value) { |
42
|
10 |
|
if (!isset($this[$key]) || !($this[$key] instanceof ObjectNode) || !($value instanceof ObjectNode)) { |
43
|
10 |
|
$this[$key] = $value; |
44
|
10 |
|
} else { |
45
|
3 |
|
$this[$key] = $this[$key]->merge($value); |
46
|
|
|
} |
47
|
10 |
|
} |
48
|
|
|
|
49
|
10 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
10 |
|
public function getIterator() |
53
|
|
|
{ |
54
|
10 |
|
return new \ArrayIterator($this->value); |
55
|
|
|
} |
56
|
|
|
|
57
|
506 |
|
public function offsetSet($offset, $value) |
58
|
|
|
{ |
59
|
506 |
|
if ($value instanceof Node) { |
60
|
358 |
|
$value->setParent($this); |
61
|
358 |
|
$this->isNative = false; |
62
|
358 |
|
} |
63
|
506 |
|
$this->value[$offset] = $value; |
64
|
506 |
|
} |
65
|
|
|
|
66
|
14 |
|
public function offsetGet($offset) |
67
|
|
|
{ |
68
|
14 |
|
return $this->value[$offset]; |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
public function has($offset) |
72
|
|
|
{ |
73
|
3 |
|
return array_key_exists($offset, $this->value); |
74
|
|
|
} |
75
|
|
|
|
76
|
303 |
|
public function offsetExists($offset) |
77
|
|
|
{ |
78
|
303 |
|
return isset($this->value[$offset]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function offsetUnset($offset) |
82
|
|
|
{ |
83
|
|
|
unset($this->value[$offset]); |
84
|
|
|
} |
85
|
|
|
|
86
|
509 |
|
public function getNativeValue() |
87
|
|
|
{ |
88
|
509 |
|
if (!$this->isNative) { |
89
|
357 |
|
$this->resolve(); |
90
|
354 |
|
} |
91
|
|
|
|
92
|
507 |
|
return $this->value; |
93
|
|
|
} |
94
|
|
|
|
95
|
357 |
|
private function resolve() |
96
|
|
|
{ |
97
|
357 |
|
foreach ($this->value as $k => $v) { |
98
|
357 |
|
$this->value[$k] = $v instanceof Node ? $v->getNativeValue() : $v; |
99
|
354 |
|
} |
100
|
354 |
|
$this->isNative = true; |
101
|
354 |
|
} |
102
|
|
|
} |
103
|
|
|
|