PluginProbe
Yatra – Travel Booking & Tour Operator Software / 2.1.16
Yatra – Travel Booking & Tour Operator Software v2.1.16
3.0.15 3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 2.0.10 All 83 releases
yatra / core / Libraries / array2xml.php

array2xml.php in Yatra – Travel Booking & Tour Operator Software 2.1.16, at core/Libraries/array2xml.php

138 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Exit if accessed directly
4 defined( 'ABSPATH' ) || exit;
5
6 class Array2XML {
7
8 private static $xml = null;
9 private static $encoding = 'UTF-8';
10
11 /**
12 * Initialize the root XML node [optional]
13 * @param $version
14 * @param $encoding
15 * @param $format_output
16 */
17 public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true) {
18 self::$xml = new DomDocument($version, $encoding);
19 self::$xml->formatOutput = $format_output;
20 self::$encoding = $encoding;
21 }
22
23 /**
24 * Convert an Array to XML
25 * @param string $node_name - name of the root node to be converted
26 * @param array $arr - aray to be converterd
27 * @return DomDocument
28 */
29 public static function &createXML($node_name, $arr=array()) {
30
31 $xml = self::getXMLRoot();
32 $xml->appendChild(self::convert($node_name, $arr));
33
34 self::$xml = null; // clear the xml node in the class for 2nd time use.
35 return $xml;
36 }
37
38 /**
39 * Convert an Array to XML
40 * @param string $node_name - name of the root node to be converted
41 * @param array $arr - aray to be converterd
42 * @return DOMNode
43 */
44 private static function &convert($node_name, $arr=array()) {
45
46 //print_arr($node_name);
47 $xml = self::getXMLRoot();
48 $node = $xml->createElement($node_name);
49
50 if(is_array($arr)){
51 // get the attributes first.;
52 if(isset($arr['@attributes'])) {
53 foreach($arr['@attributes'] as $key => $value) {
54 if(!self::isValidTagName($key)) {
55 throw new Exception('[Array2XML] Illegal character in attribute name. attribute: '.$key.' in node: '.$node_name);
56 }
57 $node->setAttribute($key, self::bool2str($value));
58 }
59 unset($arr['@attributes']); //remove the key from the array once done.
60 }
61
62 // check if it has a value stored in @value, if yes store the value and return
63 // else check if its directly stored as string
64 if(isset($arr['@value'])) {
65 $node->appendChild($xml->createTextNode(self::bool2str($arr['@value'])));
66 unset($arr['@value']); //remove the key from the array once done.
67 //return from recursion, as a note with value cannot have child nodes.
68 return $node;
69 } else if(isset($arr['@cdata'])) {
70 $node->appendChild($xml->createCDATASection(self::bool2str($arr['@cdata'])));
71 unset($arr['@cdata']); //remove the key from the array once done.
72 //return from recursion, as a note with cdata cannot have child nodes.
73 return $node;
74 }
75 }
76
77 //create subnodes using recursion
78 if(is_array($arr)){
79 // recurse to get the node for that key
80 foreach($arr as $key=>$value){
81 if(!self::isValidTagName($key)) {
82 throw new Exception('[Array2XML] Illegal character in tag name. tag: '.$key.' in node: '.$node_name);
83 }
84 if(is_array($value) && is_numeric(key($value))) {
85 // MORE THAN ONE NODE OF ITS KIND;
86 // if the new array is numeric index, means it is array of nodes of the same kind
87 // it should follow the parent key name
88 foreach($value as $k=>$v){
89 $node->appendChild(self::convert($key, $v));
90 }
91 } else {
92 // ONLY ONE NODE OF ITS KIND
93 $node->appendChild(self::convert($key, $value));
94 }
95 unset($arr[$key]); //remove the key from the array once done.
96 }
97 }
98
99 // after we are done with all the keys in the array (if it is one)
100 // we check if it has any text value, if yes, append it.
101 if(!is_array($arr)) {
102 $node->appendChild($xml->createTextNode(self::bool2str($arr)));
103 }
104
105 return $node;
106 }
107
108 /*
109 * Get the root XML node, if there isn't one, create it.
110 */
111 private static function getXMLRoot(){
112 if(empty(self::$xml)) {
113 self::init();
114 }
115 return self::$xml;
116 }
117
118 /*
119 * Get string representation of boolean value
120 */
121 private static function bool2str($v){
122 //convert boolean to text value.
123 $v = $v === true ? 'true' : $v;
124 $v = $v === false ? 'false' : $v;
125 return $v;
126 }
127
128 /*
129 * Check if the tag name or attribute name contains illegal characters
130 * Ref: http://www.w3.org/TR/xml/#sec-common-syn
131 */
132 private static function isValidTagName($tag){
133 $pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i';
134 return preg_match($pattern, $tag, $matches) && $matches[0] == $tag;
135 }
136 }
137 ?>
138