1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\Serialization\Encoder; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Inflector\Inflector; |
8
|
|
|
|
9
|
|
|
final class XmlTypeEncoder implements TypeEncoderInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var bool |
13
|
|
|
*/ |
14
|
|
|
private $prettyPrint; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param bool $prettyPrint |
18
|
|
|
*/ |
19
|
4 |
|
public function __construct(bool $prettyPrint = false) |
20
|
|
|
{ |
21
|
4 |
|
$this->prettyPrint = $prettyPrint; |
22
|
4 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
1 |
|
public function getContentType(): string |
28
|
|
|
{ |
29
|
1 |
|
return 'application/xml'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param array $data |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
3 |
|
public function encode(array $data): string |
38
|
|
|
{ |
39
|
3 |
|
if (!isset($data['_type'])) { |
40
|
1 |
|
throw new \InvalidArgumentException('Data missing _type'); |
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
$document = new \DOMDocument('1.0', 'UTF-8'); |
44
|
2 |
|
$document->formatOutput = $this->prettyPrint; |
45
|
|
|
|
46
|
2 |
|
$listNode = $this->createMetadataNode($document, $data['_type']); |
47
|
2 |
|
$document->appendChild($listNode); |
48
|
|
|
|
49
|
2 |
|
unset($data['_type']); |
50
|
|
|
|
51
|
2 |
|
$this->dataToNodes($document, $listNode, $data); |
52
|
|
|
|
53
|
1 |
|
return trim($document->saveXML($document, LIBXML_NOEMPTYTAG)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param \DOMDocument $document |
58
|
|
|
* @param string $type |
59
|
|
|
* |
60
|
|
|
* @return \DOMNode |
61
|
|
|
*/ |
62
|
2 |
|
private function createMetadataNode(\DOMDocument $document, string $type): \DOMNode |
63
|
|
|
{ |
64
|
2 |
|
$node = $document->createElement('object'); |
65
|
2 |
|
if (null !== $type) { |
66
|
2 |
|
$node->setAttribute('type', $type); |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
return $node; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $name |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
1 |
|
private function getMetaPrefix(string $name) |
78
|
|
|
{ |
79
|
1 |
|
return 'meta-'.$name; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param \DOMDocument $document |
84
|
|
|
* @param \DOMNode $listNode |
85
|
|
|
* @param array $data |
86
|
|
|
*/ |
87
|
2 |
|
private function dataToNodes(\DOMDocument $document, \DOMNode $listNode, array $data) |
88
|
|
|
{ |
89
|
2 |
|
foreach ($data as $key => $value) { |
90
|
2 |
|
if (is_string($key) && '_' === $key[0]) { |
91
|
1 |
|
$key = $this->getMetaPrefix(substr($key, 1)); |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
if (is_array($value)) { |
95
|
1 |
|
$childNode = $this->dataToArrayNode($document, $listNode, $key, $value); |
96
|
|
|
} else { |
97
|
2 |
|
$childNode = $this->dataToScalarNode($document, $listNode, $key, $value); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
if (is_int($key)) { |
101
|
1 |
|
$childNode->setAttribute('key', (string) $key); |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
$listNode->appendChild($childNode); |
105
|
|
|
} |
106
|
1 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param \DOMDocument $document |
110
|
|
|
* @param \DOMNode $listNode |
111
|
|
|
* @param string|int $key |
112
|
|
|
* @param array $value |
113
|
|
|
* |
114
|
|
|
* @return \DOMElement|\DOMNode |
115
|
|
|
*/ |
116
|
1 |
|
private function dataToArrayNode(\DOMDocument $document, \DOMNode $listNode, $key, array $value) |
117
|
|
|
{ |
118
|
1 |
|
if (!isset($value['_type'])) { |
119
|
1 |
|
$childNode = $document->createElement(is_int($key) ? Inflector::singularize($listNode->nodeName) : $key); |
120
|
1 |
|
$this->dataToNodes($document, $childNode, $value); |
121
|
|
|
|
122
|
1 |
|
return $childNode; |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
if (is_int($key)) { |
126
|
1 |
|
$childNode = $this->createMetadataNode($document, $value['_type']); |
127
|
|
|
|
128
|
1 |
|
unset($value['_type']); |
129
|
|
|
|
130
|
1 |
|
$this->dataToNodes($document, $childNode, $value); |
131
|
|
|
|
132
|
1 |
|
return $childNode; |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
$subChildNode = $this->createMetadataNode($document, $value['_type']); |
136
|
|
|
|
137
|
1 |
|
unset($value['_type']); |
138
|
|
|
|
139
|
1 |
|
$childNode = $document->createElement($key); |
140
|
1 |
|
$childNode->appendChild($subChildNode); |
141
|
|
|
|
142
|
1 |
|
$this->dataToNodes($document, $subChildNode, $value); |
143
|
|
|
|
144
|
1 |
|
return $childNode; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param \DOMDocument $document |
149
|
|
|
* @param \DOMNode $listNode |
150
|
|
|
* @param string|int $key |
151
|
|
|
* @param string|null|bool|float|int $value |
152
|
|
|
* |
153
|
|
|
* @return \DOMNode |
154
|
|
|
*/ |
155
|
2 |
|
private function dataToScalarNode(\DOMDocument $document, \DOMNode $listNode, $key, $value): \DOMNode |
156
|
|
|
{ |
157
|
2 |
|
$stringKey = is_int($key) ? Inflector::singularize($listNode->nodeName) : $key; |
158
|
|
|
|
159
|
2 |
|
if (is_string($value)) { |
160
|
1 |
|
if (false !== strpos($value, '<') || false !== strpos($value, '&')) { |
161
|
1 |
|
$childNode = $document->createElement($stringKey); |
162
|
1 |
|
$childNode->appendChild($document->createCDATASection($value)); |
163
|
|
|
} else { |
164
|
1 |
|
$childNode = $document->createElement($stringKey, $value); |
165
|
|
|
} |
166
|
|
|
|
167
|
1 |
|
$childNode->setAttribute('type', 'string'); |
168
|
|
|
|
169
|
1 |
|
return $childNode; |
170
|
|
|
} |
171
|
|
|
|
172
|
2 |
|
if (null === $value) { |
173
|
1 |
|
return $document->createElement($stringKey); |
174
|
|
|
} |
175
|
|
|
|
176
|
2 |
|
$childNode = $document->createElement($stringKey, $this->getValueAsString($value)); |
177
|
1 |
|
$childNode->setAttribute('type', $this->getType($value)); |
178
|
|
|
|
179
|
1 |
|
return $childNode; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param bool|float|int $value |
184
|
|
|
* |
185
|
|
|
* @return string |
186
|
|
|
* |
187
|
|
|
* @throws \InvalidArgumentException |
188
|
|
|
*/ |
189
|
2 |
View Code Duplication |
private function getValueAsString($value): string |
|
|
|
|
190
|
|
|
{ |
191
|
2 |
|
if (is_bool($value)) { |
192
|
1 |
|
return $value ? 'true' : 'false'; |
193
|
|
|
} |
194
|
|
|
|
195
|
2 |
|
if (is_float($value)) { |
196
|
1 |
|
$value = (string) $value; |
197
|
1 |
|
if (false === strpos($value, '.')) { |
198
|
1 |
|
$value .= '.0'; |
199
|
|
|
} |
200
|
|
|
|
201
|
1 |
|
return $value; |
202
|
|
|
} |
203
|
|
|
|
204
|
2 |
|
if (is_int($value)) { |
205
|
1 |
|
return (string) $value; |
206
|
|
|
} |
207
|
|
|
|
208
|
1 |
|
throw new \InvalidArgumentException(sprintf('Unsupported data type: %s', gettype($value))); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param bool|float|int|string $value |
213
|
|
|
* |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
1 |
|
private function getType($value): string |
217
|
|
|
{ |
218
|
1 |
|
if (is_bool($value)) { |
219
|
1 |
|
return 'boolean'; |
220
|
|
|
} |
221
|
|
|
|
222
|
1 |
|
if (is_float($value)) { |
223
|
1 |
|
return 'float'; |
224
|
|
|
} |
225
|
|
|
|
226
|
1 |
|
return 'integer'; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
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.