-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathXmlDeserializer.php
More file actions
231 lines (212 loc) · 8.17 KB
/
Copy pathXmlDeserializer.php
File metadata and controls
231 lines (212 loc) · 8.17 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
namespace enrol_arlo\Arlo\AuthAPI;
use enrol_arlo\Arlo\AuthAPI\Resource\AbstractCollection;
use enrol_arlo\Arlo\AuthAPI\Resource\AbstractResource;
use enrol_arlo\Arlo\AuthAPI\Exception\XMLDeserializerException;
class XmlDeserializer {
/**
* @var null|string $resourceClassPath custom path to resource classes.
*/
private $resourceClassPath;
/**
* @var bool
*/
private $ignoreMissingClasses = true;
private $loadOptions;
/**
* XmlDeserializer constructor.
*
* @param null $resourceClassPath
* @param bool $ignoreMissingClasses
* @param null $loadOptions
*/
public function __construct($resourceClassPath = null, $ignoreMissingClasses = true, $loadOptions = null) {
$this->resourceClassPath = null !== $resourceClassPath ? $resourceClassPath : '';
$this->ignoreMissingClasses = ($ignoreMissingClasses) ? true : false;
$this->loadOptions = null !== $loadOptions ? $loadOptions : LIBXML_NONET | LIBXML_NOBLANKS;
}
/**
* Main public method to accept Xml and deserialize it.
*
* @param $data
* @return mixed
* @throws \Exception
*/
public function deserialize($data) {
if ('' === trim($data)) {
throw new XMLDeserializerException('Invalid XML data, it can not be empty.');
}
$internalErrors = libxml_use_internal_errors(true);
libxml_clear_errors();
$dom = new \DOMDocument();
$dom->loadXML($data, $this->loadOptions | LIBXML_NOENT);
libxml_use_internal_errors($internalErrors);
if ($error = libxml_get_last_error()) {
libxml_clear_errors();
throw new \Exception($error->message);
}
$rootNode = $dom->firstChild;
if (!$rootNode->hasChildNodes()) {
throw new XMLDeserializerException('Root node has no children.');
}
$rootClassName = $this->resourceClassPath . $rootNode->nodeName;
if (!class_exists($rootClassName)) {
throw new XMLDeserializerException('Root class ' . $rootNode->nodeName . ' does not exist.');
}
$rootClassInstance = new $rootClassName();
$this->parseRootNode($rootNode, $rootClassInstance);
return $rootClassInstance;
}
/**
* Basic method to direct collection or resource parser.
*
* @param \DOMNode $node
* @param $classInstance
*/
private function parseRootNode(\DOMNode $node, $classInstance) {
// Root node is a collection of resources.
if ($classInstance instanceof AbstractCollection) {
$this->parseCollectionNode($node, $classInstance);
}
// Root node is a single resource.
if ($classInstance instanceof AbstractResource) {
$this->parseResourceNode($node, $classInstance);
}
}
/**
* Parse a link node.
*
* @param \DOMNode $node
* @param $classInstance
* @throws \Exception
*/
private function parseLinkNode(\DOMNode $node, $classInstance) {
// Make sure a Link.
if ($node->nodeName !== 'Link') {
throw new XMLDeserializerException('Not a Link');
}
// Deal with straight Link that has no expansions.
if (!$node->hasChildNodes()) {
$linkClassName = $this->resourceClassPath . $node->nodeName;
if (!class_exists($linkClassName)) {
throw new XMLDeserializerException('Class ' . $node->nodeName . ' does not exist.');
}
$link = new $linkClassName();
if ($node->hasAttributes()) {
foreach ($node->attributes as $attribute) {
$attributeName = $attribute->name;
$attributeValue = (string)$attribute->value;
$this->setValue($link, $attributeName, $attributeValue);
}
}
// Add Link attributes.
$this->setValue($classInstance, 'Link', $link);
}
// Deal with Link that has expansions.
if ($node->hasChildNodes()) {
foreach ($node->childNodes as $childNode) {
$childClassName = $this->resourceClassPath . $childNode->nodeName;
if (!class_exists($childClassName)) {
if (!$this->ignoreMissingClasses) {
throw new XMLDeserializerException('Class ' . $childNode->nodeName . ' does not exist.');
}
continue;
}
$childClassInstance = new $childClassName();
$this->parseResourceNode($childNode, $childClassInstance);
$this->setValue($classInstance, $childNode->nodeName, $childClassInstance);
}
}
}
/**
* Parse a collection node, direct to link parser.
*
* @param \DOMNode $node
* @param AbstractCollection $classInstance
*/
private function parseCollectionNode(\DOMNode $node, AbstractCollection $classInstance) {
foreach ($node->childNodes as $childNode) {
if ($childNode->nodeName === 'Link') {
$this->parseLinkNode($childNode, $classInstance);
}
}
}
/**
* Parse a resource node.
*
* @param \DOMNode $node
* @param AbstractResource $classInstance
* @throws \Exception
*/
private function parseResourceNode(\DOMNode $node, AbstractResource $classInstance) {
foreach ($node->childNodes as $childNode) {
if ($childNode->nodeName === 'Link') {
$this->parseLinkNode($childNode, $classInstance);
} else {
if ($childNode->hasChildNodes()) {
// Single element.
if (1 === $childNode->childNodes->length) {
$this->setValue($classInstance, $childNode->nodeName, $childNode->nodeValue);
continue;
}
// Has multiple elements, parse as resource.
if (1 < $childNode->childNodes->length) {
// Make class name based on class path and tag name.
$childClassName = $this->resourceClassPath . $childNode->nodeName;
if (!class_exists($childClassName)) {
if (!$this->ignoreMissingClasses) {
throw new XMLDeserializerException('Class ' . $childNode->nodeName . ' does not exist.');
}
continue;
}
// Initiate class and parse.
$childClassInstance = new $childClassName();
$this->parseResourceNode($childNode, $childClassInstance);
$this->setValue($classInstance, $childNode->nodeName, $childClassInstance);
}
}
}
}
}
/**
* Will try set value by public property, setter or adder.
*
* @param $class
* @param $property
* @param $value
*/
protected function setValue($class, $property, $value) {
try {
$reflectionClass = new \ReflectionClass($class);
} catch (\ReflectionException $e) {
return;
}
$properties = [];
foreach ($reflectionClass->getProperties() as $reflectionProperty) {
if ($reflectionProperty->isPublic()) {
$properties[$reflectionProperty->name] = $reflectionProperty->name;
}
}
if (in_array($property, $properties)) {
$class->{$property} = $value;
return;
}
$methods = [];
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
if ($reflectionMethod->isStatic()) {
continue;
}
$methods[$reflectionMethod->name] = $reflectionMethod->name;
}
$setter = 'set' . $property;
if (in_array($setter, $methods)) {
$class->{$setter}($value);
return;
}
$adder = 'add' . $property;
if (in_array($adder, $methods)) {
$class->{$adder}($value);
return;
}
}
}