1 | <?php |
||
15 | final class Normalizer implements NormalizerInterface |
||
16 | { |
||
17 | /** |
||
18 | * @var NormalizerObjectMappingRegistryInterface |
||
19 | */ |
||
20 | private $normalizerObjectMappingRegistry; |
||
21 | |||
22 | /** |
||
23 | * @var LoggerInterface |
||
24 | */ |
||
25 | private $logger; |
||
26 | |||
27 | /** |
||
28 | * @param NormalizerObjectMappingRegistryInterface $normalizerObjectMappingRegistry |
||
29 | * @param LoggerInterface|null $logger |
||
30 | */ |
||
31 | 5 | public function __construct( |
|
38 | |||
39 | /** |
||
40 | * @param Request $request |
||
41 | * @param object $object |
||
42 | * @param NormalizerContextInterface|null $context |
||
43 | * @param string $path |
||
44 | * |
||
45 | * @return array |
||
46 | */ |
||
47 | 5 | public function normalize( |
|
48 | Request $request, |
||
49 | $object, |
||
50 | NormalizerContextInterface $context = null, |
||
51 | string $path = '' |
||
52 | ): array { |
||
53 | 5 | $this->validateDataType($object, $path); |
|
54 | |||
55 | 4 | $context = $context ?? NormalizerContextBuilder::create()->getContext(); |
|
56 | |||
57 | 4 | $class = is_object($object) ? get_class($object) : $object; |
|
58 | 4 | $objectMapping = $this->getObjectMapping($class); |
|
59 | |||
60 | 3 | $fieldMappings = $objectMapping->getNormalizationFieldMappings($path); |
|
61 | |||
62 | 3 | $data = $this->getFieldsByFieldNormalizationMappings($context, $fieldMappings, $path, $request, $object); |
|
63 | |||
64 | 3 | $embeddedMappings = $objectMapping->getNormalizationEmbeddedFieldMappings($path); |
|
65 | 3 | $embedded = $this->getFieldsByFieldNormalizationMappings($context, $embeddedMappings, $path, $request, $object); |
|
66 | |||
67 | 3 | $linkMappings = $objectMapping->getNormalizationLinkMappings($path); |
|
68 | 3 | $links = $this->getLinksByLinkNormalizationMappings($context, $linkMappings, $path, $request, $object); |
|
69 | |||
70 | 3 | if ([] !== $embedded) { |
|
71 | 2 | $data['_embedded'] = $embedded; |
|
72 | } |
||
73 | |||
74 | 3 | if ([] !== $links) { |
|
75 | 1 | $data['_links'] = $links; |
|
76 | } |
||
77 | |||
78 | 3 | $data['_type'] = $objectMapping->getNormalizationType(); |
|
79 | |||
80 | 3 | return $data; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param object $object |
||
85 | * @param string $path |
||
86 | * |
||
87 | * @throws SerializerLogicException |
||
88 | */ |
||
89 | 5 | private function validateDataType($object, string $path) |
|
99 | |||
100 | /** |
||
101 | * @param string $class |
||
102 | * |
||
103 | * @return NormalizationObjectMappingInterface |
||
104 | * |
||
105 | * @throws SerializerLogicException |
||
106 | */ |
||
107 | 4 | private function getObjectMapping(string $class): NormalizationObjectMappingInterface |
|
117 | |||
118 | /** |
||
119 | * @param NormalizerContextInterface $context |
||
120 | * @param NormalizationFieldMappingInterface[] $normalizationFieldMappings |
||
121 | * @param string $path |
||
122 | * @param Request $request |
||
123 | * @param $object |
||
124 | * |
||
125 | * @return array |
||
126 | */ |
||
127 | 3 | private function getFieldsByFieldNormalizationMappings( |
|
153 | |||
154 | /** |
||
155 | * @param NormalizerContextInterface $context |
||
156 | * @param NormalizationLinkMappingInterface[] $normalizationLinkMappings |
||
157 | * @param string $path |
||
158 | * @param Request $request |
||
159 | * @param object $object |
||
160 | * |
||
161 | * @return array |
||
162 | */ |
||
163 | 3 | private function getLinksByLinkNormalizationMappings( |
|
164 | NormalizerContextInterface $context, |
||
165 | array $normalizationLinkMappings, |
||
166 | string $path, |
||
167 | Request $request, |
||
168 | $object |
||
169 | ): array { |
||
170 | 3 | $links = []; |
|
171 | 3 | foreach ($normalizationLinkMappings as $normalizationLinkMapping) { |
|
172 | 3 | if (!$this->isWithinGroup($context, $normalizationLinkMapping)) { |
|
173 | 1 | continue; |
|
174 | } |
||
175 | |||
176 | 2 | $linkNormalizer = $normalizationLinkMapping->getLinkNormalizer(); |
|
177 | |||
178 | 2 | if (null === $link = $linkNormalizer->normalizeLink($path, $request, $object, $context)) { |
|
179 | 1 | continue; |
|
180 | } |
||
181 | |||
182 | 1 | $links[$normalizationLinkMapping->getName()] = $link; |
|
183 | } |
||
184 | |||
185 | 3 | return $links; |
|
186 | } |
||
187 | |||
188 | /** |
||
189 | * @param NormalizerContextInterface $context |
||
190 | * @param NormalizationFieldMappingInterface|NormalizationLinkMappingInterface $mapping |
||
191 | * |
||
192 | * @return bool |
||
193 | */ |
||
194 | 3 | private function isWithinGroup(NormalizerContextInterface $context, $mapping): bool |
|
208 | |||
209 | /** |
||
210 | * @param string $path |
||
211 | * @param string $name |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | 3 | private function getSubPathByName(string $path, string $name): string |
|
219 | } |
||
220 |