|
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
|
|
|
* @return static |
|
29
|
|
|
*/ |
|
30
|
37 |
|
public static function createFromValue($value, ?Document $ownerDocument = null, ?int $flags = 0): Node |
|
31
|
|
|
{ |
|
32
|
37 |
|
static $nodeFactory; |
|
33
|
|
|
|
|
34
|
37 |
|
$flags = $flags ?? 0; |
|
35
|
|
|
|
|
36
|
|
|
try { |
|
37
|
37 |
|
$result = ($nodeFactory ?? $nodeFactory = new UnsafeNodeFactory) |
|
38
|
37 |
|
->createNodeFromValue($value, $ownerDocument, $flags); |
|
39
|
|
|
|
|
40
|
|
|
// Always throw if root node could not be created |
|
41
|
37 |
|
if ($result === null) { |
|
42
|
|
|
throw new InvalidNodeValueException(\sprintf( |
|
43
|
|
|
"Failed to create node from value of type '%s'", |
|
44
|
|
|
\gettype($value) |
|
45
|
|
|
)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// Check that the created node matches the class used to call the method |
|
49
|
37 |
|
if (!($result instanceof static) && !($flags & self::PERMIT_INCORRECT_REFERENCE_TYPE)) { |
|
|
|
|
|
|
50
|
|
|
throw new InvalidNodeValueException(\sprintf( |
|
51
|
|
|
"Value of type %s parsed as %s, %s expected", |
|
52
|
|
|
\gettype($value), |
|
53
|
|
|
\get_class($result), |
|
54
|
|
|
static::class |
|
55
|
|
|
)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
37 |
|
return $result; |
|
59
|
|
|
} catch (InvalidNodeValueException $e) { |
|
60
|
|
|
throw $e; |
|
61
|
|
|
//@codeCoverageIgnoreStart |
|
62
|
|
|
} catch (\Exception $e) { |
|
63
|
|
|
throw unexpected($e); |
|
64
|
|
|
} |
|
65
|
|
|
//@codeCoverageIgnoreEnd |
|
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
|
|
|
abstract public function getValue(); |
|
135
|
|
|
abstract public function jsonSerialize(); |
|
136
|
|
|
} |
|
137
|
|
|
|