1 <?php
2 /**
3 * Array2XML: A class to convert array in PHP to XML
4 * It also takes into account attributes names unlike SimpleXML in PHP
5 * It returns the XML in form of DOMDocument class for further manipulation.
6 * It throws exception if the tag name or attribute name has illegal chars.
7 * Author : Lalit Patel
8 * Website: http://www.lalit.org/lab/convert-php-array-to-xml-with-attributes
9 * License: Apache License 2.0
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Version: 0.1 (10 July 2011)
12 * Version: 0.2 (16 August 2011)
13 * - replaced htmlentities() with htmlspecialchars() (Thanks to Liel Dulev)
14 * - fixed a edge case where root node has a false/null/0 value. (Thanks to Liel Dulev)
15 * Version: 0.3 (22 August 2011)
16 * - fixed tag sanitize regex which didn't allow tagnames with single character.
17 * Version: 0.4 (18 September 2011)
18 * - Added support for CDATA section using @cdata instead of @value.
19 * Version: 0.5 (07 December 2011)
20 * - Changed logic to check numeric array indices not starting from 0.
21 * Version: 0.6 (04 March 2012)
22 * - Code now doesn't @cdata to be placed in an empty array
23 * Version: 0.7 (24 March 2012)
24 * - Reverted to version 0.5
25 * Version: 0.8 (02 May 2012)
26 * - Removed htmlspecialchars() before adding to text node or attributes.
27 * Usage:
28 * $xml = Array2XML::createXML('root_node_name', $php_array);
29 * echo $xml->saveXML();
30 */
31
32 namespace Cross\Lib;
33
34 use DOMDocument;
35 use DOMNode;
36 use Exception;
37
38 class Array2XML
39 {
40
41 private static $xml = null;
42 private static $encoding = 'UTF-8';
43
44 /**
45 * Initialize the root XML node [optional]
46 *
47 * @param $version
48 * @param $encoding
49 * @param $format_output
50 */
51 public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true)
52 {
53 self::$xml = new DomDocument($version, $encoding);
54 self::$xml->formatOutput = $format_output;
55 self::$encoding = $encoding;
56 }
57
58 /**
59 * Convert an Array to XML
60 *
61 * @param string $node_name - name of the root node to be converted
62 * @param array $arr - array to be converterd
63 * @return DomDocument
64 * @throws Exception
65 */
66 public static function &createXML($node_name, $arr = array())
67 {
68 $xml = self::getXMLRoot();
69 $xml->appendChild(self::convert($node_name, $arr));
70
71 self::$xml = null; // clear the xml node in the class for 2nd time use.
72 return $xml;
73 }
74
75 /**
76 * Convert an Array to XML
77 *
78 * @param string $node_name - name of the root node to be converted
79 * @param array $arr - aray to be converterd
80 * @throws Exception
81 * @return DOMNode
82 */
83 private static function &convert($node_name, $arr = array())
84 {
85
86 //print_arr($node_name);
87 $xml = self::getXMLRoot();
88 $node = $xml->createElement($node_name);
89
90 if (is_array($arr)) {
91 // get the attributes first.;
92 if (isset($arr['@attributes'])) {
93 foreach ($arr['@attributes'] as $key => $value) {
94 if (!self::isValidTagName($key)) {
95 throw new Exception('[Array2XML] Illegal character in attribute name. attribute: ' . $key . ' in node: ' . $node_name);
96 }
97 $node->setAttribute($key, self::bool2str($value));
98 }
99 unset($arr['@attributes']); //remove the key from the array once done.
100 }
101
102 // check if it has a value stored in @value, if yes store the value and return
103 // else check if its directly stored as string
104 if (isset($arr['@value'])) {
105 $node->appendChild($xml->createTextNode(self::bool2str($arr['@value'])));
106 unset($arr['@value']); //remove the key from the array once done.
107 //return from recursion, as a note with value cannot have child nodes.
108 return $node;
109 }
110 else if (isset($arr['@cdata'])) {
111 $node->appendChild($xml->createCDATASection(self::bool2str($arr['@cdata'])));
112 unset($arr['@cdata']); //remove the key from the array once done.
113 //return from recursion, as a note with cdata cannot have child nodes.
114 return $node;
115 }
116 }
117
118 //create subnodes using recursion
119 if (is_array($arr)) {
120 // recurse to get the node for that key
121 foreach ($arr as $key => $value) {
122 if (!self::isValidTagName($key)) {
123 throw new Exception('[Array2XML] Illegal character in tag name. tag: ' . $key . ' in node: ' . $node_name);
124 }
125 if (is_array($value) && is_numeric(key($value))) {
126 // MORE THAN ONE NODE OF ITS KIND;
127 // if the new array is numeric index, means it is array of nodes of the same kind
128 // it should follow the parent key name
129 foreach ($value as $k => $v) {
130 $node->appendChild(self::convert($key, $v));
131 }
132 }
133 else {
134 // ONLY ONE NODE OF ITS KIND
135 $node->appendChild(self::convert($key, $value));
136 }
137 unset($arr[$key]); //remove the key from the array once done.
138 }
139 }
140
141 // after we are done with all the keys in the array (if it is one)
142 // we check if it has any text value, if yes, append it.
143 if (!is_array($arr)) {
144 $node->appendChild($xml->createTextNode(self::bool2str($arr)));
145 }
146
147 return $node;
148 }
149
150 /*
151 * Get the root XML node, if there isn't one, create it.
152 */
153 private static function getXMLRoot()
154 {
155 if (empty(self::$xml)) {
156 self::init();
157 }
158
159 return self::$xml;
160 }
161
162 /*
163 * Get string representation of boolean value
164 */
165 private static function bool2str($v)
166 {
167 //convert boolean to text value.
168 $v = $v === true ? 'true' : $v;
169 $v = $v === false ? 'false' : $v;
170
171 return $v;
172 }
173
174 /*
175 * Check if the tag name or attribute name contains illegal characters
176 * Ref: http://www.w3.org/TR/xml/#sec-common-syn
177 */
178 private static function isValidTagName($tag)
179 {
180 $pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i';
181
182 return preg_match($pattern, $tag, $matches) && $matches[0] == $tag;
183 }
184 }
185
186