8000 [Serializer] removed dom property from XmlEncoder · symfony/symfony@85a02f5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 85a02f5

Browse files
connorhunicolas-grekas
authored andcommitted
[Serializer] removed dom property from XmlEncoder
1 parent 9b2dcac commit 85a02f5

File tree

6 files changed

+169
-15
lines changed

6 files changed

+169
-15
lines changed

src/Symfony/Component/Serializer/Encoder/XmlEncoder.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
2626
{
2727
const FORMAT = 'xml';
2828

29-
/**
30-
* @var \DOMDocument
31-
*/
32-
private $dom;
3329
private $format;
3430
private $context;
3531
private $rootNodeName = 'response';
@@ -58,19 +54,19 @@ public function encode($data, $format, array $context = [])
5854

5955
$xmlRootNodeName = $this->resolveXmlRootName($context);
6056

61-
$this->dom = $this->createDomDocument($context);
57+
$dom = $this->createDomDocument($context);
6258
$this->format = $format;
6359
$this->context = $context;
6460

6561
if (null !== $data && !is_scalar($data)) {
66-
$root = $this->dom->createElement($xmlRootNodeName);
67-
$this->dom->appendChild($root);
62+
$root = $dom->createElement($xmlRootNodeName);
63+
$dom->appendChild($root);
6864
$this->buildXml($root, $data, $xmlRootNodeName);
6965
} else {
70-
$this->appendNode($this->dom, $data, $xmlRootNodeName);
66+
$this->appendNode($dom, $data, $xmlRootNodeName);
7167
}
7268

73-
return $this->dom->saveXML();
69+
return $dom->saveXML();
7470
}
7571

