@@ -69,6 +69,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
69
69
* @var \DOMDocument
70
70
*/
71
71
private $ dom ;
72
+ private $ domStack = [];
72
73
private $ format ;
73
74
private $ context ;
74
75
@@ -77,6 +78,24 @@ public function __construct(array $defaultContext = [])
77
78
$ this ->defaultContext = array_merge ($ this ->defaultContext , $ defaultContext );
78
79
}
79
80
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
+
80
99
/**
81
100
* {@inheritdoc}
82
101
*/
@@ -90,7 +109,7 @@ public function encode($data, string $format, array $context = [])
90
109
91
110
$ xmlRootNodeName = $ context [self ::ROOT_NODE_NAME ] ?? $ this ->defaultContext [self ::ROOT_NODE_NAME ];
92
111
93
- $ this ->dom = $ this -> createDomDocument ($ context );
112
+ $ this ->pushDom ($ context );
94
113
$ this ->format = $ format ;
95
114
$ this ->context = $ context ;
96
115
@@ -102,7 +121,10 @@ public function encode($data, string $format, array $context = [])
102
121
$ this ->appendNode ($ this ->dom , $ data , $ xmlRootNodeName );
103
122
}
104
123
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 ;
106
128
}
107
129
108
130
/**
0 commit comments