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

Skip to content

Commit 06ac911

Browse files
committed
removed dom property from XmlEncoder
1 parent ef1253b commit 06ac911

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
6868
/**
6969
* @var \DOMDocument
7070
*/
71-
private $dom;
7271
private $format;
7372
private $context;
7473

@@ -90,19 +89,19 @@ public function encode($data, string $format, array $context = [])
9089

9190
$xmlRootNodeName = $context[self::ROOT_NODE_NAME] ?? $this->defaultContext[self::ROOT_NODE_NAME];
9291

93-
$this->dom = $this->createDomDocument($context);
92+
$dom = $this->createDomDocument($context);
9493
$this->format = $format;
9594
$this->context = $context;
9695

9796
if (null !== $data && !is_scalar($data)) {
98-
$root = $this->dom->createElement($xmlRootNodeName);
99-
$this->dom->appendChild($root);
97+
$root = $dom->createElement($xmlRootNodeName);
98+
$dom->appendChild($root);
10099
$this->buildXml($root, $data, $xmlRootNodeName);
101100
} else {
102-
$this->appendNode($this->dom, $data, $xmlRootNodeName);
101+
$this->appendNode($dom, $data, $xmlRootNodeName);
103102
}
104103

105-
return $this->dom->saveXML($ignorePiNode ? $this->dom->documentElement : null);
104+
return $dom->saveXML($ignorePiNode ? $dom->documentElement : null);
106105
}
107106

108107
/**
@@ -197,7 +196,7 @@ public function supportsDecoding(string $format)
197196
final protected function appendXMLString(\DOMNode $node, string $val): bool
198197
{
199198
if ('' !== $val) {
200-
$frag = $this->dom->createDocumentFragment();
199+
$frag = $node->ownerDocument->createDocumentFragment();
201200
$frag->appendXML($val);
202201
$node->appendChild($frag);
203202

@@ -209,15 +208,15 @@ final protected function appendXMLString(\DOMNode $node, string $val): bool
209208

210209
final protected function appendText(\DOMNode $node, string $val): bool
211210
{
212-
$nodeText = $this->dom->createTextNode($val);
211+
$nodeText = $node->ownerDocument->createTextNode($val);
213212
$node->appendChild($nodeText);
214213

215214
return true;
216215
}
217216

218217
final protected function appendCData(\DOMNode $node, string $val): bool
219218
{
220-
$nodeText = $this->dom->createCDATASection($val);
219+
$nodeText = $node->ownerDocument->createCDATASection($val);
221220
$node->appendChild($nodeText);
222221

223222
return true;
@@ -239,7 +238,7 @@ final protected function appendDocumentFragment(\DOMNode $node, $fragment): bool
239238

240239
final protected function appendComment(\DOMNode $node, string $data): bool
241240
{
242-
$node->appendChild($this->dom->createComment($data));
241+
$node->appendChild($node->ownerDocument->createComment($data));
243242

244243
return true;
245244
}
@@ -442,7 +441,15 @@ private function buildXml(\DOMNode $parentNode, $data, string $xmlRootNodeName =
442441
*/
443442
private function appendNode(\DOMNode $parentNode, $data, string $nodeName, string $key = null): bool
444443
{
445-
$node = $this->dom->createElement($nodeName);
444+
$dom = null;
445+
if ($parentNode instanceof \DomDocument) {
446+
$dom = $parentNode;
447+
}
448+
else {
449+
$dom = $parentNode->ownerDocument;
450+
}
451+
452+
$node = $dom->createElement($nodeName);
446453
if (null !== $key) {
447454
$node->setAttribute('key', $key);
448455
}
@@ -473,12 +480,12 @@ private function selectNodeType(\DOMNode $node, $val): bool
473480
if (\is_array($val)) {
474481
return $this->buildXml($node, $val);
475482
} elseif ($val instanceof \SimpleXMLElement) {
476-
$child = $this->dom->importNode(dom_import_simplexml($val), true);
483+
$child = $node->ownerDocument->importNode(dom_import_simplexml($val), true);
477484
$node->appendChild($child);
478485
} elseif ($val instanceof \Traversable) {
479486
$this->buildXml($node, $val);
480487
} elseif ($val instanceof \DOMNode) {
481-
$child = $this->dom->importNode($val, true);
488+
$child = $node->ownerDocument->importNode($val, true);
482489
$node->appendChild($child);
483490
} elseif (\is_object($val)) {
484491
if (null === $this->serializer) {

0 commit comments

Comments
 (0)
0