|
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 |
|
16 | 17 | /**
|
@@ -375,7 +376,7 @@ private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null)
|
375 | 376 | {
|
376 | 377 | $append = true;
|
377 | 378 |
|
378 |
| - if (\is_array($data) || ($data instanceof \Traversable && !$this->serializer->supportsNormalization($data, $this->format))) { |
| 379 | + if (\is_array($data) || ($data instanceof \Traversable && (null === $this->serializer || !$this->serializer->supportsNormalization($data, $this->format)))) { |
379 | 380 | foreach ($data as $key => $data) {
|
380 | 381 | //Ah this is the magic @ attribute types.
|
381 | 382 | if (0 === strpos($key, '@') && $this->isElementNameValid($attributeName = substr($key, 1))) {
|
@@ -410,6 +411,10 @@ private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null)
|
410 | 411 | }
|
411 | 412 |
|
412 | 413 | if (\is_object($data)) {
|
| 414 | + if (null === $this->serializer) { |
| 415 | + throw new BadMethodCallException(sprintf('The serializer needs to be set to allow %s() to be used with object data.', __METHOD__)); |
| 416 | + } |
| 417 | + |
413 | 418 | $data = $this->serializer->normalize($data, $this->format, $this->context);
|
414 | 419 | if (null !== $data && !is_scalar($data)) {
|
415 | 420 | return $this->buildXml($parentNode, $data, $xmlRootNodeName);
|
@@ -484,6 +489,10 @@ private function selectNodeType(\DOMNode $node, $val)
|
484 | 489 | } elseif ($val instanceof \Traversable) {
|
485 | 490 | $this->buildXml($node, $val);
|
486 | 491 | } elseif (\is_object($val)) {
|
| 492 | + if (null === $this->serializer) { |
| 493 | + throw new BadMethodCallException(sprintf('The serializer needs to be set to allow %s() to be used with object data.', __METHOD__)); |
| 494 | + } |
| 495 | + |
487 | 496 | return $this->selectNodeType($node, $this->serializer->normalize($val, $this->format, $this->context));
|
488 | 497 | } elseif (is_numeric($val)) {
|
489 | 498 | return $this->appendText($node, (string) $val);
|
|
0 commit comments