-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormatter.php
215 lines (184 loc) · 6.39 KB
/
Formatter.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
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
<?php
declare(strict_types=1);
namespace Inspirum\XML\Formatter;
use DOMNode;
use Stringable;
use function array_keys;
use function array_merge;
use function count;
use function in_array;
use function is_array;
use function is_bool;
use function is_numeric;
use function is_scalar;
use function ltrim;
use function rtrim;
use function sprintf;
use function str_starts_with;
use function trim;
use const XML_CDATA_SECTION_NODE;
use const XML_TEXT_NODE;
final class Formatter
{
/**
* Prefix namespaces with xmlns
*
* @param array<string,string> $namespaces
*
* @return array<string,string>
*/
public static function namespacesToAttributes(array $namespaces): array
{
$attributes = [];
foreach ($namespaces as $namespaceLocalName => $namespaceValue) {
if (str_starts_with($namespaceLocalName, 'xmlns') === false) {
$namespaceLocalName = rtrim(sprintf('xmlns:%s', $namespaceLocalName), ':');
}
$attributes[$namespaceLocalName] = $namespaceValue;
}
return $attributes;
}
/**
* Normalize value.
*/
public static function encodeValue(mixed $value): ?string
{
if ($value === null) {
return null;
}
if (is_bool($value)) {
$value = $value ? 'true' : 'false';
}
if (is_scalar($value) || $value instanceof Stringable) {
return (string) $value;
}
return null;
}
/**
* Normalize value.
*/
public static function decodeValue(mixed $value): mixed
{
if (is_numeric($value)) {
return $value + 0;
}
if (in_array($value, ['true', 'True'], true)) {
return true;
}
if (in_array($value, ['false', 'False'], true)) {
return false;
}
return $value;
}
/**
* Convert DOM node to array
*/
public static function nodeToArray(DOMNode $node, Config $config): mixed
{
return self::nodeToArrayRecursive($node, $config, 1);
}
private static function nodeToArrayRecursive(DOMNode $node, Config $config, int $depth): mixed
{
$value = null;
/** @var array<string,mixed> $attributes */
$attributes = [];
/** @var array<string,array<mixed>> $nodes */
$nodes = [];
if ($node->hasAttributes()) {
/** @var \DOMAttr $attribute */
foreach ($node->attributes ?? [] as $attribute) {
$attributes[$attribute->nodeName] = $config->isAutoCast()
? self::decodeValue($attribute->nodeValue)
: $attribute->nodeValue;
}
}
if ($node->hasChildNodes()) {
/** @var \DOMNode $child */
foreach ($node->childNodes ?? [] as $child) {
if (in_array($child->nodeType, [XML_TEXT_NODE, XML_CDATA_SECTION_NODE])) {
if (trim((string) $child->nodeValue) !== '') {
$value = $config->isAutoCast()
? self::decodeValue($child->nodeValue)
: $child->nodeValue;
}
continue;
}
$childNodes = self::nodeToArrayRecursive($child, $config, $depth + 1);
if ($config->isFlatten()) {
self::flattenArray($nodes, $config->isWithoutRoot() && $depth === 1 ? '' : $child->nodeName, $childNodes, $config);
} else {
$nodes[$child->nodeName][] = $childNodes;
}
}
}
if ($config->isFullResponse()) {
return [
$config->getAttributesName() => $attributes,
$config->getValueName() => $value,
$config->getNodesName() => $nodes,
];
}
if (count($nodes) === 0 && count($attributes) === 0) {
return $value;
}
return self::simplifyArray($attributes, $value, $nodes, $config, $node);
}
/**
* Flatten node to one-dimensional array
*
* @param array<string,array<mixed>> $nodes
*/
private static function flattenArray(array &$nodes, string $nodeNames, mixed $childNodes, Config $config): void
{
if (!is_array($childNodes)) {
$nodes[$nodeNames][] = $childNodes;
return;
}
foreach ($childNodes as $childNodeName => $childNodeValues) {
if ($childNodeName === $config->getAttributesName() && is_array($childNodeValues)) {
foreach ($childNodeValues as $attributeName => $attributeValue) {
$nodeKey = sprintf('%s%s%s', $nodeNames, $config->getFlattenAttributes(), $attributeName);
$nodes[$nodeKey][] = $attributeValue;
}
} elseif ($childNodeName === $config->getValueName()) {
$nodes[$nodeNames][] = $childNodeValues;
} else {
$nodeKey = ltrim(sprintf('%s%s%s', $nodeNames, $config->getFlattenNodes(), $childNodeName), $config->getFlattenNodes());
if (is_array($childNodeValues)) {
$nodes[$nodeKey] = array_merge($nodes[$nodeKey] ?? [], $childNodeValues);
} else {
$nodes[$nodeKey][] = $childNodeValues;
}
}
}
}
/**
* Remove unnecessary data
*
* @param array<string,mixed> $attributes
* @param array<string,array<mixed>> $nodes
*
* @return array<int|string,mixed>
*/
private static function simplifyArray(array $attributes, mixed $value, array $nodes, Config $config, DOMNode $node): array
{
$simpleResult = $nodes;
foreach ($nodes as $nodeName => $values) {
if (
!$config->isAlwaysArray()
&& in_array($nodeName, $config->getAlwaysArrayNodeNames(), true) === false
&& in_array($node->nodeName . '.' . $nodeName, $config->getAlwaysArrayNodeNames(), true) === false
&& array_keys($values) === [0]
) {
$simpleResult[$nodeName] = $values[0];
}
}
if (count($attributes) > 0) {
$simpleResult[$config->getAttributesName()] = $attributes;
}
if ($value !== null) {
$simpleResult[$config->getValueName()] = $value;
}
return $simpleResult;
}
}