|
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 |
|
@@ -303,42 +305,35 @@ private function updateData(array $data, $attribute, $attributeValue)
|
303 | 305 | /**
|
304 | 306 | * Is the max depth reached for the given attribute?
|
305 | 307 | *
|
306 |
| - * @param string $class |
307 |
| - * @param string $attribute |
308 |
| - * @param array $context |
| 308 | + * @param AttributeMetadataInterface[] $attributesMetadata |
| 309 | + * @param string $class |
| 310 | + * @param string $attribute |
| 311 | + * @param array $context |
309 | 312 | *
|
310 | 313 | * @return bool
|
311 | 314 | */
|
312 |
| - private function isMaxDepthReached($class, $attribute, array &$context) |
| 315 | + private function isMaxDepthReached(array $attributesMetadata, $class, $attribute, array &$context) |
313 | 316 | {
|
314 |
| - if (!$this->classMetadataFactory || !isset($context[static::ENABLE_MAX_DEPTH])) { |
| 317 | + if ( |
| 318 | + !isset($context[static::ENABLE_MAX_DEPTH]) || |
| 319 | + !isset($attributesMetadata[$attribute]) || |
| 320 | + null === $maxDepth = $attributesMetadata[$attribute]->getMaxDepth() |
| 321 | + ) { |
315 | 322 | return false;
|
316 | 323 | }
|
317 | 324 |
|
318 |
| - $classMetadata = $this->classMetadataFactory->getMetadataFor($class); |
319 |
| - $attributesMetadata = $classMetadata->getAttributesMetadata(); |
320 |
| - |
321 |
| - if (!isset($attributesMetadata[$attribute])) { |
322 |
| - return false; |
323 |
| - } |
| 325 | + $key = sprintf(static::DEPTH_KEY_PATTERN, $class, $attribute); |
| 326 | + if (!isset($context[$key])) { |
| 327 | + $context[$key] = 1; |
324 | 328 |
|
325 |
| - $maxDepth = $attributesMetadata[$attribute]->getMaxDepth(); |
326 |
| - if (null === $maxDepth) { |
327 | 329 | return false;
|
328 | 330 | }
|
329 | 331 |
|
330 |
| - $key = sprintf(static::DEPTH_KEY_PATTERN, $class, $attribute); |
331 |
| - $keyExist = isset($context[$key]); |
332 |
| - |
333 |
| - if ($keyExist && $context[$key] === $maxDepth) { |
| 332 | + if ($context[$key] === $maxDepth) { |
334 | 333 | return true;
|
335 | 334 | }
|
336 | 335 |
|
337 |
| - if ($keyExist) { |
338 |
| - ++$context[$key]; |
339 |
| - } else { |
340 |
| - $context[$key] = 1; |
341 |
| - } |
| 336 | + ++$context[$key]; |
342 | 337 |
|
343 | 338 | return false;
|
344 | 339 | }
|
|
0 commit comments