1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace DaveRandom\Jom; |
4
|
|
|
|
5
|
|
|
use DaveRandom\Jom\Exceptions\InvalidNodeValueException; |
6
|
|
|
use DaveRandom\Jom\Exceptions\InvalidReferenceNodeException; |
7
|
|
|
|
8
|
|
|
abstract class Node implements \JsonSerializable |
9
|
|
|
{ |
10
|
|
|
public const IGNORE_INVALID_VALUES = 0b01; |
11
|
|
|
public const PERMIT_INCORRECT_REFERENCE_TYPE = 0b10; |
12
|
|
|
|
13
|
|
|
protected $ownerDocument; |
14
|
|
|
|
15
|
|
|
/** @var string|int|null */ |
16
|
|
|
protected $key; |
17
|
|
|
|
18
|
|
|
/** @var VectorNode|null */ |
19
|
|
|
protected $parent; |
20
|
|
|
|
21
|
|
|
/** @var Node|null */ |
22
|
|
|
protected $previousSibling; |
23
|
|
|
|
24
|
|
|
/** @var Node|null */ |
25
|
|
|
protected $nextSibling; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @throws InvalidNodeValueException |
29
|
|
|
*/ |
30
|
37 |
|
private static function validateCreatedNodeType(Node $node, string $expectedType, $value, ?int $flags): Node |
31
|
|
|
{ |
32
|
37 |
|
if ($node instanceof $expectedType || ($flags & self::PERMIT_INCORRECT_REFERENCE_TYPE)) { |
33
|
37 |
|
return $node; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** @noinspection PhpInternalEntityUsedInspection */ |
37
|
|
|
throw new InvalidNodeValueException(\sprintf( |
38
|
|
|
"Value of type %s parsed as instance of %s, instance of %s expected", |
39
|
|
|
describe($value), |
40
|
|
|
\get_class($node), |
41
|
|
|
$expectedType |
42
|
|
|
)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @throws InvalidNodeValueException |
47
|
|
|
* @return static |
48
|
|
|
*/ |
49
|
37 |
|
public static function createFromValue($value, ?Document $ownerDocument = null, ?int $flags = 0): Node |
50
|
|
|
{ |
51
|
37 |
|
static $nodeFactory; |
52
|
|
|
|
53
|
|
|
try { |
54
|
37 |
|
$result = ($nodeFactory ?? $nodeFactory = new UnsafeNodeFactory) |
55
|
37 |
|
->createNodeFromValue($value, $ownerDocument, $flags ?? 0); |
56
|
|
|
} catch (InvalidNodeValueException $e) { |
57
|
|
|
throw $e; |
58
|
|
|
//@codeCoverageIgnoreStart |
59
|
|
|
} catch (\Exception $e) { |
60
|
|
|
/** @noinspection PhpInternalEntityUsedInspection */ |
61
|
|
|
throw unexpected($e); |
62
|
|
|
} |
63
|
|
|
//@codeCoverageIgnoreEnd |
64
|
|
|
|
65
|
37 |
|
return self::validateCreatedNodeType($result, static::class, $value, $flags); |
66
|
|
|
} |
67
|
|
|
|
68
|
165 |
|
protected function __construct(?Document $ownerDocument) |
69
|
|
|
{ |
70
|
165 |
|
$this->ownerDocument = $ownerDocument; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function __clone() |
74
|
|
|
{ |
75
|
|
|
$this->setReferences(null, null, null, null); |
76
|
|
|
} |
77
|
|
|
|
78
|
151 |
|
final protected function setReferences(?VectorNode $parent, $key, ?Node $previousSibling, ?Node $nextSibling): void |
79
|
|
|
{ |
80
|
151 |
|
$this->parent = $parent; |
81
|
151 |
|
$this->key = $key; |
82
|
151 |
|
$this->previousSibling = $previousSibling; |
83
|
151 |
|
$this->nextSibling = $nextSibling; |
84
|
|
|
} |
85
|
|
|
|
86
|
47 |
|
final public function getParent(): ?VectorNode |
87
|
|
|
{ |
88
|
47 |
|
return $this->parent; |
89
|
|
|
} |
90
|
|
|
|
91
|
7 |
|
final public function getPreviousSibling(): ?Node |
92
|
|
|
{ |
93
|
7 |
|
return $this->previousSibling; |
94
|
|
|
} |
95
|
|
|
|
96
|
7 |
|
final public function getNextSibling(): ?Node |
97
|
|
|
{ |
98
|
7 |
|
return $this->nextSibling; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function hasChildren(): bool |
102
|
|
|
{ |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function containsChild(/** @noinspection PhpUnusedParameterInspection */ Node $child): bool |
107
|
|
|
{ |
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getFirstChild(): ?Node |
112
|
|
|
{ |
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function getLastChild(): ?Node |
117
|
|
|
{ |
118
|
|
|
return null; |
119
|
|
|
} |
120
|
|
|
|
121
|
49 |
|
final public function getOwnerDocument(): ?Document |
122
|
|
|
{ |
123
|
49 |
|
return $this->ownerDocument; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return string|int|null |
128
|
|
|
*/ |
129
|
50 |
|
final public function getKey() |
130
|
|
|
{ |
131
|
50 |
|
return $this->key; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return Node[] |
136
|
|
|
* @throws InvalidReferenceNodeException |
137
|
|
|
*/ |
138
|
11 |
|
final public function getAncestors(?Node $root = null): array |
139
|
|
|
{ |
140
|
11 |
|
$path = [$this]; |
141
|
11 |
|
$current = $this->parent; |
142
|
11 |
|
$rootParent = $root !== null |
143
|
11 |
|
? $root->parent |
144
|
11 |
|
: null; |
145
|
|
|
|
146
|
11 |
|
while ($current !== $rootParent && $current !== null) { |
147
|
9 |
|
$path[] = $current; |
148
|
9 |
|
$current = $current->parent; |
149
|
|
|
} |
150
|
|
|
|
151
|
11 |
|
if ($current !== $rootParent) { |
152
|
|
|
throw new InvalidReferenceNodeException('Path target node is not an ancestor of the subject node'); |
153
|
|
|
} |
154
|
|
|
|
155
|
11 |
|
return $path; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
abstract public function getValue(); |
159
|
|
|
abstract public function jsonSerialize(); |
160
|
|
|
} |
161
|
|
|
|