1
|
|
|
<?php |
2
|
|
|
namespace Thunder\Serializard\Format; |
3
|
|
|
|
4
|
|
|
use Thunder\Serializard\Exception\SerializationFailureException; |
5
|
|
|
use Thunder\Serializard\NormalizerContainer\NormalizerContainerInterface as Normalizers; |
6
|
|
|
use Thunder\Serializard\HydratorContainer\HydratorContainerInterface as Hydrators; |
7
|
|
|
use Thunder\Serializard\NormalizerContext\NormalizerContextInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
final class XmlFormat implements FormatInterface |
13
|
|
|
{ |
14
|
|
|
/** @var callable */ |
15
|
|
|
private $rootProvider; |
16
|
|
|
|
17
|
10 |
|
public function __construct(callable $rootProvider) |
18
|
|
|
{ |
19
|
10 |
|
$this->rootProvider = $rootProvider; |
20
|
10 |
|
} |
21
|
|
|
|
22
|
2 |
|
public function serialize($var, Normalizers $normalizers, NormalizerContextInterface $context) |
23
|
|
|
{ |
24
|
2 |
|
return $this->doSerialize($var, $normalizers, $context); |
25
|
|
|
} |
26
|
|
|
|
27
|
2 |
|
private function doSerialize($var, Normalizers $normalizers, NormalizerContextInterface $context, \DOMNode $doc = null, $parent = null, $key = null, array $state = [], array $classes = []) |
28
|
|
|
{ |
29
|
2 |
|
$isRoot = ($doc === null); |
30
|
2 |
|
$doc = $doc ?: new \DOMDocument('1.0', 'utf-8'); |
31
|
2 |
|
$doc->formatOutput = true; |
|
|
|
|
32
|
2 |
|
$parent = $parent ?: $doc; |
33
|
|
|
|
34
|
2 |
|
$this->serializeValue($var, $normalizers, $context, $doc, $parent, $key, $state, $classes); |
35
|
|
|
|
36
|
2 |
|
return $isRoot ? $doc->saveXML() : null; |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
private function serializeValue($var, Normalizers $normalizers, NormalizerContextInterface $context, \DOMNode $doc, $parent, $key, array $state = [], array $classes = []) |
40
|
|
|
{ |
41
|
|
|
/** @var \DOMDocument|\DOMElement $doc */ |
42
|
|
|
/** @var \DOMDocument|\DOMElement $parent */ |
43
|
2 |
|
if(\is_object($var)) { |
44
|
2 |
|
$class = \get_class($var); |
45
|
2 |
|
$handler = $normalizers->getHandler($class); |
46
|
2 |
|
$arr = $handler($var); |
47
|
|
|
|
48
|
2 |
|
$hash = spl_object_hash($var); |
49
|
2 |
|
$classes[] = $class; |
50
|
2 |
|
if(isset($state[$hash])) { |
51
|
1 |
|
throw SerializationFailureException::fromCycle($classes); |
52
|
|
|
} |
53
|
2 |
|
$state[$hash] = 1; |
54
|
|
|
|
55
|
2 |
|
$this->doSerialize($arr, $normalizers, $context->withParent($var), $doc, $parent, $this->getRoot($class), $state, $classes); |
56
|
|
|
|
57
|
2 |
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
if(\is_array($var)) { |
61
|
2 |
|
$item = $key ? $doc->createElement($key) : $parent; |
62
|
2 |
|
foreach($var as $index => $value) { |
63
|
2 |
|
$this->doSerialize($value, $normalizers, $context, $doc, $item, $index, $state, $classes); |
64
|
|
|
} |
65
|
2 |
|
!$key ?: $parent->appendChild($item); |
66
|
|
|
|
67
|
2 |
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
$parent->appendChild($doc->createElement($key, $var)); |
71
|
2 |
|
} |
72
|
|
|
|
73
|
1 |
|
public function unserialize($var, $class, Hydrators $hydrators) |
74
|
|
|
{ |
75
|
1 |
|
$doc = new \DOMDocument(); |
76
|
1 |
|
$doc->loadXML($var); |
77
|
1 |
|
$data = $this->parse($doc, $doc); |
78
|
1 |
|
$hydrator = $hydrators->getHandler($class); |
79
|
|
|
|
80
|
1 |
|
return $hydrator($data[$this->getRoot($class)], $hydrators); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
private function parse(\DOMDocument $doc, $parent = null) |
84
|
|
|
{ |
85
|
1 |
|
$ret = []; |
86
|
|
|
/** @var \DOMElement $parent */ |
87
|
|
|
/** @var \DOMElement $node */ |
88
|
1 |
|
$tags = []; |
89
|
1 |
|
foreach($parent->childNodes as $node) { |
90
|
1 |
|
if($node->nodeName === '#text') { |
91
|
1 |
|
continue; |
92
|
|
|
} |
93
|
1 |
|
if($node->childNodes->length === 1 && $node->childNodes->item(0) instanceof \DOMText) { |
94
|
1 |
|
$ret[$node->tagName] = $node->childNodes->item(0)->nodeValue; |
95
|
1 |
|
continue; |
96
|
|
|
} |
97
|
1 |
|
$result = $this->parse($doc, $node); |
98
|
1 |
|
if(array_key_exists($node->tagName, $ret)) { |
99
|
1 |
|
$tags[] = $node->tagName; |
100
|
1 |
|
$ret = [$ret[$node->tagName]]; |
101
|
1 |
|
$ret[] = $result; |
102
|
1 |
|
continue; |
103
|
|
|
} |
104
|
1 |
|
if(\in_array($node->tagName, $tags, true)) { |
105
|
1 |
|
$ret[] = $result; |
106
|
1 |
|
continue; |
107
|
|
|
} |
108
|
1 |
|
$ret[$node->tagName] = $result; |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
return $ret; |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
private function getRoot($class) |
115
|
|
|
{ |
116
|
2 |
|
return \call_user_func($this->rootProvider, $class); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.