7672
/**
@@ -189,7 +185,7 @@ public function getRootNodeName()
189185
final protected function appendXMLString(\DOMNode $node, $val)
190186
{
191187
if (\strlen($val) > 0) {
192-
$frag = $this->dom->createDocumentFragment();
188+
$frag = $node->ownerDocument->createDocumentFragment();
193189
$frag->appendXML($val);
194190
$node->appendChild($frag);
195191

@@ -206,7 +202,7 @@ final protected function appendXMLString(\DOMNode $node, $val)
206202
*/
207203
final protected function appendText(\DOMNode $node, $val)
208204
{
209-
$nodeText = $this->dom->createTextNode($val);
205+
$nodeText = $node->ownerDocument->createTextNode($val);
210206
$node->appendChild($nodeText);
211207

212208
return true;
@@ -219,7 +215,7 @@ final protected function appendText(\DOMNode $node, $val)
219215
*/
220216
final protected function appendCData(\DOMNode $node, $val)
221217
{
222-
$nodeText = $this->dom->createCDATASection($val);
218+
$nodeText = $node->ownerDocument->createCDATASection($val);
223219
$node->appendChild($nodeText);
224220

225221
return true;
@@ -449,7 +445,14 @@ private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null)
449445
*/
450446
private function appendNode(\DOMNode $parentNode, $data, $nodeName, $key = null)
451447
{
452-
$node = $this->dom->createElement($nodeName);
448+
$dom = null;
449+
if ($parentNode instanceof \DomDocument) {
450+
$dom = $parentNode;
451+
} else {
452+
$dom = $parentNode->ownerDocument;
453+
}
454+
455+
$node = $dom->createElement($nodeName);
453456
if (null !== $key) {
454457
$node->setAttribute('key', $key);
455458
}
@@ -488,12 +491,12 @@ private function selectNodeType(\DOMNode $node, $val)
488491
if (\is_array($val)) {
489492
return $this->buildXml($node, $val);
490493
} elseif ($val instanceof \SimpleXMLElement) {
491-
$child = $this->dom->importNode(dom_import_simplexml($val), true);
494+
$child = $node->ownerDocument->importNode(dom_import_simplexml($val), true);
492495
$node->appendChild($child);
493496
} elseif ($val instanceof \Traversable) {
494497
$this->buildXml($node, $val);
495498
} elseif ($val instanceof \DOMNode) {
496-
$child = $this->dom->importNode($val, true);
499+
$child = $node->ownerDocument->importNode($val, true);
497500
$node->appendChild($child);
498501
} elseif (\is_object($val)) {
499502
if (null === $this->serializer) {

src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2020
use Symfony\Component\Serializer\Serializer;
2121
use Symfony\Component\Serializer\Tests\Fixtures\Dummy;
22+
use Symfony\Component\Serializer\Tests\Fixtures\EnvelopedMessage;
23+
use Symfony\Component\Serializer\Tests\Fixtures\EnvelopedMessageNormalizer;
24+
use Symfony\Component\Serializer\Tests\Fixtures\EnvelopeNormalizer;
25+
use Symfony\Component\Serializer\Tests\Fixtures\EnvelopeObject;
2226
use Symfony\Component\Serializer\Tests\Fixtures\NormalizableTraversableDummy;
2327
use Symfony\Component\Serializer\Tests\Fixtures\ScalarDummy;
2428

@@ -702,6 +706,38 @@ public function testNotEncodableValueExceptionMessageForAResource()
702706
(new XmlEncoder())->encode(tmpfile(), 'xml');
703707
}
704708

709+
public function testReentrantXmlEncoder()
710+
{
711+
$envelope = new EnvelopeObject();
712+
$message = new EnvelopedMessage();
713+
$message->text = 'Symfony is great';
714+
$envelope->message = $message;
715+
716+
$encoder = $this->createXmlEncoderWithEnvelopeNormalizer();
717+
$expected = <<<'XML'
718+
<?xml version="1.0"?>
719+
<response><message>PD94bWwgdmVyc2lvbj0iMS4wIj8+CjxyZXNwb25zZT48dGV4dD5TeW1mb255IGlzIGdyZWF0PC90ZXh0PjwvcmVzcG9uc2U+Cg==</message></response>
720+
721+
XML;
722+
723+
$this->assertSame($expected, $encoder->encode($envelope, 'xml'));
724+
}
725+
726+
private function createXmlEncoderWithEnvelopeNormalizer(): XmlEncoder
727+
{
728+
$normalizers = [
729+
$envelopeNormalizer = new EnvelopeNormalizer(),
730+
new EnvelopedMessageNormalizer(),
731+
];
732+
733+
$encoder = new XmlEncoder();
734+
$serializer = new Serializer($normalizers, ['xml' => $encoder]);
735+
$encoder->setSerializer($serializer);
736+
$envelopeNormalizer->setSerializer($serializer);
737+
738+
return $encoder;
739+
}
740+
705741
/**
706742
* @return XmlEncoder
707743
*/
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
15+
16+
/**
17+
* @author Karoly Gossler <connor@connor.hu>
18+
*/
19+
class EnvelopeNormalizer implements NormalizerInterface
20+
{
21+
private $serializer;
22+
23+
public function normalize($envelope, string $format = null, array $context = []): array
24+
{
25+
$xmlContent = $this->serializer->serialize($envelope->message, 'xml');
26+
27+
$encodedContent = base64_encode($xmlContent);
28+
29+
return [
30+
'message' => $encodedContent,
31+
];
32+
}
33+
34+
public function supportsNormalization($data, string $format = null, array $context = []): bool
35+
{
36+
return $data instanceof EnvelopeObject;
37+
}
38+
39+
public function setSerializer($serializer): void
40+
{
41+
$this->serializer = $serializer;
42+
}
43+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
/**
15+
* @author Karoly Gossler <connor@connor.hu>
16+
*/
17+
class EnvelopeObject
18+
{
19+
public $message;
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
/**
15+
* @author Karoly Gossler <connor@connor.hu>
16+
*/
17+
class EnvelopedMessage
18+
{
19+
public $text;
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
15+
16+
/**
17+
* @author Karoly Gossler <connor@connor.hu>
18+
*/
19+
class EnvelopedMessageNormalizer implements NormalizerInterface
20+
{
21+
public function normalize($message, string $format = null, array $context = []): array
22+
{
23+
return [
24+
'text' => $message->text,
25+
];
26+
}
27+
28+
public function supportsNormalization($data, string $format = null, array $context = []): bool
29+
{
30+
return $data instanceof EnvelopedMessage;
31+
}
32+
}

0 commit comments

Comments
 (0)
0