@@ -38,19 +38,22 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
38
38
private $ context ;
39
39
private $ rootNodeName = 'response ' ;
40
40
private $ loadOptions ;
41
- private $ ignoredNodeTypes ;
41
+ private $ decoderIgnoredNodeTypes ;
42
+ private $ encoderIgnoredNodeTypes ;
42
43
43
44
/**
44
45
* Construct new XmlEncoder and allow to change the root node element name.
45
46
*
46
- * @param int|null $loadOptions A bit field of LIBXML_* constants
47
- * @param int[] $ignoredNodeTypes an array of ignored XML node types, each one of the DOM Predefined XML_* Constants
47
+ * @param int|null $loadOptions A bit field of LIBXML_* constants
48
+ * @param int[] $decoderIgnoredNodeTypes an array of ignored XML node types while decoding, each one of the DOM Predefined XML_* Constants
49
+ * @param int[] $encoderIgnoredNodeTypes an array of ignored XML node types while encoding, each one of the DOM Predefined XML_* Constants
48
50
*/
49
- public function __construct (string $ rootNodeName = 'response ' , int $ loadOptions = null , array $ ignoredNodeTypes = array (XML_PI_NODE , XML_COMMENT_NODE ))
51
+ public function __construct (string $ rootNodeName = 'response ' , int $ loadOptions = null , array $ decoderIgnoredNodeTypes = array (XML_PI_NODE , XML_COMMENT_NODE ), array $ encoderIgnoredNodeTypes = array ( ))
50
52
{
51
53
$ this ->rootNodeName = $ rootNodeName ;
52
54
$ this ->loadOptions = null !== $ loadOptions ? $ loadOptions : LIBXML_NONET | LIBXML_NOBLANKS ;
53
- $ this ->ignoredNodeTypes = $ ignoredNodeTypes ;
55
+ $ this ->decoderIgnoredNodeTypes = $ decoderIgnoredNodeTypes ;
56
+ $ this ->encoderIgnoredNodeTypes = $ encoderIgnoredNodeTypes ;
54
57
}
55
58
56
59
/**
@@ -59,7 +62,7 @@ public function __construct(string $rootNodeName = 'response', int $loadOptions
59
62
public function encode ($ data , $ format , array $ context = array ())
60
63
{
61
64
if ($ data instanceof \DOMDocument) {
62
- return $ data ->saveXML ();
65
+ return $ data ->saveXML (in_array ( XML_PI_NODE , $ this -> encoderIgnoredNodeTypes , true ) ? $ data -> documentElement : null );
63
66
}
64
67
65
68
$ xmlRootNodeName = $ this ->resolveXmlRootName ($ context );
@@ -76,7 +79,7 @@ public function encode($data, $format, array $context = array())
76
79
$ this ->appendNode ($ this ->dom , $ data , $ xmlRootNodeName );
77
80
}
78
81
79
- return $ this ->dom ->saveXML ();
82
+ return $ this ->dom ->saveXML (in_array ( XML_PI_NODE , $ this -> encoderIgnoredNodeTypes , true ) ? $ this -> dom -> documentElement : null );
80
83
}
81
84
82
85
/**
@@ -109,7 +112,7 @@ public function decode($data, $format, array $context = array())
109
112
if (XML_DOCUMENT_TYPE_NODE === $ child ->nodeType ) {
110
113
throw new NotEncodableValueException ('Document types are not allowed. ' );
111
114
}
112
- if (!$ rootNode && !\in_array ($ child ->nodeType , $ this ->ignoredNodeTypes , true )) {
115
+ if (!$ rootNode && !\in_array ($ child ->nodeType , $ this ->decoderIgnoredNodeTypes , true )) {
113
116
$ rootNode = $ child ;
114
117
}
115
118
}
@@ -327,7 +330,7 @@ private function parseXmlValue(\DOMNode $node, array $context = array())
327
330
$ value = array ();
328
331
329
332
foreach ($ node ->childNodes as $ subnode ) {
330
- if (\in_array ($ subnode ->nodeType , $ this ->ignoredNodeTypes , true )) {
333
+ if (\in_array ($ subnode ->nodeType , $ this ->decoderIgnoredNodeTypes , true )) {
331
334
continue ;
332
335
}
333
336
0 commit comments