1
1
<?php
2
+
2
3
namespace Spatie \ArrayToXml ;
4
+
3
5
use DOMElement ;
4
6
use DOMDocument ;
5
7
use DOMException ;
6
8
use DOMImplementation ;
9
+
7
10
class ArrayToXml
8
11
{
9
12
/**
@@ -18,6 +21,7 @@ class ArrayToXml
18
21
* @var bool
19
22
*/
20
23
protected $ replaceSpacesByUnderScoresInKeyNames = true ;
24
+
21
25
/**
22
26
* Construct a new instance.
23
27
*
@@ -34,10 +38,10 @@ public function __construct(array $array, $rootElement = '', $replaceSpacesByUnd
34
38
{
35
39
$ this ->document = new DOMDocument ($ xmlVersion , $ xmlEncoding );
36
40
$ this ->replaceSpacesByUnderScoresInKeyNames = $ replaceSpacesByUnderScoresInKeyNames ;
37
- if ($ this ->isArrayAllKeySequential ($ array ) && ! empty ($ array )) {
41
+ if ($ this ->isArrayAllKeySequential ($ array ) && !empty ($ array )) {
38
42
throw new DOMException ('Invalid Character Error ' );
39
43
}
40
- if (! empty ($ docTypeArray )) {
44
+ if (!empty ($ docTypeArray )) {
41
45
$ docType = $ this ->createDocType ($ docTypeArray );
42
46
$ this ->document ->appendChild ($ docType );
43
47
}
@@ -64,6 +68,7 @@ public static function convert(array $array, $rootElementName = '', $replaceSpac
64
68
$ converter = new static ($ array , $ rootElementName , $ replaceSpacesByUnderScoresInKeyNames , $ xmlEncoding , $ xmlVersion , $ docTypeArray );
65
69
return $ converter ->toXml ();
66
70
}
71
+
67
72
/**
68
73
* Return as XML.
69
74
*
@@ -73,6 +78,7 @@ public function toXml()
73
78
{
74
79
return $ this ->document ->saveXML ();
75
80
}
81
+
76
82
/**
77
83
* Return as DOM object.
78
84
*
@@ -82,6 +88,7 @@ public function toDom()
82
88
{
83
89
return $ this ->document ;
84
90
}
91
+
85
92
/**
86
93
* Parse individual element.
87
94
*
@@ -91,12 +98,13 @@ public function toDom()
91
98
private function convertElement (DOMElement $ element , $ value )
92
99
{
93
100
$ sequential = $ this ->isArrayAllKeySequential ($ value );
94
- if (! is_array ($ value )) {
101
+ if (!is_array ($ value )) {
95
102
$ element ->nodeValue = htmlspecialchars ($ value );
96
103
return ;
97
104
}
98
105
foreach ($ value as $ key => $ data ) {
99
- if (! $ sequential ) {
106
+ $ key = $ this ->getRealKey ($ key );
107
+ if (!$ sequential ) {
100
108
if (($ key === '_attributes ' ) || ($ key === '@attributes ' )) {
101
109
$ this ->addAttributes ($ element , $ data );
102
110
} elseif ((($ key === '_value ' ) || ($ key === '@value ' )) && is_string ($ data )) {
@@ -113,6 +121,23 @@ private function convertElement(DOMElement $element, $value)
113
121
}
114
122
}
115
123
}
124
+
125
+ /**
126
+ * Get Real key by $.
127
+ *
128
+ * @param string $key
129
+ * @param string $key
130
+ * @return bool|string
131
+ */
132
+ private function getRealKey ($ key )
133
+ {
134
+ $ pos = strpos ($ key , '$ ' );
135
+ if ($ pos > 0 ) {
136
+ $ key = substr ($ key , 0 , $ pos );
137
+ }
138
+ return $ key ;
139
+ }
140
+
116
141
/**
117
142
* Add node.
118
143
*
@@ -129,6 +154,7 @@ protected function addNode(DOMElement $element, $key, $value)
129
154
$ element ->appendChild ($ child );
130
155
$ this ->convertElement ($ child , $ value );
131
156
}
157
+
132
158
/**
133
159
* Add collection node.
134
160
*
@@ -147,6 +173,7 @@ protected function addCollectionNode(DOMElement $element, $value)
147
173
$ element ->parentNode ->appendChild ($ child );
148
174
$ this ->convertElement ($ child , $ value );
149
175
}
176
+
150
177
/**
151
178
* Add sequential node.
152
179
*
@@ -165,6 +192,7 @@ protected function addSequentialNode(DOMElement $element, $value)
165
192
$ child ->nodeValue = htmlspecialchars ($ value );
166
193
$ element ->parentNode ->appendChild ($ child );
167
194
}
195
+
168
196
/**
169
197
* Check if array are all sequential.
170
198
*
@@ -174,14 +202,15 @@ protected function addSequentialNode(DOMElement $element, $value)
174
202
*/
175
203
protected function isArrayAllKeySequential ($ value )
176
204
{
177
- if (! is_array ($ value )) {
205
+ if (!is_array ($ value )) {
178
206
return false ;
179
207
}
180
208
if (count ($ value ) <= 0 ) {
181
209
return true ;
182
210
}
183
211
return array_unique (array_map ('is_int ' , array_keys ($ value ))) === [true ];
184
212
}
213
+
185
214
/**
186
215
* Add attributes.
187
216
*
@@ -194,10 +223,11 @@ protected function addAttributes($element, $data)
194
223
$ element ->setAttribute ($ attrKey , $ attrVal );
195
224
}
196
225
}
226
+
197
227
/**
198
228
* Create the root element.
199
229
*
200
- * @param string|array $rootElement
230
+ * @param string|array $rootElement
201
231
* @return DOMElement
202
232
*/
203
233
protected function createRootElement ($ rootElement )
@@ -216,6 +246,7 @@ protected function createRootElement($rootElement)
216
246
}
217
247
return $ element ;
218
248
}
249
+
219
250
/**
220
251
* Pass in an array of elements to set the doctype of the XML.
221
252
* @param $docTypeArray
0 commit comments