-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlEncoder.php
More file actions
122 lines (106 loc) · 3.96 KB
/
XmlEncoder.php
File metadata and controls
122 lines (106 loc) · 3.96 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
<?php
declare(strict_types=1);
namespace KaririCode\Serializer\Encoder;
use KaririCode\Serializer\Contract\Encoder;
use KaririCode\Serializer\Contract\SerializationContext;
use KaririCode\Serializer\Exception\SerializationException;
/**
* Encodes/decodes arrays as application/xml via SimpleXMLElement + DOMDocument.
*
* Uses `libxml_use_internal_errors` to suppress PHP notices on malformed XML decode.
* Supports `root` and `pretty` context parameters.
*
* @package KaririCode\Serializer\Encoder
* @author Walmir Silva <walmir.silva@kariricode.org>
* @since 3.1.0 ARFA 1.3
*/
final readonly class XmlEncoder implements Encoder
{
/** @param array<mixed> $data */
#[\Override]
public function encode(array $data, SerializationContext $context): string
{
$rootParam = $context->getParameter('root', 'root');
$rootTag = \is_string($rootParam) ? $rootParam : 'root';
try {
$xml = new \SimpleXMLElement("<{$rootTag}/>");
$this->arrayToXml($data, $xml);
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = (bool) $context->getParameter('pretty', false);
$xmlString = $xml->asXML();
if (! \is_string($xmlString) || $xmlString === '') {
throw SerializationException::encodingFailed('xml', 'asXML() returned empty.');
}
$dom->loadXML($xmlString);
return $dom->saveXML() ?: '';
} catch (\Throwable $e) {
throw SerializationException::encodingFailed('xml', $e->getMessage());
}
}
/** @return array<mixed> */
#[\Override]
public function decode(string $payload, SerializationContext $context): array
{
// Suppress PHP warnings emitted by SimpleXMLElement before throwing on invalid XML.
// Without this, `new SimpleXMLElement('not xml')` emits 3 PHP notices that CI
// treats as test-suite warnings (exit code 1 even with failOnWarning=false).
$prevErrors = libxml_use_internal_errors(true);
try {
$xml = new \SimpleXMLElement($payload, LIBXML_NOCDATA);
libxml_use_internal_errors($prevErrors);
return $this->xmlToArray($xml);
} catch (\Throwable $e) {
libxml_use_internal_errors($prevErrors);
libxml_clear_errors();
throw SerializationException::decodingFailed('xml', $e->getMessage());
}
}
#[\Override]
public function supports(string $format): bool
{
return $format === 'xml';
}
#[\Override]
public function getFormat(): string
{
return 'xml';
}
/** @param array<mixed> $data */
private function arrayToXml(array $data, \SimpleXMLElement $xml): void
{
foreach ($data as $key => $value) {
$tag = \is_int($key) ? 'item' : $key;
if (\is_array($value)) {
$child = $xml->addChild($tag);
if ($child !== null) {
$this->arrayToXml($value, $child);
}
} else {
$strValue = \is_scalar($value) || $value === null ? (string) $value : '';
$xml->addChild($tag, htmlspecialchars($strValue, ENT_XML1));
}
}
}
/** @return array<mixed> */
private function xmlToArray(\SimpleXMLElement $xml): array
{
$result = [];
$children = $xml->children();
if ($children === null) {
return $result;
}
foreach ($children as $key => $child) {
$value = $child->count() > 0 ? $this->xmlToArray($child) : (string) $child;
if (isset($result[$key])) {
if (! \is_array($result[$key]) || ! isset($result[$key][0])) {
$result[$key] = [$result[$key]];
}
$result[$key][] = $value;
} else {
$result[$key] = $value;
}
}
return $result;
}
}