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
5354 * $xml = Array2XML::createXML('root_node_name', $php_array);
5455 * echo $xml->saveXML();
5556 */
56-
5757class Array2XML {
5858
5959 private static $ xml = null ;
60- private static $ encoding = 'UTF-8 ' ;
60+ private static $ encoding = 'UTF-8 ' ;
6161
6262 /**
6363 * Initialize the root XML node [optional]
@@ -68,7 +68,7 @@ class Array2XML {
6868 public static function init ($ version = '1.0 ' , $ encoding = 'UTF-8 ' , $ format_output = true ) {
6969 self ::$ xml = new DomDocument ($ version , $ encoding );
7070 self ::$ xml ->formatOutput = $ format_output ;
71- self ::$ encoding = $ encoding ;
71+ self ::$ encoding = $ encoding ;
7272 }
7373
7474 /**
@@ -77,7 +77,7 @@ public static function init($version = '1.0', $encoding = 'UTF-8', $format_outpu
7777 * @param array $arr - aray to be converterd
7878 * @return DomDocument
7979 */
80- public static function &createXML ($ node_name , $ arr= array ()) {
80+ public static function &createXML ($ node_name , $ arr = array ()) {
8181 $ xml = self ::getXMLRoot ();
8282 $ xml ->appendChild (self ::convert ($ node_name , $ arr ));
8383
@@ -86,23 +86,29 @@ public static function &createXML($node_name, $arr=array()) {
8686 }
8787
8888 /**
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
89+ * Convert an Array to XML.
90+ *
91+ * @param string $node_name
92+ * Name of the root node to be converted.
93+ * @param array $arr
94+ * Array to be converted.
95+ *
96+ * @throws \Exception
97+ *
98+ * @return \DOMNode
9399 */
94- private static function &convert ($ node_name , $ arr= array ()) {
100+ private static function &convert ($ node_name , $ arr = array ()) {
95101
96102 //print_arr($node_name);
97103 $ xml = self ::getXMLRoot ();
98104 $ node = $ xml ->createElement ($ node_name );
99105
100- if (is_array ($ arr )){
106+ if (is_array ($ arr )) {
101107 // 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 );
108+ if (isset ($ arr ['@attributes ' ])) {
109+ foreach ($ arr ['@attributes ' ] as $ key => $ value ) {
110+ if (!self ::isValidTagName ($ key )) {
111+ throw new Exception ('[Array2XML] Illegal character in attribute name. attribute: ' . $ key . ' in node: ' . $ node_name );
106112 }
107113 $ node ->setAttribute ($ key , self ::bool2str ($ value ));
108114 }
@@ -111,12 +117,12 @@ private static function &convert($node_name, $arr=array()) {
111117
112118 // check if it has a value stored in @value, if yes store the value and return
113119 // else check if its directly stored as string
114- if (isset ($ arr ['@value ' ])) {
120+ if (isset ($ arr ['@value ' ])) {
115121 $ node ->appendChild ($ xml ->createTextNode (self ::bool2str ($ arr ['@value ' ])));
116122 unset($ arr ['@value ' ]); //remove the key from the array once done.
117123 //return from recursion, as a note with value cannot have child nodes.
118124 return $ node ;
119- } else if (isset ($ arr ['@cdata ' ])) {
125+ } else if (isset ($ arr ['@cdata ' ])) {
120126 $ node ->appendChild ($ xml ->createCDATASection (self ::bool2str ($ arr ['@cdata ' ])));
121127 unset($ arr ['@cdata ' ]); //remove the key from the array once done.
122128 //return from recursion, as a note with cdata cannot have child nodes.
@@ -125,17 +131,17 @@ private static function &convert($node_name, $arr=array()) {
125131 }
126132
127133 //create subnodes using recursion
128- if (is_array ($ arr )){
134+ if (is_array ($ arr )) {
129135 // 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 );
136+ foreach ($ arr as $ key => $ value ) {
137+ if (!self ::isValidTagName ($ key )) {
138+ throw new Exception ('[Array2XML] Illegal character in tag name. tag: ' . $ key . ' in node: ' . $ node_name );
133139 }
134- if (is_array ($ value ) && is_numeric (key ($ value ))) {
140+ if (is_array ($ value ) && is_numeric (key ($ value ))) {
135141 // MORE THAN ONE NODE OF ITS KIND;
136142 // if the new array is numeric index, means it is array of nodes of the same kind
137143 // it should follow the parent key name
138- foreach ($ value as $ k=> $ v ){
144+ foreach ($ value as $ k => $ v ) {
139145 $ node ->appendChild (self ::convert ($ key , $ v ));
140146 }
141147 } else {
@@ -148,7 +154,7 @@ private static function &convert($node_name, $arr=array()) {
148154
149155 // after we are done with all the keys in the array (if it is one)
150156 // we check if it has any text value, if yes, append it.
151- if (!is_array ($ arr )) {
157+ if (!is_array ($ arr )) {
152158 $ node ->appendChild ($ xml ->createTextNode (self ::bool2str ($ arr )));
153159 }
154160
@@ -158,8 +164,8 @@ private static function &convert($node_name, $arr=array()) {
158164 /*
159165 * Get the root XML node, if there isn't one, create it.
160166 */
161- private static function getXMLRoot (){
162- if (empty (self ::$ xml )) {
167+ private static function getXMLRoot () {
168+ if (empty (self ::$ xml )) {
163169 self ::init ();
164170 }
165171 return self ::$ xml ;
@@ -168,7 +174,7 @@ private static function getXMLRoot(){
168174 /*
169175 * Get string representation of boolean value
170176 */
171- private static function bool2str ($ v ){
177+ private static function bool2str ($ v ) {
172178 //convert boolean to text value.
173179 $ v = $ v === true ? 'true ' : $ v ;
174180 $ v = $ v === false ? 'false ' : $ v ;
@@ -179,7 +185,7 @@ private static function bool2str($v){
179185 * Check if the tag name or attribute name contains illegal characters
180186 * Ref: http://www.w3.org/TR/xml/#sec-common-syn
181187 */
182- private static function isValidTagName ($ tag ){
188+ private static function isValidTagName ($ tag ) {
183189 $ pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i ' ;
184190 return preg_match ($ pattern , $ tag , $ matches ) && $ matches [0 ] == $ tag ;
185191 }
0 commit comments