|
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; |
@@ -368,7 +369,7 @@ private function buildXml(\DOMNode $parentNode, $data, string $xmlRootNodeName = |
368 | 369 | $removeEmptyTags = $this->context[self::REMOVE_EMPTY_TAGS] ?? $this->defaultContext[self::REMOVE_EMPTY_TAGS] ?? false; |
369 | 370 | $encoderIgnoredNodeTypes = $this->context[self::ENCODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::ENCODER_IGNORED_NODE_TYPES]; |
370 | 371 |
|
371 | | - if (\is_array($data) || ($data instanceof \Traversable && !$this->serializer->supportsNormalization($data, $this->format))) { |
| 372 | + if (\is_array($data) || ($data instanceof \Traversable && (null === $this->serializer || !$this->serializer->supportsNormalization($data, $this->format)))) { |
372 | 373 | foreach ($data as $key => $data) { |
373 | 374 | //Ah this is the magic @ attribute types. |
374 | 375 | if (0 === strpos($key, '@') && $this->isElementNameValid($attributeName = substr($key, 1))) { |
@@ -407,6 +408,10 @@ private function buildXml(\DOMNode $parentNode, $data, string $xmlRootNodeName = |
407 | 408 | } |
408 | 409 |
|
409 | 410 | if (\is_object($data)) { |
| 411 | + if (null === $this->serializer) { |
| 412 | + throw new BadMethodCallException(sprintf('The serializer needs to be set to allow %s() to be used with object data.', __METHOD__)); |
| 413 | + } |
| 414 | + |
410 | 415 | $data = $this->serializer->normalize($data, $this->format, $this->context); |
411 | 416 | if (null !== $data && !is_scalar($data)) { |
412 | 417 | return $this->buildXml($parentNode, $data, $xmlRootNodeName); |
@@ -469,6 +474,10 @@ private function selectNodeType(\DOMNode $node, $val): bool |
469 | 474 | } elseif ($val instanceof \Traversable) { |
470 | 475 | $this->buildXml($node, $val); |
471 | 476 | } elseif (\is_object($val)) { |
| 477 | + if (null === $this->serializer) { |
| 478 | + throw new BadMethodCallException(sprintf('The serializer needs to be set to allow %s() to be used with object data.', __METHOD__)); |
| 479 | + } |
| 480 | + |
472 | 481 | return $this->selectNodeType($node, $this->serializer->normalize($val, $this->format, $this->context)); |
473 | 482 | } elseif (is_numeric($val)) { |
474 | 483 | return $this->appendText($node, (string) $val); |
|
0 commit comments