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 | 8 | public function __construct( |
|
38 | |||
39 | /** |
||
40 | * @param object $object |
||
41 | * @param NormalizerContextInterface|null $context |
||
42 | * @param string $path |
||
43 | * |
||
44 | * @return array |
||
45 | */ |
||
46 | 8 | public function normalize( |
|
82 | |||
83 | /** |
||
84 | * @param object $object |
||
85 | * @param string $path |
||
86 | * |
||
87 | * @throws SerializerLogicException |
||
88 | */ |
||
89 | 8 | private function validateDataType($object, string $path) |
|
99 | |||
100 | /** |
||
101 | * @param string $class |
||
102 | * |
||
103 | * @return NormalizationObjectMappingInterface |
||
104 | * |
||
105 | * @throws SerializerLogicException |
||
106 | */ |
||
107 | 7 | private function getObjectMapping(string $class): NormalizationObjectMappingInterface |
|
117 | |||
118 | /** |
||
119 | * @param NormalizerContextInterface $context |
||
120 | * @param NormalizationFieldMappingInterface[] $normalizationFieldMappings |
||
121 | * @param string $path |
||
122 | * @param object $object |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | 6 | private function getFieldsByFieldNormalizationMappings( |
|
127 | NormalizerContextInterface $context, |
||
128 | array $normalizationFieldMappings, |
||
129 | string $path, |
||
130 | $object |
||
131 | ): array { |
||
132 | 6 | $data = []; |
|
133 | 6 | foreach ($normalizationFieldMappings as $normalizationFieldMapping) { |
|
134 | 5 | if (!$this->isCompliant($context, $normalizationFieldMapping, $object)) { |
|
135 | 1 | continue; |
|
136 | } |
||
137 | |||
138 | 4 | if (!$this->isWithinGroup($context, $normalizationFieldMapping)) { |
|
139 | 1 | continue; |
|
140 | } |
||
141 | |||
142 | 3 | $fieldNormalizer = $normalizationFieldMapping->getFieldNormalizer(); |
|
143 | |||
144 | 3 | $name = $normalizationFieldMapping->getName(); |
|
145 | |||
146 | 3 | $subPath = $this->getSubPathByName($path, $name); |
|
147 | |||
148 | 3 | $this->logger->info('serialize: path {path}', ['path' => $subPath]); |
|
149 | |||
150 | 3 | $data[$name] = $fieldNormalizer->normalizeField($subPath, $object, $context, $this); |
|
151 | } |
||
152 | |||
153 | 6 | return $data; |
|
154 | } |
||
155 | |||
156 | /** |
||
157 | * @param NormalizerContextInterface $context |
||
158 | * @param NormalizationLinkMappingInterface[] $normalizationLinkMappings |
||
159 | * @param string $path |
||
160 | * @param object $object |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | 6 | private function getLinksByLinkNormalizationMappings( |
|
165 | NormalizerContextInterface $context, |
||
166 | array $normalizationLinkMappings, |
||
167 | string $path, |
||
168 | $object |
||
169 | ): array { |
||
170 | 6 | $links = []; |
|
171 | 6 | foreach ($normalizationLinkMappings as $normalizationLinkMapping) { |
|
172 | 6 | if (!$this->isCompliant($context, $normalizationLinkMapping, $object)) { |
|
173 | 1 | continue; |
|
174 | } |
||
175 | |||
176 | 5 | if (!$this->isWithinGroup($context, $normalizationLinkMapping)) { |
|
177 | 1 | continue; |
|
178 | } |
||
179 | |||
180 | 4 | $linkNormalizer = $normalizationLinkMapping->getLinkNormalizer(); |
|
181 | |||
182 | 4 | if (null === $link = $linkNormalizer->normalizeLink($path, $object, $context)) { |
|
183 | 1 | continue; |
|
184 | } |
||
185 | |||
186 | 3 | $links[$normalizationLinkMapping->getName()] = $link; |
|
187 | } |
||
188 | |||
189 | 6 | return $links; |
|
190 | } |
||
191 | |||
192 | /** |
||
193 | * @param NormalizerContextInterface $context |
||
194 | * @param NormalizationFieldMappingInterface|NormalizationLinkMappingInterface $mapping |
||
195 | * @param object $object |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | 6 | private function isCompliant(NormalizerContextInterface $context, $mapping, $object): bool |
|
200 | { |
||
201 | 6 | if (!is_callable([$mapping, 'getPolicy'])) { |
|
202 | 4 | return true; |
|
203 | } |
||
204 | |||
205 | 2 | return $mapping->getPolicy()->isCompliant($context, $object); |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * @param NormalizerContextInterface $context |
||
210 | * @param NormalizationFieldMappingInterface|NormalizationLinkMappingInterface $mapping |
||
211 | * |
||
212 | * @return bool |
||
213 | */ |
||
214 | 5 | private function isWithinGroup(NormalizerContextInterface $context, $mapping): bool |
|
237 | |||
238 | /** |
||
239 | * @param string $path |
||
240 | * @param string $name |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | 3 | private function getSubPathByName(string $path, string $name): string |
|
248 | } |
||
249 |
This method has been deprecated.