|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Saxulum\JsonDocument; |
|
4
|
|
|
|
|
5
|
|
|
use Saxulum\JsonDocument\NodeToArray\ArrayNodeToArrayHandler; |
|
6
|
|
|
use Saxulum\JsonDocument\NodeToArray\AttributeNodeToArrayHandler; |
|
7
|
|
|
use Saxulum\JsonDocument\NodeToArray\NodeToArray; |
|
8
|
|
|
use Saxulum\JsonDocument\NodeToArray\ObjectNodeToArrayHandler; |
|
9
|
|
|
use Saxulum\JsonDocument\NodeToArray\ValueNodeToArrayHandler; |
|
10
|
|
|
|
|
11
|
|
|
trait DocumentTrait |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var \ReflectionClass |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $reflection; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var NodeToArray |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $nodeToArrayHandler; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param string $name |
|
25
|
|
|
* @return AttributeNode |
|
26
|
|
|
*/ |
|
27
|
|
|
public function createAttributeNode($name) |
|
28
|
|
|
{ |
|
29
|
|
|
$attribute = new AttributeNode(); |
|
30
|
|
|
|
|
31
|
|
|
Document::setProperty($attribute, 'name', $name); |
|
32
|
|
|
Document::setProperty($attribute, 'document', $this); |
|
33
|
|
|
|
|
34
|
|
|
return $attribute; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $name |
|
39
|
|
|
* @return ValueNode |
|
40
|
|
|
*/ |
|
41
|
|
|
public function createValueNode($name) |
|
42
|
|
|
{ |
|
43
|
|
|
$value = new ValueNode(); |
|
44
|
|
|
|
|
45
|
|
|
Document::setProperty($value, 'name', $name); |
|
46
|
|
|
Document::setProperty($value, 'document', $this); |
|
47
|
|
|
|
|
48
|
|
|
return $value; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $name |
|
53
|
|
|
* @return ObjectNode |
|
54
|
|
|
*/ |
|
55
|
|
|
public function createObjectNode($name) |
|
56
|
|
|
{ |
|
57
|
|
|
$object = new ObjectNode(); |
|
58
|
|
|
|
|
59
|
|
|
Document::setProperty($object, 'name', $name); |
|
60
|
|
|
Document::setProperty($object, 'document', $this); |
|
61
|
|
|
|
|
62
|
|
|
return $object; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $name |
|
67
|
|
|
* @return ArrayNode |
|
68
|
|
|
*/ |
|
69
|
|
|
public function createArrayNode($name) |
|
70
|
|
|
{ |
|
71
|
|
|
$array = new ArrayNode(); |
|
72
|
|
|
|
|
73
|
|
|
Document::setProperty($array, 'name', $name); |
|
74
|
|
|
Document::setProperty($array, 'document', $this); |
|
75
|
|
|
|
|
76
|
|
|
return $array; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param AbstractNode $node |
|
81
|
|
|
* @param int $options |
|
82
|
|
|
* @param \Closure $filter |
|
83
|
|
|
* @return string |
|
84
|
|
|
* @throws \Exception |
|
85
|
|
|
*/ |
|
86
|
|
|
public function save(AbstractNode $node = null, $options = 0, \Closure $filter = null) |
|
87
|
|
|
{ |
|
88
|
|
|
if (null === $node) { |
|
89
|
|
|
$node = $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$array = $this->getNodeToArrayHandler()->getArray($node); |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
if (null !== $filter) { |
|
95
|
|
|
$array = $this->walkRecursiveRemove($array, $filter); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return json_encode($array, $options); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return NodeToArray |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function getNodeToArrayHandler() |
|
105
|
|
|
{ |
|
106
|
|
|
if (null === $this->nodeToArrayHandler) { |
|
107
|
|
|
$this->nodeToArrayHandler = new NodeToArray(array( |
|
108
|
|
|
new ObjectNodeToArrayHandler(), |
|
109
|
|
|
new ArrayNodeToArrayHandler(), |
|
110
|
|
|
new AttributeNodeToArrayHandler(), |
|
111
|
|
|
new ValueNodeToArrayHandler() |
|
112
|
|
|
)); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $this->nodeToArrayHandler; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @source https://github.com/gajus/marray/blob/master/src/marray.php |
|
120
|
|
|
* @copyright Copyright (c) 2013-2014, Anuary (http://anuary.com/) |
|
121
|
|
|
* @param array $array |
|
122
|
|
|
* @param callable $callback |
|
123
|
|
|
* @return array |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function walkRecursiveRemove(array $array, callable $callback) |
|
126
|
|
|
{ |
|
127
|
|
|
foreach ($array as $k => $v) { |
|
128
|
|
|
if (is_array($v)) { |
|
129
|
|
|
$array[$k] = $this->walkRecursiveRemove($v, $callback); |
|
130
|
|
|
} else { |
|
131
|
|
|
if ($callback($v, $k)) { |
|
132
|
|
|
unset($array[$k]); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $array; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.