10000 changed dom property to pop/pushable · symfony/symfony@39615f4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 39615f4

Browse files
committed
changed dom property to pop/pushable
1 parent ef1253b commit 39615f4

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
6969
* @var \DOMDocument
7070
*/
7171
private $dom;
72+
private $domStack = [];
7273
private $format;
7374
private $context;
7475

@@ -77,6 +78,24 @@ public function __construct(array $defaultContext = [])
7778
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
7879
}
7980

81+
private function pushDom(array $context = []): void
82+
{
83+
if (null !== $this->dom) {
84+
$this->domStack[] = $this->dom;
85+
}
86+
87+
$this->dom = $this->createDomDocument($context);
88+
}
89+
90+
private function popDom(): void
91+
{
92+
if (0 === \count($this->domStack)) {
93+
return;
94+
}
95+
96+
$this->dom = array_pop($this->domStack);
97+
}
98+
8099
/**
81100
* {@inheritdoc}
82101
*/
@@ -90,7 +109,7 @@ public function encode($data, string $format, array $context = [])
90109

91110
$xmlRootNodeName = $context[self::ROOT_NODE_NAME] ?? $this->defaultContext[self::ROOT_NODE_NAME];
92111

93-
$this->dom = $this->createDomDocument($context);
112+
$this->pushDom($context);
94113
$this->format = $format;
95114
$this->context = $context;
96115

@@ -102,7 +121,10 @@ public function encode($data, string $format, array $context = [])
102121
$this->appendNode($this->dom, $data, $xmlRootNodeName);
103122
}
104123

105-
return $this->dom->saveXML($ignorePiNode ? $this->dom->documentElement : null);
124+
$encodedString = $this->dom->saveXML($ignorePiNode ? $this->dom->documentElement : null);
125+
$this->popDom();
126+
127+
return $encodedString;
106128
}
107129

108130
/**

0 commit comments

Comments
 (0)
0