-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXmlSerializer.php
182 lines (158 loc) · 5.21 KB
/
XmlSerializer.php
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
<?php
/**
* Copyright (C) 2014, Some right reserved.
* @author Kacper "Kadet" Donat <[email protected]>
* @license http://creativecommons.org/licenses/by-sa/4.0/legalcode CC BY-SA
*
* Contact with author:
* Xmpp: [email protected]
* E-mail: [email protected]
*
* From Kadet with love.
*/
namespace Kadet\XmlSerializer;
require_once 'functions.php';
class XmlSerializer
{
/**
* @var \DOMDocument
*/
private $_document;
/**
* Default DAO for serialized objects.
*
* @var string
*/
public $dao = 'stdClass';
public function __construct()
{
$this->_document = new \DOMDocument();
$this->_document->formatOutput = true;
$this->_document->encoding = 'utf-8';
}
/**
* Serializes object.
*
* @param object $var
* @param \DOMElement $node
*
* @return \DOMElement
*/
private function _serializeObject($var, \DOMElement &$node)
{
if (is_subclass_of($var, 'Kadet\\XmlSerializer\\XmlSerializable'))
return $var->toXml($node, $this);
$reflection = new \ReflectionObject($var);
foreach ($reflection->getProperties() as $property) {
$property->setAccessible(true);
$annotations = getAnnotations($property->getDocComment());
if (isset($annotations['xml-skip'])) continue;
$value = $property->getValue($var);
if (isset($annotations['xml-attrib'])) {
$name = !empty($annotations['xml-attrib']) ? $annotations['xml-attrib'] : $property->getName();
$node->setAttribute($name, is_array($value) || is_object($value) ? serialize($value) : $value);
} else {
$name = !empty($annotations['xml-tag']) ? $annotations['xml-tag'] : $property->getName();
$element = $this->_document->createElement($name);
$node->appendChild($this->serializeElement($value, $element));
}
if (!$property->isPublic()) $property->setAccessible(false);
}
return $node;
}
/**
* Serializes object to specified element.
*
* @param mixed $var
* @param \DOMElement $node
*
* @return \DOMElement
*/
public function serializeElement($var, \DOMElement &$node)
{
switch (gettype($var)) {
case 'object':
$node = $this->_serializeObject($var, $node);
if (get_class($var) != $this->dao)
$node->setAttribute('s:type', htmlspecialchars(get_class($var)));
break;
case 'array':
$node->setAttribute('s:type', 'array');
$node = $this->_serializeArray($var, $node);
break;
default:
$node = $this->_serializeVar($var, $node);
break;
}
return $node;
}
/**
* Serializes object.
*
* @param mixed $var Object to serialize.
* @param string $tag XML Tag for root.
*
* @return string XML string with serialized object.
*/
public function serialize($var, $tag = null)
{
if (isset($this->_document->documentElement))
$this->_document->removeChild($this->_document->documentElement);
$element = $this->_document->createElement(empty($tag) ? $this->_getTag($var) : $tag);
$this->_document->appendChild($this->serializeElement($var, $element));
$this->_document->documentElement->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:s', 'urn:kadet:serializer');
if ($this->dao != 'stdClass')
$this->_document->documentElement->setAttribute('s:dao', $this->dao);
return $this->_document->saveXML();
}
/**
* Gets tag for variable.
*
* @param mixed $var Variable to determine tag.
*
* @return string Xml Tag.
*/
private function _getTag($var)
{
if (!is_object($var)) return gettype($var);
$reflection = new \ReflectionObject($var);
$annotations = getAnnotations($reflection->getDocComment());
if (isset($annotations['xml-tag']))
return $annotations['xml-tag'];
else
return basename(get_class($var));
}
/**
* Serializes array to xml element.
*
* @param array $array Array to serialize.
* @param \DOMElement $node Xml node.
*
* @return \DOMElement
*/
private function _serializeArray(array $array, \DOMElement &$node)
{
foreach ($array as $key => $value) {
$element = $node->ownerDocument->createElement('element');
$element->setAttribute('s:key', $key);
$node->appendChild($this->serializeElement($value, $element));
}
return $node;
}
/**
* Serializes scalar to xml node.
*
* @param mixed $var Scalar to serialize.
* @param \DOMElement $element Xml node.
*
* @return \DOMElement
*/
private function _serializeVar($var, \DOMElement &$element)
{
if(is_bool($var))
$element->nodeValue = $var ? 'true' : 'false';
else
$element->nodeValue = (string)$var;
return $element;
}
}