1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
5
|
|
|
* Date: 7/18/15 |
6
|
|
|
* Time: 2:26 PM. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace NilPortugues\Api\Xml; |
13
|
|
|
|
14
|
|
|
use NilPortugues\Api\Transformer\Helpers\RecursiveDeleteHelper; |
15
|
|
|
use NilPortugues\Api\Transformer\Helpers\RecursiveFormatterHelper; |
16
|
|
|
use NilPortugues\Api\Transformer\Helpers\RecursiveRenamerHelper; |
17
|
|
|
use NilPortugues\Api\Transformer\Transformer; |
18
|
|
|
use NilPortugues\Serializer\Serializer; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class XmlTransformer. |
22
|
|
|
*/ |
23
|
|
|
class XmlTransformer extends Transformer |
24
|
|
|
{ |
25
|
|
|
const META_KEY = 'meta'; |
26
|
|
|
const LINKS = 'links'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param mixed $value |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function serialize($value) |
34
|
|
|
{ |
35
|
|
|
$presenter = new XmlPresenter(); |
36
|
|
|
|
37
|
|
|
return $presenter->output($this->serialization($value)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param $value |
42
|
|
|
* |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
protected function serialization($value) |
46
|
|
|
{ |
47
|
|
|
if (is_array($value) && !empty($value[Serializer::MAP_TYPE])) { |
48
|
|
|
$data = []; |
49
|
|
|
foreach ($value[Serializer::SCALAR_VALUE] as $v) { |
50
|
|
|
$data[] = $this->serializeObject($v); |
51
|
|
|
} |
52
|
|
|
} else { |
53
|
|
|
$data = $this->serializeObject($value); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $data; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param array $value |
61
|
|
|
* |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
protected function serializeObject(array $value) |
65
|
|
|
{ |
66
|
|
|
if (null !== $this->mappings) { |
67
|
|
|
/** @var \NilPortugues\Api\Mapping\Mapping $mapping */ |
68
|
|
|
foreach ($this->mappings as $class => $mapping) { |
69
|
|
|
RecursiveDeleteHelper::deleteProperties($this->mappings, $value, $class); |
70
|
|
|
RecursiveRenamerHelper::renameKeyValue($this->mappings, $value, $class); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->setResponseMeta($value); |
75
|
|
|
$this->setResponseLinks($value); |
76
|
|
|
self::formatScalarValues($value); |
77
|
|
|
RecursiveDeleteHelper::deleteKeys($value, [Serializer::CLASS_IDENTIFIER_KEY]); |
78
|
|
|
self::flattenObjectsWithSingleKeyScalars($value); |
79
|
|
|
|
80
|
|
|
return $value; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $response |
85
|
|
|
*/ |
86
|
|
|
private function setResponseMeta(array &$response) |
87
|
|
|
{ |
88
|
|
|
if (!empty($this->meta)) { |
89
|
|
|
$response[self::META_KEY] = $this->meta; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param array $response |
95
|
|
|
*/ |
96
|
|
|
private function setResponseLinks(array &$response) |
97
|
|
|
{ |
98
|
|
|
$links = array_filter( |
99
|
|
|
array_merge( |
100
|
|
|
$this->buildSelfLink($response), |
101
|
|
|
$this->buildLinks(), |
102
|
|
|
$this->getResponseAdditionalLinks($response, $response[Serializer::CLASS_IDENTIFIER_KEY]) |
103
|
|
|
) |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
if (!empty($links)) { |
107
|
|
|
$response[self::LINKS] = $this->addHrefToLinks($links); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param array $response |
113
|
|
|
* |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
protected function buildSelfLink(array &$response) |
117
|
|
|
{ |
118
|
|
|
$link = []; |
119
|
|
|
|
120
|
|
|
if (!empty($type = $response[Serializer::CLASS_IDENTIFIER_KEY])) { |
121
|
|
|
list($idValues, $idProperties) = RecursiveFormatterHelper::getIdPropertyAndValues( |
122
|
|
|
$this->mappings, |
123
|
|
|
$response, |
124
|
|
|
$type |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
$href = self::buildUrl( |
128
|
|
|
$this->mappings, |
129
|
|
|
$idProperties, |
130
|
|
|
$idValues, |
131
|
|
|
$this->mappings[$type]->getResourceUrl(), |
132
|
|
|
$type |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
if ($href != $this->mappings[$type]->getResourceUrl()) { |
136
|
|
|
$link[self::SELF_LINK] = $href; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $link; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param \NilPortugues\Api\Mapping\Mapping[] $mappings |
145
|
|
|
* @param $idProperties |
146
|
|
|
* @param $idValues |
147
|
|
|
* @param $url |
148
|
|
|
* @param $type |
149
|
|
|
* |
150
|
|
|
* @return mixed |
151
|
|
|
*/ |
152
|
|
|
protected static function buildUrl(array &$mappings, $idProperties, $idValues, $url, $type) |
153
|
|
|
{ |
154
|
|
|
$outputUrl = \str_replace($idProperties, $idValues, $url); |
155
|
|
|
|
156
|
|
|
if ($outputUrl !== $url) { |
157
|
|
|
return $outputUrl; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$outputUrl = self::secondPassBuildUrl([$mappings[$type]->getClassAlias()], $idValues, $url); |
161
|
|
|
|
162
|
|
|
if ($outputUrl !== $url) { |
163
|
|
|
return $outputUrl; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$className = $mappings[$type]->getClassName(); |
167
|
|
|
$className = \explode('\\', $className); |
168
|
|
|
$className = \array_pop($className); |
169
|
|
|
|
170
|
|
|
$outputUrl = self::secondPassBuildUrl([$className], $idValues, $url); |
171
|
|
|
if ($outputUrl !== $url) { |
172
|
|
|
return $outputUrl; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $url; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param $idPropertyName |
180
|
|
|
* @param $idValues |
181
|
|
|
* @param $url |
182
|
|
|
* |
183
|
|
|
* @return mixed |
184
|
|
|
*/ |
185
|
|
|
protected static function secondPassBuildUrl($idPropertyName, $idValues, $url) |
186
|
|
|
{ |
187
|
|
|
if (!empty($idPropertyName)) { |
188
|
|
|
$outputUrl = self::toCamelCase($idPropertyName, $idValues, $url); |
189
|
|
|
if ($url !== $outputUrl) { |
190
|
|
|
return $outputUrl; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$outputUrl = self::toLowerFirstCamelCase($idPropertyName, $idValues, $url); |
194
|
|
|
if ($url !== $outputUrl) { |
195
|
|
|
return $outputUrl; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$outputUrl = self::toUnderScore($idPropertyName, $idValues, $url); |
199
|
|
|
if ($url !== $outputUrl) { |
200
|
|
|
return $outputUrl; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $url; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param $original |
209
|
|
|
* @param $idValues |
210
|
|
|
* @param $url |
211
|
|
|
* |
212
|
|
|
* @return mixed |
213
|
|
|
*/ |
214
|
|
View Code Duplication |
protected static function toCamelCase($original, $idValues, $url) |
|
|
|
|
215
|
|
|
{ |
216
|
|
|
foreach ($original as &$o) { |
217
|
|
|
$o = '{'.self::underscoreToCamelCase(self::camelCaseToUnderscore($o)).'}'; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return \str_replace($original, $idValues, $url); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Converts a underscore string to camelCase. |
225
|
|
|
* |
226
|
|
|
* @param string $string |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
protected static function underscoreToCamelCase($string) |
231
|
|
|
{ |
232
|
|
|
return \str_replace(' ', '', \ucwords(\strtolower(\str_replace(['_', '-'], ' ', $string)))); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Transforms a given string from camelCase to under_score style. |
237
|
|
|
* |
238
|
|
|
* @param string $camel |
239
|
|
|
* @param string $splitter |
240
|
|
|
* |
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
|
|
protected static function camelCaseToUnderscore($camel, $splitter = '_') |
244
|
|
|
{ |
245
|
|
|
$camel = \preg_replace( |
246
|
|
|
'/(?!^)[[:upper:]][[:lower:]]/', |
247
|
|
|
'$0', |
248
|
|
|
\preg_replace('/(?!^)[[:upper:]]+/', $splitter.'$0', $camel) |
249
|
|
|
); |
250
|
|
|
|
251
|
|
|
return \strtolower($camel); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param $original |
256
|
|
|
* @param $idValues |
257
|
|
|
* @param $url |
258
|
|
|
* |
259
|
|
|
* @return mixed |
260
|
|
|
*/ |
261
|
|
|
protected static function toLowerFirstCamelCase($original, $idValues, $url) |
262
|
|
|
{ |
263
|
|
|
foreach ($original as &$o) { |
264
|
|
|
$o = self::underscoreToCamelCase(self::camelCaseToUnderscore($o)); |
265
|
|
|
$o[0] = \strtolower($o[0]); |
266
|
|
|
$o = '{'.$o.'}'; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
return \str_replace($original, $idValues, $url); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param $original |
274
|
|
|
* @param $idValues |
275
|
|
|
* @param $url |
276
|
|
|
* |
277
|
|
|
* @return mixed |
278
|
|
|
*/ |
279
|
|
View Code Duplication |
protected static function toUnderScore($original, $idValues, $url) |
|
|
|
|
280
|
|
|
{ |
281
|
|
|
foreach ($original as &$o) { |
282
|
|
|
$o = '{'.self::camelCaseToUnderscore($o).'}'; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
return \str_replace($original, $idValues, $url); |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
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.