|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dontdrinkandroot\RestBundle\Serializer; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\Collection; |
|
6
|
|
|
use Doctrine\Common\Util\ClassUtils; |
|
7
|
|
|
use Doctrine\DBAL\Types\Type; |
|
8
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Method; |
|
9
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\ClassMetadata; |
|
10
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\PropertyMetadata; |
|
11
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\RestMetadataFactory; |
|
12
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
|
13
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
14
|
|
|
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; |
|
15
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author Philip Washington Sorst <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class RestNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface |
|
21
|
|
|
{ |
|
22
|
|
|
const DDR_REST_INCLUDES = 'ddrRestIncludes'; |
|
23
|
|
|
const DDR_REST_PATH = 'ddrRestPath'; |
|
24
|
|
|
const DDR_REST_DEPTH = 'ddrRestDepth'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var RestMetadataFactory |
|
28
|
|
|
*/ |
|
29
|
|
|
private $metadataFactory; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var PropertyAccessorInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $propertyAccessor; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var UrlGeneratorInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $urlGenerator; |
|
40
|
|
|
|
|
41
|
82 |
|
public function __construct( |
|
42
|
|
|
RestMetadataFactory $metadataFactory, |
|
43
|
|
|
PropertyAccessorInterface $propertyAccessor, |
|
44
|
|
|
UrlGeneratorInterface $urlGenerator |
|
45
|
|
|
) { |
|
46
|
82 |
|
$this->metadataFactory = $metadataFactory; |
|
47
|
82 |
|
$this->propertyAccessor = $propertyAccessor; |
|
48
|
82 |
|
$this->urlGenerator = $urlGenerator; |
|
49
|
82 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
64 |
|
public function normalize($data, $format = null, array $context = []) |
|
55
|
|
|
{ |
|
56
|
64 |
|
if (!array_key_exists(self::DDR_REST_INCLUDES, $context)) { |
|
57
|
|
|
throw new \LogicException('Includes missing'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
64 |
|
if (!array_key_exists(self::DDR_REST_PATH, $context)) { |
|
61
|
|
|
throw new \LogicException('Path missing'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
64 |
|
if (!array_key_exists(self::DDR_REST_DEPTH, $context)) { |
|
65
|
|
|
throw new \LogicException('Depth missing'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
64 |
|
$includes = $context[self::DDR_REST_INCLUDES]; |
|
69
|
64 |
|
$path = $context[self::DDR_REST_PATH]; |
|
70
|
64 |
|
$depth = $context[self::DDR_REST_DEPTH]; |
|
71
|
|
|
|
|
72
|
64 |
|
if (is_array($data)) { |
|
73
|
34 |
|
$normalizedData = []; |
|
74
|
34 |
|
foreach ($data as $datum) { |
|
75
|
34 |
|
$normalizedData[] = $this->normalize( |
|
76
|
34 |
|
$datum, |
|
77
|
34 |
|
$format, |
|
78
|
|
|
[ |
|
79
|
34 |
|
self::DDR_REST_INCLUDES => $includes, |
|
80
|
34 |
|
self::DDR_REST_DEPTH => $depth + 1, |
|
81
|
34 |
|
self::DDR_REST_PATH => $path |
|
82
|
|
|
] |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
34 |
|
return $normalizedData; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
64 |
|
if (is_object($data)) { |
|
90
|
|
|
|
|
91
|
|
|
/** @var ClassMetadata $classMetadata */ |
|
92
|
64 |
|
$classMetadata = $this->metadataFactory->getMetadataForClass(ClassUtils::getClass($data)); |
|
93
|
|
|
|
|
94
|
64 |
|
$normalizedData = []; |
|
95
|
|
|
|
|
96
|
64 |
|
if ($classMetadata->isRestResource() && $classMetadata->hasMethod(Method::GET) && $this->isIncluded( |
|
97
|
52 |
|
$path, |
|
98
|
52 |
|
['_links'], |
|
99
|
64 |
|
$includes |
|
100
|
|
|
) |
|
101
|
|
|
) { |
|
102
|
2 |
|
$selfLink = $this->urlGenerator->generate( |
|
103
|
2 |
|
$classMetadata->namePrefix . '.get', |
|
104
|
2 |
|
['id' => $this->propertyAccessor->getValue($data, $classMetadata->getIdField())], |
|
105
|
2 |
|
UrlGeneratorInterface::ABSOLUTE_URL |
|
106
|
|
|
); |
|
107
|
2 |
|
$normalizedData['_links'] = [ |
|
108
|
|
|
'self' => [ |
|
109
|
2 |
|
'href' => $selfLink |
|
110
|
|
|
] |
|
111
|
|
|
]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** @var PropertyMetadata $propertyMetadatum */ |
|
115
|
64 |
|
foreach ($classMetadata->propertyMetadata as $propertyMetadatum) { |
|
116
|
|
|
|
|
117
|
64 |
|
if ($propertyMetadatum->isExcluded()) { |
|
118
|
38 |
|
continue; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
64 |
|
if ($propertyMetadatum->isAssociation()) { |
|
122
|
|
|
|
|
123
|
|
|
/* Inlude if includable AND it is on include path */ |
|
124
|
46 |
|
if ($propertyMetadatum->isIncludable() && $this->isIncluded( |
|
125
|
46 |
|
$path, |
|
126
|
46 |
|
$propertyMetadatum->getIncludablePaths(), |
|
127
|
46 |
|
$includes |
|
128
|
|
|
) |
|
129
|
|
|
) { |
|
130
|
30 |
|
$value = $this->propertyAccessor->getValue($data, $propertyMetadatum->name); |
|
131
|
30 |
|
if ($propertyMetadatum->isCollection()) { |
|
132
|
|
|
/** @var Collection $value */ |
|
133
|
14 |
|
$value = $value->getValues(); |
|
134
|
|
|
} |
|
135
|
30 |
|
$normalizedData[$propertyMetadatum->name] = $this->normalize( |
|
136
|
30 |
|
$value, |
|
137
|
30 |
|
$format, |
|
138
|
|
|
[ |
|
139
|
30 |
|
self::DDR_REST_INCLUDES => $includes, |
|
140
|
30 |
|
self::DDR_REST_DEPTH => $depth + 1, |
|
141
|
46 |
|
self::DDR_REST_PATH => $this->appendPath($path, $propertyMetadatum->name) |
|
142
|
|
|
] |
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
} else { |
|
146
|
|
|
|
|
147
|
|
|
/* Inlude if includable is missing OR it is on include path */ |
|
148
|
64 |
|
if (!$propertyMetadatum->isIncludable() || $this->isIncluded( |
|
149
|
20 |
|
$path, |
|
150
|
20 |
|
$propertyMetadatum->getIncludablePaths(), |
|
151
|
64 |
|
$includes |
|
152
|
|
|
) |
|
153
|
|
|
) { |
|
154
|
64 |
|
$value = $this->propertyAccessor->getValue($data, $propertyMetadatum->name); |
|
155
|
64 |
|
if (is_scalar($value) || array_key_exists($propertyMetadatum->getType(), Type::getTypesMap())) { |
|
156
|
64 |
|
$normalizedData[$propertyMetadatum->name] = $this->normalizeField( |
|
157
|
64 |
|
$value, |
|
158
|
64 |
|
$propertyMetadatum |
|
159
|
|
|
); |
|
160
|
|
|
} else { |
|
161
|
28 |
|
$normalizedData[$propertyMetadatum->name] = $this->normalize( |
|
162
|
28 |
|
$value, |
|
163
|
28 |
|
$format, |
|
164
|
|
|
[ |
|
165
|
28 |
|
self::DDR_REST_INCLUDES => $includes, |
|
166
|
28 |
|
self::DDR_REST_DEPTH => $depth + 1, |
|
167
|
64 |
|
self::DDR_REST_PATH => $this->appendPath($path, $propertyMetadatum->name) |
|
168
|
|
|
] |
|
169
|
|
|
); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
64 |
|
return $normalizedData; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
22 |
|
return $data; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* {@inheritdoc} |
|
183
|
|
|
*/ |
|
184
|
64 |
|
public function supportsNormalization($data, $format = null) |
|
185
|
|
|
{ |
|
186
|
64 |
|
if ('json' === $format) { |
|
187
|
64 |
|
return true; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return false; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* {@inheritdoc} |
|
195
|
|
|
*/ |
|
196
|
64 |
|
public function hasCacheableSupportsMethod(): bool |
|
197
|
|
|
{ |
|
198
|
64 |
|
return true; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
52 |
|
private function isIncluded($currentPath, array $paths, ?array $includes): bool |
|
202
|
|
|
{ |
|
203
|
52 |
|
if (null === $includes) { |
|
204
|
|
|
return false; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
52 |
|
foreach ($paths as $path) { |
|
208
|
52 |
|
if (in_array($this->appendPath($currentPath, $path), $includes)) { |
|
209
|
52 |
|
return true; |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
52 |
|
return false; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
52 |
|
private function appendPath($path, $name) |
|
217
|
|
|
{ |
|
218
|
52 |
|
if (null === $path || '' === $path) { |
|
219
|
52 |
|
return $name; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
28 |
|
return $path . '.' . $name; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
64 |
|
private function normalizeField($value, PropertyMetadata $propertyMetadata) |
|
226
|
|
|
{ |
|
227
|
64 |
|
switch ($propertyMetadata->getType()) { |
|
228
|
|
|
|
|
229
|
64 |
|
case Type::DATETIME: |
|
230
|
8 |
|
if (null === $value) { |
|
231
|
2 |
|
return null; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** @var $value \DateTime */ |
|
235
|
6 |
|
return $value->format('Y-m-d H:i:s'); |
|
236
|
|
|
|
|
237
|
64 |
|
case Type::DATE: |
|
238
|
8 |
|
if (null === $value) { |
|
239
|
2 |
|
return null; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** @var $value \DateTime */ |
|
243
|
6 |
|
return $value->format('Y-m-d'); |
|
244
|
|
|
|
|
245
|
64 |
|
case Type::TIME: |
|
246
|
8 |
|
if (null === $value) { |
|
247
|
2 |
|
return null; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** @var $value \DateTime */ |
|
251
|
6 |
|
return $value->format('H:i:s'); |
|
252
|
|
|
|
|
253
|
|
|
default: |
|
254
|
64 |
|
return $value; |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|