|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace DaveRandom\Jom; |
|
4
|
|
|
|
|
5
|
|
|
use DaveRandom\Jom\Exceptions\InvalidNodeValueException; |
|
6
|
|
|
|
|
7
|
|
|
abstract class NodeFactory |
|
8
|
|
|
{ |
|
9
|
|
|
private const SCALAR_VALUE_NODE_CLASSES = [ |
|
10
|
|
|
'boolean' => BooleanNode::class, |
|
11
|
|
|
'integer' => NumberNode::class, |
|
12
|
|
|
'double' => NumberNode::class, |
|
13
|
|
|
'string' => StringNode::class, |
|
14
|
|
|
]; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @throws InvalidNodeValueException |
|
18
|
|
|
*/ |
|
19
|
30 |
|
final protected function createArrayNodeFromPackedArray(array $values, ?Document $doc, int $flags): ArrayNode |
|
20
|
|
|
{ |
|
21
|
|
|
try { |
|
22
|
30 |
|
$node = new ArrayNode([], $doc); |
|
23
|
|
|
|
|
24
|
30 |
|
foreach ($values as $value) { |
|
25
|
25 |
|
if (null !== $valueNode = $this->createNodeFromValue($value, $doc, $flags)) { |
|
26
|
25 |
|
$node->push($valueNode); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
30 |
|
return $node; |
|
31
|
|
|
} catch (InvalidNodeValueException $e) { |
|
32
|
|
|
throw $e; |
|
33
|
|
|
//@codeCoverageIgnoreStart |
|
34
|
|
|
} catch (\Exception $e) { |
|
35
|
|
|
throw new \Error('Unexpected ' . \get_class($e) . ": {$e->getMessage()}", 0, $e); |
|
36
|
|
|
} |
|
37
|
|
|
//@codeCoverageIgnoreEnd |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @throws InvalidNodeValueException |
|
42
|
|
|
*/ |
|
43
|
|
|
final protected function createObjectNodeFromPropertyMap($properties, ?Document $doc, int $flags): ObjectNode |
|
44
|
|
|
{ |
|
45
|
|
|
try { |
|
46
|
|
|
$node = new ObjectNode([], $doc); |
|
47
|
|
|
|
|
48
|
|
|
foreach ($properties as $name => $value) { |
|
49
|
|
|
if (null !== $valueNode = $this->createNodeFromValue($value, $doc, $flags)) { |
|
50
|
|
|
$node->setProperty($name, $valueNode); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $node; |
|
55
|
|
|
} catch (InvalidNodeValueException $e) { |
|
56
|
|
|
throw $e; |
|
57
|
|
|
//@codeCoverageIgnoreStart |
|
58
|
|
|
} catch (\Exception $e) { |
|
59
|
|
|
throw new \Error('Unexpected ' . \get_class($e) . ": {$e->getMessage()}", 0, $e); |
|
60
|
|
|
} |
|
61
|
|
|
//@codeCoverageIgnoreEnd |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
37 |
|
final protected function createScalarOrNullNodeFromValue($value, ?Document $doc): ?Node |
|
65
|
|
|
{ |
|
66
|
37 |
|
if ($value === null) { |
|
67
|
35 |
|
return new NullNode($doc); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
30 |
|
$className = self::SCALAR_VALUE_NODE_CLASSES[\gettype($value)] ?? null; |
|
71
|
|
|
|
|
72
|
30 |
|
if ($className !== null) { |
|
73
|
|
|
return new $className($value, $doc); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
30 |
|
return null; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @throws InvalidNodeValueException |
|
81
|
|
|
*/ |
|
82
|
|
|
abstract public function createNodeFromValue($value, ?Document $doc, int $flags): ?Node; |
|
83
|
|
|
} |
|
84
|
|
|
|