-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonxTypeDecoder.php
More file actions
144 lines (111 loc) · 3.54 KB
/
JsonxTypeDecoder.php
File metadata and controls
144 lines (111 loc) · 3.54 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
<?php
declare(strict_types=1);
namespace Chubbyphp\DecodeEncode\Decoder;
use Chubbyphp\DecodeEncode\RuntimeException;
/**
* @see https://www.ibm.com/support/knowledgecenter/SS9H2Y_7.6.0/com.ibm.dp.doc/json_jsonx.html
*/
final class JsonxTypeDecoder implements TypeDecoderInterface
{
private const string DATATYPE_OBJECT = 'object';
private const string DATATYPE_ARRAY = 'array';
private const string DATATYPE_BOOLEAN = 'boolean';
private const string DATATYPE_STRING = 'string';
private const string DATATYPE_NUMBER = 'number';
private const string DATATYPE_NULL = 'null';
public function getContentType(): string
{
return 'application/jsonx+xml';
}
/**
* @return array<string, mixed>
*
* @throws RuntimeException
*/
public function decode(string $data): array
{
$document = new \DOMDocument();
@$document->loadXML($data);
$documentElement = $document->documentElement;
if (null === $documentElement) {
throw RuntimeException::createNotParsable($this->getContentType());
}
$result = $this->decodeNode($documentElement);
if (!\is_array($result)) {
throw RuntimeException::createNotParsable($this->getContentType());
}
/** @var array<string, mixed> $result */
return $result;
}
/**
* @return null|array<mixed>|bool|float|int|string
*/
private function decodeNode(\DOMElement $node): array|bool|float|int|string|null
{
$nodeName = $node->nodeName;
$nodeType = substr($nodeName, 5);
if (self::DATATYPE_OBJECT === $nodeType) {
return $this->decodeObjectNode($node);
}
if (self::DATATYPE_ARRAY === $nodeType) {
return $this->decodeArrayNode($node);
}
if (self::DATATYPE_BOOLEAN === $nodeType) {
return $this->decodeBooleanNode($node);
}
if (self::DATATYPE_STRING === $nodeType) {
return $this->decodeStringNode($node);
}
if (self::DATATYPE_NUMBER === $nodeType) {
return $this->decodeNumberNode($node);
}
if (self::DATATYPE_NULL === $nodeType) {
return null;
}
throw RuntimeException::createNotParsable($this->getContentType());
}
/**
* @return array<string, mixed>
*/
private function decodeObjectNode(\DOMElement $node): array
{
$data = [];
foreach ($node->childNodes as $childNode) {
if (!$childNode instanceof \DOMElement) {
continue;
}
$data[$childNode->getAttribute('name')] = $this->decodeNode($childNode);
}
return $data;
}
/**
* @return list<mixed>
*/
private function decodeArrayNode(\DOMElement $node): array
{
$data = [];
foreach ($node->childNodes as $childNode) {
if (!$childNode instanceof \DOMElement) {
continue;
}
$data[] = $this->decodeNode($childNode);
}
return $data;
}
private function decodeBooleanNode(\DOMElement $node): bool
{
return 'true' === $node->nodeValue;
}
private function decodeStringNode(\DOMElement $node): string
{
return $node->nodeValue ?? '';
}
private function decodeNumberNode(\DOMElement $node): float|int
{
$value = $node->nodeValue ?? '0';
if ($value === (string) (int) $value) {
return (int) $value;
}
return (float) $value;
}
}