1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace DaveRandom\Jom; |
4
|
|
|
|
5
|
|
|
use DaveRandom\Jom\Exceptions\InvalidNodeValueException; |
6
|
|
|
use DaveRandom\Jom\Exceptions\InvalidSubjectNodeException; |
7
|
|
|
use DaveRandom\Jom\Exceptions\WriteOperationForbiddenException; |
8
|
|
|
|
9
|
|
|
final class UnsafeNodeFactory extends NodeFactory |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @throws WriteOperationForbiddenException |
13
|
|
|
* @throws InvalidNodeValueException |
14
|
|
|
* @throws InvalidSubjectNodeException |
15
|
|
|
*/ |
16
|
|
|
private function createObjectNodeFromPropertyArray(Document $doc, array $properties): ObjectNode |
17
|
|
|
{ |
18
|
|
|
$node = new ObjectNode($doc); |
19
|
|
|
|
20
|
|
|
foreach ($properties as $name => $value) { |
21
|
|
|
$node->setProperty($name, $this->createNodeFromValue($doc, $value)); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
return $node; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @throws WriteOperationForbiddenException |
29
|
|
|
* @throws InvalidNodeValueException |
30
|
|
|
* @throws InvalidSubjectNodeException |
31
|
|
|
*/ |
32
|
|
|
private function createVectorNodeFromArray(Document $doc, array $values): VectorNode |
33
|
|
|
{ |
34
|
|
|
$i = 0; |
35
|
|
|
$packed = true; |
36
|
|
|
|
37
|
|
|
foreach ($values as $key => $value) { |
38
|
|
|
if ($key !== $i++) { |
39
|
|
|
$packed = false; |
40
|
|
|
break; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $packed |
45
|
|
|
? $this->createArrayNodeFromPackedArray($doc, $values) |
46
|
|
|
: $this->createObjectNodeFromPropertyArray($doc, $values); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws WriteOperationForbiddenException |
51
|
|
|
* @throws InvalidNodeValueException |
52
|
|
|
* @throws InvalidSubjectNodeException |
53
|
|
|
*/ |
54
|
|
|
private function createNodeFromObject(Document $doc, object $object): Node |
55
|
|
|
{ |
56
|
|
|
if ($object instanceof \stdClass) { |
57
|
|
|
return $this->createObjectNodeFromStdClass($doc, $object); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($object instanceof \JsonSerializable) { |
61
|
|
|
return $this->createNodeFromValue($doc, $object->jsonSerialize()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->createObjectNodeFromPropertyArray($doc, \get_object_vars($object)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
*/ |
70
|
|
|
public function createNodeFromValue(Document $doc, $value): Node |
71
|
|
|
{ |
72
|
|
|
if (null !== $node = $this->createScalarOrNullNodeFromValue($doc, $value)) { |
73
|
|
|
return $node; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (\is_object($value)) { |
77
|
|
|
return $this->createNodeFromObject($doc, $value); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (\is_array($value)) { |
81
|
|
|
return $this->createVectorNodeFromArray($doc, $value); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
throw new InvalidNodeValueException("Failed to create node from value of type '" . \gettype($value) . "'"); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|