|
17 | 17 | use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
18 | 18 | use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
|
19 | 19 | use Symfony\Component\PropertyInfo\Type;
|
| 20 | +use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface; |
20 | 21 | use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
21 | 22 | use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
|
22 | 23 |
|
@@ -67,9 +68,10 @@ public function normalize($object, $format = null, array $context = array())
|
67 | 68 | $stack = array();
|
68 | 69 | $attributes = $this->getAttributes($object, $format, $context);
|
69 | 70 | $class = get_class($object);
|
| 71 | + $attributesMetadata = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata() : null; |
70 | 72 |
|
71 | 73 | foreach ($attributes as $attribute) {
|
72 |
| - if ($this->isMaxDepthReached($class, $attribute, $context)) { |
| 74 | + if (null !== $attributesMetadata && $this->isMaxDepthReached($attributesMetadata, $class, $attribute, $context)) { |
73 | 75 | continue;
|
74 | 76 | }
|
75 | 77 |
|
@@ -289,42 +291,35 @@ private function updateData(array $data, $attribute, $attributeValue)
|
289 | 291 | /**
|
290 | 292 | * Is the max depth reached for the given attribute?
|
291 | 293 | *
|
292 |
| - * @param string $class |
293 |
| - * @param string $attribute |
294 |
| - * @param array $context |
| 294 | + * @param AttributeMetadataInterface[] $attributesMetadata |
| 295 | + * @param string $class |
| 296 | + * @param string $attribute |
| 297 | + * @param array $context |
295 | 298 | *
|
296 | 299 | * @return bool
|
297 | 300 | */
|
298 |
| - private function isMaxDepthReached($class, $attribute, array &$context) |
| 301 | + private function isMaxDepthReached(array $attributesMetadata, $class, $attribute, array &$context) |
299 | 302 | {
|
300 |
| - if (!$this->classMetadataFactory || !isset($context[static::ENABLE_MAX_DEPTH])) { |
| 303 | + if ( |
| 304 | + !isset($context[static::ENABLE_MAX_DEPTH]) || |
| 305 | + !isset($attributesMetadata[$attribute]) || |
| 306 | + null === $maxDepth = $attributesMetadata[$attribute]->getMaxDepth() |
| 307 | + ) { |
301 | 308 | return false;
|
302 | 309 | }
|
303 | 310 |
|
304 |
| - $classMetadata = $this->classMetadataFactory->getMetadataFor($class); |
305 |
| - $attributesMetadata = $classMetadata->getAttributesMetadata(); |
306 |
| - |
307 |
| - if (!isset($attributesMetadata[$attribute])) { |
308 |
| - return false; |
309 |
| - } |
| 311 | + $key = sprintf(static::DEPTH_KEY_PATTERN, $class, $attribute); |
| 312 | + if (!isset($context[$key])) { |
| 313 | + $context[$key] = 1; |
310 | 314 |
|
311 |
| - $maxDepth = $attributesMetadata[$attribute]->getMaxDepth(); |
312 |
| - if (null === $maxDepth) { |
313 | 315 | return false;
|
314 | 316 | }
|
315 | 317 |
|
316 |
| - $key = sprintf(static::DEPTH_KEY_PATTERN, $class, $attribute); |
317 |
| - $keyExist = isset($context[$key]); |
318 |
| - |
319 |
| - if ($keyExist && $context[$key] === $maxDepth) { |
| 318 | + if ($context[$key] === $maxDepth) { |
320 | 319 | return true;
|
321 | 320 | }
|
322 | 321 |
|
323 |
| - if ($keyExist) { |
324 |
| - ++$context[$key]; |
325 |
| - } else { |
326 |
| - $context[$key] = 1; |
327 |
| - } |
| 322 | + ++$context[$key]; |
328 | 323 |
|
329 | 324 | return false;
|
330 | 325 | }
|
|
0 commit comments