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