22/**
33 * OpenLSS - Lighter Smarter Simpler
44 *
5- * This file is part of OpenLSS.
5+ * This file is part of OpenLSS.
66 *
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.
1111 *
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.
1616 *
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+ */
2121namespace LSS ;
22+
2223use \DomDocument ;
2324use \Exception ;
2425
4849 * - Reverted to version 0.5
4950 * Version: 0.8 (02 May 2012)
5051 * - 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.
5154 *
5255 * Usage:
5356 * $xml = Array2XML::createXML('root_node_name', $php_array);
5457 * echo $xml->saveXML();
5558 */
56-
5759class Array2XML {
5860
61+ /**
62+ * @var DOMDocument
63+ */
5964 private static $ xml = null ;
60- private static $ encoding = 'UTF-8 ' ;
65+ private static $ encoding = 'UTF-8 ' ;
6166
6267 /**
6368 * Initialize the root XML node [optional]
@@ -68,7 +73,7 @@ class Array2XML {
6873 public static function init ($ version = '1.0 ' , $ encoding = 'UTF-8 ' , $ format_output = true ) {
6974 self ::$ xml = new DomDocument ($ version , $ encoding );
7075 self ::$ xml ->formatOutput = $ format_output ;
71- self ::$ encoding = $ encoding ;
76+ self ::$ encoding = $ encoding ;
7277 }
7378
7479 /**
@@ -77,7 +82,7 @@ public static function init($version = '1.0', $encoding = 'UTF-8', $format_outpu
7782 * @param array $arr - aray to be converterd
7883 * @return DomDocument
7984 */
80- public static function &createXML ($ node_name , $ arr= array ()) {
85+ public static function &createXML ($ node_name , $ arr = array ()) {
8186 $ xml = self ::getXMLRoot ();
8287 $ xml ->appendChild (self ::convert ($ node_name , $ arr ));
8388
@@ -86,23 +91,29 @@ public static function &createXML($node_name, $arr=array()) {
8691 }
8792
8893 /**
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
93104 */
94- private static function &convert ($ node_name , $ arr= array ()) {
105+ private static function &convert ($ node_name , $ arr = array ()) {
95106
96107 //print_arr($node_name);
97108 $ xml = self ::getXMLRoot ();
98109 $ node = $ xml ->createElement ($ node_name );
99110
100- if (is_array ($ arr )){
111+ if (is_array ($ arr )) {
101112 // 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 );
106117 }
107118 $ node ->setAttribute ($ key , self ::bool2str ($ value ));
108119 }
@@ -111,31 +122,38 @@ private static function &convert($node_name, $arr=array()) {
111122
112123 // check if it has a value stored in @value, if yes store the value and return
113124 // else check if its directly stored as string
114- if (isset ($ arr ['@value ' ])) {
125+ if (isset ($ arr ['@value ' ])) {
115126 $ node ->appendChild ($ xml ->createTextNode (self ::bool2str ($ arr ['@value ' ])));
116127 unset($ arr ['@value ' ]); //remove the key from the array once done.
117128 //return from recursion, as a note with value cannot have child nodes.
118129 return $ node ;
119- } else if (isset ($ arr ['@cdata ' ])) {
130+ } else if (isset ($ arr ['@cdata ' ])) {
120131 $ node ->appendChild ($ xml ->createCDATASection (self ::bool2str ($ arr ['@cdata ' ])));
121132 unset($ arr ['@cdata ' ]); //remove the key from the array once done.
122133 //return from recursion, as a note with cdata cannot have child nodes.
123134 return $ node ;
124135 }
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+ }
125143 }
126144
127145 //create subnodes using recursion
128- if (is_array ($ arr )){
146+ if (is_array ($ arr )) {
129147 // 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 );
133151 }
134- if (is_array ($ value ) && is_numeric (key ($ value ))) {
152+ if (is_array ($ value ) && is_numeric (key ($ value ))) {
135153 // MORE THAN ONE NODE OF ITS KIND;
136154 // if the new array is numeric index, means it is array of nodes of the same kind
137155 // it should follow the parent key name
138- foreach ($ value as $ k=> $ v ){
156+ foreach ($ value as $ k => $ v ) {
139157 $ node ->appendChild (self ::convert ($ key , $ v ));
140158 }
141159 } else {
@@ -148,7 +166,7 @@ private static function &convert($node_name, $arr=array()) {
148166
149167 // after we are done with all the keys in the array (if it is one)
150168 // we check if it has any text value, if yes, append it.
151- if (!is_array ($ arr )) {
169+ if (!is_array ($ arr )) {
152170 $ node ->appendChild ($ xml ->createTextNode (self ::bool2str ($ arr )));
153171 }
154172
@@ -158,8 +176,8 @@ private static function &convert($node_name, $arr=array()) {
158176 /*
159177 * Get the root XML node, if there isn't one, create it.
160178 */
161- private static function getXMLRoot (){
162- if (empty (self ::$ xml )) {
179+ private static function getXMLRoot () {
180+ if (empty (self ::$ xml )) {
163181 self ::init ();
164182 }
165183 return self ::$ xml ;
@@ -168,7 +186,7 @@ private static function getXMLRoot(){
168186 /*
169187 * Get string representation of boolean value
170188 */
171- private static function bool2str ($ v ){
189+ private static function bool2str ($ v ) {
172190 //convert boolean to text value.
173191 $ v = $ v === true ? 'true ' : $ v ;
174192 $ v = $ v === false ? 'false ' : $ v ;
@@ -179,7 +197,7 @@ private static function bool2str($v){
179197 * Check if the tag name or attribute name contains illegal characters
180198 * Ref: http://www.w3.org/TR/xml/#sec-common-syn
181199 */
182- private static function isValidTagName ($ tag ){
200+ private static function isValidTagName ($ tag ) {
183201 $ pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i ' ;
184202 return preg_match ($ pattern , $ tag , $ matches ) && $ matches [0 ] == $ tag ;
185203 }
0 commit comments