These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Chubbyphp\Deserialization\Decoder; |
||
6 | |||
7 | use Chubbyphp\Deserialization\DeserializerRuntimeException; |
||
8 | |||
9 | /** |
||
10 | * @see https://www.ibm.com/support/knowledgecenter/SS9H2Y_7.6.0/com.ibm.dp.doc/json_jsonx.html |
||
11 | */ |
||
12 | final class JsonxTypeDecoder implements TypeDecoderInterface |
||
13 | { |
||
14 | const DATATYPE_OBJECT = 'object'; |
||
15 | const DATATYPE_ARRAY = 'array'; |
||
16 | const DATATYPE_BOOLEAN = 'boolean'; |
||
17 | const DATATYPE_STRING = 'string'; |
||
18 | const DATATYPE_NUMBER = 'number'; |
||
19 | const DATATYPE_NULL = 'null'; |
||
20 | |||
21 | /** |
||
22 | * @return string |
||
23 | */ |
||
24 | 4 | public function getContentType(): string |
|
25 | { |
||
26 | 4 | return 'application/x-jsonx'; |
|
27 | } |
||
28 | |||
29 | /** |
||
30 | * @param string $data |
||
31 | * |
||
32 | * @throws DeserializerRuntimeException |
||
33 | * |
||
34 | * @return array |
||
35 | */ |
||
36 | 5 | public function decode(string $data): array |
|
37 | { |
||
38 | 5 | $document = new \DOMDocument(); |
|
39 | |||
40 | 5 | if (!@$document->loadXML($data)) { |
|
41 | 1 | throw DeserializerRuntimeException::createNotParsable($this->getContentType()); |
|
42 | } |
||
43 | |||
44 | 4 | return $this->decodeNode($document->documentElement); |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param \DOMNode $node |
||
49 | * |
||
50 | * @return array|bool|string|int|float|null |
||
51 | */ |
||
52 | 4 | private function decodeNode(\DOMNode $node) |
|
53 | { |
||
54 | 4 | $nodeName = $node->nodeName; |
|
55 | |||
56 | 4 | $nodeType = substr($nodeName, 5); |
|
57 | |||
58 | 4 | if (self::DATATYPE_OBJECT === $nodeType) { |
|
59 | 2 | return $this->decodeObjectNode($node); |
|
60 | } |
||
61 | |||
62 | 4 | if (self::DATATYPE_ARRAY === $nodeType) { |
|
63 | 1 | return $this->decodeArrayNode($node); |
|
64 | } |
||
65 | |||
66 | 4 | if (self::DATATYPE_BOOLEAN === $nodeType) { |
|
67 | 2 | return $this->decodeBooleanNode($node); |
|
68 | } |
||
69 | |||
70 | 4 | if (self::DATATYPE_STRING === $nodeType) { |
|
71 | 2 | return $this->decodeStringNode($node); |
|
72 | } |
||
73 | |||
74 | 4 | if (self::DATATYPE_NUMBER === $nodeType) { |
|
75 | 2 | return $this->decodeNumberNode($node); |
|
76 | } |
||
77 | |||
78 | 3 | if (self::DATATYPE_NULL === $nodeType) { |
|
79 | 1 | return null; |
|
80 | } |
||
81 | |||
82 | 2 | throw DeserializerRuntimeException::createNotParsable($this->getContentType()); |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param \DOMNode $node |
||
87 | * |
||
88 | * @return array |
||
89 | */ |
||
90 | 2 | View Code Duplication | private function decodeObjectNode(\DOMNode $node): array |
0 ignored issues
–
show
|
|||
91 | { |
||
92 | 2 | $data = []; |
|
93 | 2 | foreach ($node->childNodes as $childNode) { |
|
94 | 2 | if ($childNode instanceof \DOMText) { |
|
95 | 2 | continue; |
|
96 | } |
||
97 | |||
98 | 2 | $data[$childNode->getAttribute('name')] = $this->decodeNode($childNode); |
|
99 | } |
||
100 | |||
101 | 2 | return $data; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param \DOMNode $node |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | 1 | View Code Duplication | private function decodeArrayNode(\DOMNode $node): array |
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
110 | { |
||
111 | 1 | $data = []; |
|
112 | 1 | foreach ($node->childNodes as $childNode) { |
|
113 | 1 | if ($childNode instanceof \DOMText) { |
|
114 | 1 | continue; |
|
115 | } |
||
116 | |||
117 | 1 | $data[] = $this->decodeNode($childNode); |
|
118 | } |
||
119 | |||
120 | 1 | return $data; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param \DOMNode $node |
||
125 | * |
||
126 | * @return bool |
||
127 | */ |
||
128 | 2 | private function decodeBooleanNode(\DOMNode $node): bool |
|
129 | { |
||
130 | 2 | return 'true' === $node->nodeValue; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * @param \DOMNode $node |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | 2 | private function decodeStringNode(\DOMNode $node): string |
|
139 | { |
||
140 | 2 | return html_entity_decode($node->nodeValue, ENT_COMPAT | ENT_XML1, 'UTF-8'); |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param \DOMNode $node |
||
145 | * |
||
146 | * @return int|float |
||
147 | */ |
||
148 | 2 | private function decodeNumberNode(\DOMNode $node) |
|
149 | { |
||
150 | 2 | $value = $node->nodeValue; |
|
151 | |||
152 | 2 | if ($value === (string) (int) $value) { |
|
153 | 2 | return (int) $value; |
|
154 | } |
||
155 | |||
156 | 2 | return (float) $value; |
|
157 | } |
||
158 | } |
||
159 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.