2
2
/**
3
3
* OpenLSS - Lighter Smarter Simpler
4
4
*
5
- * This file is part of OpenLSS.
5
+ * This file is part of OpenLSS.
6
6
*
7
- * OpenLSS is free software: you can redistribute it and/or modify
8
- * it under the terms of the GNU Lesser General Public License as
9
- * published by the Free Software Foundation, either version 3 of
10
- * the License, or (at your option) any later version.
7
+ * OpenLSS is free software: you can redistribute it and/or modify
8
+ * it under the terms of the GNU Lesser General Public License as
9
+ * published by the Free Software Foundation, either version 3 of
10
+ * the License, or (at your option) any later version.
11
11
*
12
- * OpenLSS is distributed in the hope that it will be useful,
13
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- * GNU Lesser General Public License for more details.
12
+ * OpenLSS is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ * GNU Lesser General Public License for more details.
16
16
*
17
- * You should have received a copy of the
18
- * GNU Lesser General Public License along with OpenLSS.
19
- * If not, see <http://www.gnu.org/licenses/>.
20
- */
17
+ * You should have received a copy of the
18
+ * GNU Lesser General Public License along with OpenLSS.
19
+ * If not, see <http://www.gnu.org/licenses/>.
20
+ */
21
21
namespace LSS ;
22
+
22
23
use \DomDocument ;
23
24
use \Exception ;
24
25
48
49
* - Reverted to version 0.5
49
50
* Version: 0.8 (02 May 2012)
50
51
* - Removed htmlspecialchars() before adding to text node or attributes.
52
+ * Version: 0.11 (28 October 2015)
53
+ * - Fixed typos; Added support for plain insertion of XML trough @xml.
51
54
*
52
55
* Usage:
53
56
* $xml = Array2XML::createXML('root_node_name', $php_array);
54
57
* echo $xml->saveXML();
55
58
*/
56
-
57
59
class Array2XML {
58
60
61
+ /**
62
+ * @var DOMDocument
63
+ */
59
64
private static $ xml = null ;
60
- private static $ encoding = 'UTF-8 ' ;
65
+ private static $ encoding = 'UTF-8 ' ;
61
66
62
67
/**
63
68
* Initialize the root XML node [optional]
@@ -68,7 +73,7 @@ class Array2XML {
68
73
public static function init ($ version = '1.0 ' , $ encoding = 'UTF-8 ' , $ format_output = true ) {
69
74
self ::$ xml = new DomDocument ($ version , $ encoding );
70
75
self ::$ xml ->formatOutput = $ format_output ;
71
- self ::$ encoding = $ encoding ;
76
+ self ::$ encoding = $ encoding ;
72
77
}
73
78
74
79
/**
@@ -77,7 +82,7 @@ public static function init($version = '1.0', $encoding = 'UTF-8', $format_outpu
77
82
* @param array $arr - aray to be converterd
78
83
* @return DomDocument
79
84
*/
80
- public static function &createXML ($ node_name , $ arr= array ()) {
85
+ public static function &createXML ($ node_name , $ arr = array ()) {
81
86
$ xml = self ::getXMLRoot ();
82
87
$ xml ->appendChild (self ::convert ($ node_name , $ arr ));
83
88
@@ -86,23 +91,29 @@ public static function &createXML($node_name, $arr=array()) {
86
91
}
87
92
88
93
/**
89
- * Convert an Array to XML
90
- * @param string $node_name - name of the root node to be converted
91
- * @param array $arr - aray to be converterd
92
- * @return DOMNode
94
+ * Convert an Array to XML.
95
+ *
96
+ * @param string $node_name
97
+ * Name of the root node to be converted.
98
+ * @param array $arr
99
+ * Array to be converted.
100
+ *
101
+ * @throws \Exception
102
+ *
103
+ * @return \DOMNode
93
104
*/
94
- private static function &convert ($ node_name , $ arr= array ()) {
105
+ private static function &convert ($ node_name , $ arr = array ()) {
95
106
96
107
//print_arr($node_name);
97
108
$ xml = self ::getXMLRoot ();
98
109
$ node = $ xml ->createElement ($ node_name );
99
110
100
- if (is_array ($ arr )){
111
+ if (is_array ($ arr )) {
101
112
// get the attributes first.;
102
- if (isset ($ arr ['@attributes ' ])) {
103
- foreach ($ arr ['@attributes ' ] as $ key => $ value ) {
104
- if (!self ::isValidTagName ($ key )) {
105
- throw new Exception ('[Array2XML] Illegal character in attribute name. attribute: ' . $ key. ' in node: ' . $ node_name );
113
+ if (isset ($ arr ['@attributes ' ])) {
114
+ foreach ($ arr ['@attributes ' ] as $ key => $ value ) {
115
+ if (!self ::isValidTagName ($ key )) {
116
+ throw new Exception ('[Array2XML] Illegal character in attribute name. attribute: ' . $ key . ' in node: ' . $ node_name );
106
117
}
107
118
$ node ->setAttribute ($ key , self ::bool2str ($ value ));
108
119
}
@@ -111,31 +122,38 @@ private static function &convert($node_name, $arr=array()) {
111
122
112
123
// check if it has a value stored in @value, if yes store the value and return
113
124
// else check if its directly stored as string
114
- if (isset ($ arr ['@value ' ])) {
125
+ if (isset ($ arr ['@value ' ])) {
115
126
$ node ->appendChild ($ xml ->createTextNode (self ::bool2str ($ arr ['@value ' ])));
116
127
unset($ arr ['@value ' ]); //remove the key from the array once done.
117
128
//return from recursion, as a note with value cannot have child nodes.
118
129
return $ node ;
119
- } else if (isset ($ arr ['@cdata ' ])) {
130
+ } else if (isset ($ arr ['@cdata ' ])) {
120
131
$ node ->appendChild ($ xml ->createCDATASection (self ::bool2str ($ arr ['@cdata ' ])));
121
132
unset($ arr ['@cdata ' ]); //remove the key from the array once done.
122
133
//return from recursion, as a note with cdata cannot have child nodes.
123
134
return $ node ;
124
135
}
136
+ else if (isset ($ arr ['@xml ' ])) {
137
+ $ fragment = $ xml ->createDocumentFragment ();
138
+ $ fragment ->appendXML ($ arr ['@xml ' ]);
139
+ $ node ->appendChild ($ fragment );
140
+ unset($ arr ['@xml ' ]);
141
+ return $ node ;
142
+ }
125
143
}
126
144
127
145
//create subnodes using recursion
128
- if (is_array ($ arr )){
146
+ if (is_array ($ arr )) {
129
147
// recurse to get the node for that key
130
- foreach ($ arr as $ key=> $ value ){
131
- if (!self ::isValidTagName ($ key )) {
132
- throw new Exception ('[Array2XML] Illegal character in tag name. tag: ' . $ key. ' in node: ' . $ node_name );
148
+ foreach ($ arr as $ key => $ value ) {
149
+ if (!self ::isValidTagName ($ key )) {
150
+ throw new Exception ('[Array2XML] Illegal character in tag name. tag: ' . $ key . ' in node: ' . $ node_name );
133
151
}
134
- if (is_array ($ value ) && is_numeric (key ($ value ))) {
152
+ if (is_array ($ value ) && is_numeric (key ($ value ))) {
135
153
// MORE THAN ONE NODE OF ITS KIND;
136
154
// if the new array is numeric index, means it is array of nodes of the same kind
137
155
// it should follow the parent key name
138
- foreach ($ value as $ k=> $ v ){
156
+ foreach ($ value as $ k => $ v ) {
139
157
$ node ->appendChild (self ::convert ($ key , $ v ));
140
158
}
141
159
} else {
@@ -148,7 +166,7 @@ private static function &convert($node_name, $arr=array()) {
148
166
149
167
// after we are done with all the keys in the array (if it is one)
150
168
// we check if it has any text value, if yes, append it.
151
- if (!is_array ($ arr )) {
169
+ if (!is_array ($ arr )) {
152
170
$ node ->appendChild ($ xml ->createTextNode (self ::bool2str ($ arr )));
153
171
}
154
172
@@ -158,8 +176,8 @@ private static function &convert($node_name, $arr=array()) {
158
176
/*
159
177
* Get the root XML node, if there isn't one, create it.
160
178
*/
161
- private static function getXMLRoot (){
162
- if (empty (self ::$ xml )) {
179
+ private static function getXMLRoot () {
180
+ if (empty (self ::$ xml )) {
163
181
self ::init ();
164
182
}
165
183
return self ::$ xml ;
@@ -168,7 +186,7 @@ private static function getXMLRoot(){
168
186
/*
169
187
* Get string representation of boolean value
170
188
*/
171
- private static function bool2str ($ v ){
189
+ private static function bool2str ($ v ) {
172
190
//convert boolean to text value.
173
191
$ v = $ v === true ? 'true ' : $ v ;
174
192
$ v = $ v === false ? 'false ' : $ v ;
@@ -179,7 +197,7 @@ private static function bool2str($v){
179
197
* Check if the tag name or attribute name contains illegal characters
180
198
* Ref: http://www.w3.org/TR/xml/#sec-common-syn
181
199
*/
182
- private static function isValidTagName ($ tag ){
200
+ private static function isValidTagName ($ tag ) {
183
201
$ pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i ' ;
184
202
return preg_match ($ pattern , $ tag , $ matches ) && $ matches [0 ] == $ tag ;
185
203
}
0 commit comments