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, ?int $flags): Node |
30
|
|
|
{ |
31
|
37 |
|
if ($node instanceof $expectedType || ($flags & self::PERMIT_INCORRECT_REFERENCE_TYPE)) { |
32
|
37 |
|
return $node; |
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
|
|
|
try { |
53
|
37 |
|
$result = ($nodeFactory ?? $nodeFactory = new UnsafeNodeFactory) |
54
|
37 |
|
->createNodeFromValue($value, $ownerDocument, $flags ?? 0); |
55
|
|
|
} catch (InvalidNodeValueException $e) { |
56
|
|
|
throw $e; |
57
|
|
|
//@codeCoverageIgnoreStart |
58
|
|
|
} catch (\Exception $e) { |
59
|
|
|
/** @noinspection PhpInternalEntityUsedInspection */ |
60
|
|
|
throw unexpected($e); |
61
|
|
|
} |
62
|
|
|
//@codeCoverageIgnoreEnd |
63
|
|
|
|
64
|
37 |
|
return self::validateCreatedNodeType($result, static::class, $value, $flags); |
65
|
|
|
} |
66
|
|
|
|
67
|
165 |
|
protected function __construct(?Document $ownerDocument) |
68
|
|
|
{ |
69
|
165 |
|
$this->ownerDocument = $ownerDocument; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function __clone() |
73
|
|
|
{ |
74
|
|
|
$this->setReferences(null, null, null, null); |
75
|
|
|
} |
76
|
|
|
|
77
|
151 |
|
final protected function setReferences(?VectorNode $parent, $key, ?Node $previousSibling, ?Node $nextSibling): void |
78
|
|
|
{ |
79
|
151 |
|
$this->parent = $parent; |
80
|
151 |
|
$this->key = $key; |
81
|
151 |
|
$this->previousSibling = $previousSibling; |
82
|
151 |
|
$this->nextSibling = $nextSibling; |
83
|
|
|
} |
84
|
|
|
|
85
|
47 |
|
final public function getParent(): ?VectorNode |
86
|
|
|
{ |
87
|
47 |
|
return $this->parent; |
88
|
|
|
} |
89
|
|
|
|
90
|
7 |
|
final public function getPreviousSibling(): ?Node |
91
|
|
|
{ |
92
|
7 |
|
return $this->previousSibling; |
93
|
|
|
} |
94
|
|
|
|
95
|
7 |
|
final public function getNextSibling(): ?Node |
96
|
|
|
{ |
97
|
7 |
|
return $this->nextSibling; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function hasChildren(): bool |
101
|
|
|
{ |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function containsChild(/** @noinspection PhpUnusedParameterInspection */ Node $child): bool |
106
|
|
|
{ |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function getFirstChild(): ?Node |
111
|
|
|
{ |
112
|
|
|
return null; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getLastChild(): ?Node |
116
|
|
|
{ |
117
|
|
|
return null; |
118
|
|
|
} |
119
|
|
|
|
120
|
49 |
|
final public function getOwnerDocument(): ?Document |
121
|
|
|
{ |
122
|
49 |
|
return $this->ownerDocument; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return string|int|null |
127
|
|
|
*/ |
128
|
50 |
|
final public function getKey() |
129
|
|
|
{ |
130
|
50 |
|
return $this->key; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
abstract public function getValue(); |
134
|
|
|
abstract public function jsonSerialize(); |
135
|
|
|
} |
136
|
|
|
|