|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\Serializer\Encoder; |
13 | 13 |
|
| 14 | +use Symfony\Component\Serializer\Exception\BadMethodCallException; |
14 | 15 | use Symfony\Component\Serializer\Exception\NotEncodableValueException; |
15 | 16 | use Symfony\Component\Serializer\SerializerAwareInterface; |
16 | 17 | use Symfony\Component\Serializer\SerializerAwareTrait; |
@@ -413,7 +414,7 @@ private function buildXml(\DOMNode $parentNode, $data, string $xmlRootNodeName = |
413 | 414 | $removeEmptyTags = $this->context[self::REMOVE_EMPTY_TAGS] ?? $this->defaultContext[self::REMOVE_EMPTY_TAGS] ?? false; |
414 | 415 | $encoderIgnoredNodeTypes = $this->context[self::ENCODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::ENCODER_IGNORED_NODE_TYPES]; |
415 | 416 |
|
416 | | - if (\is_array($data) || ($data instanceof \Traversable && !$this->serializer->supportsNormalization($data, $this->format))) { |
| 417 | + if (\is_array($data) || ($data instanceof \Traversable && (null === $this->serializer || !$this->serializer->supportsNormalization($data, $this->format)))) { |
417 | 418 | foreach ($data as $key => $data) { |
418 | 419 | //Ah this is the magic @ attribute types. |
419 | 420 | if (0 === strpos($key, '@') && $this->isElementNameValid($attributeName = substr($key, 1))) { |
@@ -452,6 +453,10 @@ private function buildXml(\DOMNode $parentNode, $data, string $xmlRootNodeName = |
452 | 453 | } |
453 | 454 |
|
454 | 455 | if (\is_object($data)) { |
| 456 | + if (null === $this->serializer) { |
| 457 | + throw new BadMethodCallException(sprintf('The serializer needs to be set to allow %s() to be used with object data.', __METHOD__)); |
| 458 | + } |
| 459 | + |
455 | 460 | $data = $this->serializer->normalize($data, $this->format, $this->context); |
456 | 461 | if (null !== $data && !is_scalar($data)) { |
457 | 462 | return $this->buildXml($parentNode, $data, $xmlRootNodeName); |
@@ -514,6 +519,10 @@ private function selectNodeType(\DOMNode $node, $val): bool |
514 | 519 | } elseif ($val instanceof \Traversable) { |
515 | 520 | $this->buildXml($node, $val); |
516 | 521 | } elseif (\is_object($val)) { |
| 522 | + if (null === $this->serializer) { |
| 523 | + throw new BadMethodCallException(sprintf('The serializer needs to be set to allow %s() to be used with object data.', __METHOD__)); |
| 524 | + } |
| 525 | + |
517 | 526 | return $this->selectNodeType($node, $this->serializer->normalize($val, $this->format, $this->context)); |
518 | 527 | } elseif (is_numeric($val)) { |
519 | 528 | return $this->appendText($node, (string) $val); |
|
0 commit comments