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