Repository navigation
Expand file tree
/
Copy pathNodeFactory.php
More file actions
147 lines (123 loc) · 4.13 KB
/
Copy pathNodeFactory.php
File metadata and controls
147 lines (123 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php declare(strict_types=1);
namespace DaveRandom\Jom;
use DaveRandom\Jom\Exceptions\Exception;
use DaveRandom\Jom\Exceptions\InvalidNodeValueException;
abstract class NodeFactory
{
private const ENABLE_INVALID_VALUE_IGNORE = 0x8000;
private const IGNORE_INVALID_VALUES = Node::IGNORE_INVALID_VALUES | self::ENABLE_INVALID_VALUE_IGNORE;
private const NODE_FACTORY_METHODS = [
'NULL' => 'createNullNode',
'boolean' => 'createNodeFromScalarValue',
'integer' => 'createNodeFromScalarValue',
'double' => 'createNodeFromScalarValue',
'string' => 'createNodeFromScalarValue',
'array' => 'createNodeFromArrayValue',
'object' => 'createNodeFromObjectValue',
];
/**
* @param mixed $value
* @throws InvalidNodeValueException
*/
private function throwInvalidValue($value): void
{
throw new InvalidNodeValueException(\sprintf(
"Failed to create node from value of type '%s'",
describe($value)
));
}
/**
* @uses createNullNode
*/
private function createNullNode(?Document $ownerDoc): NullNode
{
return new NullNode($ownerDoc);
}
/**
* @param mixed $value
* @uses createNodeFromScalarValue
*/
private function createNodeFromScalarValue(?Document $ownerDoc, $value): Node
{
$className = [
'boolean' => BooleanNode::class,
'integer' => NumberNode::class,
'double' => NumberNode::class,
'string' => StringNode::class,
][\gettype($value)];
return new $className($value, $ownerDoc);
}
/**
* @param mixed $value
* @throws InvalidNodeValueException
* @throws Exception
*/
final protected function tryCreateNodeFromValue($value, ?Document $ownerDoc, int $flags): ?Node
{
$factory = self::NODE_FACTORY_METHODS[\gettype($value)] ?? null;
if ($factory !== null) {
$node = $this->{$factory}($ownerDoc, $value, $flags);
}
if (isset($node)) {
return $node;
}
if (!($flags & Node::IGNORE_INVALID_VALUES)) {
$this->throwInvalidValue($value);
}
return null;
}
/**
* @param mixed[] $values
* @throws InvalidNodeValueException
* @throws Exception
*/
final protected function createArrayNodeFromPackedArray(array $values, ?Document $ownerDoc, int $flags): ArrayNode
{
$node = new ArrayNode([], $ownerDoc);
foreach ($values as $value) {
if (null !== $valueNode = $this->tryCreateNodeFromValue($value, $ownerDoc, $flags)) {
$node->push($valueNode);
}
}
return $node;
}
/**
* @param array|object $properties
* @throws InvalidNodeValueException
* @throws Exception
*/
final protected function createObjectNodeFromPropertyMap($properties, ?Document $ownerDoc, int $flags): ObjectNode
{
$node = new ObjectNode([], $ownerDoc);
foreach ($properties as $name => $value) {
if (null !== $valueNode = $this->tryCreateNodeFromValue($value, $ownerDoc, $flags)) {
$node->setProperty($name, $valueNode);
}
}
return $node;
}
/**
* @param mixed[] $array
* @throws InvalidNodeValueException
* @throws Exception
*/
abstract protected function createNodeFromArrayValue(?Document $ownerDoc, array $array, int $flags): VectorNode;
/**
* @throws InvalidNodeValueException
* @throws Exception
*/
abstract protected function createNodeFromObjectValue(?Document $ownerDoc, object $object, int $flags): ?Node;
/**
* @param bool|int|float|string|array|object|null A value that can be encoded as JSON
* @throws InvalidNodeValueException
* @throws Exception
*/
final public function createNodeFromValue($value, ?Document $ownerDoc, int $flags): Node
{
$node = $this->tryCreateNodeFromValue($value, $ownerDoc, $flags);
if ($node === null) {
$this->throwInvalidValue($value);
}
return $node;
}
}