PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.18.0
GiveWP – Donation Plugin and Fundraising Platform v2.18.0
4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / libraries / array2xml.php
give / includes / libraries Last commit date
googlechartlib 8 years ago array2xml.php 8 years ago browser.php 8 years ago give-pdf.php 6 years ago wp-async-request.php 8 years ago wp-background-process.php 8 years ago
array2xml.php
133 lines
1 <?php
2 class Array2XML {
3
4 private static $xml = null;
5 private static $encoding = 'UTF-8';
6
7 /**
8 * Initialize the root XML node [optional]
9 * @param $version
10 * @param $encoding
11 * @param $format_output
12 */
13 public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true) {
14 self::$xml = new DomDocument($version, $encoding);
15 self::$xml->formatOutput = $format_output;
16 self::$encoding = $encoding;
17 }
18
19 /**
20 * Convert an Array to XML
21 * @param string $node_name - name of the root node to be converted
22 * @param array $arr - aray to be converterd
23 * @return DomDocument
24 */
25 public static function &createXML($node_name, $arr=array()) {
26 $xml = self::getXMLRoot();
27 $xml->appendChild(self::convert($node_name, $arr));
28
29 self::$xml = null; // clear the xml node in the class for 2nd time use.
30 return $xml;
31 }
32
33 /**
34 * Convert an Array to XML
35 * @param string $node_name - name of the root node to be converted
36 * @param array $arr - aray to be converterd
37 * @return DOMNode
38 */
39 private static function &convert($node_name, $arr=array()) {
40
41 //print_arr($node_name);
42 $xml = self::getXMLRoot();
43 $node = $xml->createElement($node_name);
44
45 if(is_array($arr)){
46 // get the attributes first.;
47 if(isset($arr['@attributes'])) {
48 foreach($arr['@attributes'] as $key => $value) {
49 if(!self::isValidTagName($key)) {
50 throw new Exception('[Array2XML] Illegal character in attribute name. attribute: '.$key.' in node: '.$node_name);
51 }
52 $node->setAttribute($key, self::bool2str($value));
53 }
54 unset($arr['@attributes']); //remove the key from the array once done.
55 }
56
57 // check if it has a value stored in @value, if yes store the value and return
58 // else check if its directly stored as string
59 if(isset($arr['@value'])) {
60 $node->appendChild($xml->createTextNode(self::bool2str($arr['@value'])));
61 unset($arr['@value']); //remove the key from the array once done.
62 //return from recursion, as a note with value cannot have child nodes.
63 return $node;
64 } else if(isset($arr['@cdata'])) {
65 $node->appendChild($xml->createCDATASection(self::bool2str($arr['@cdata'])));
66 unset($arr['@cdata']); //remove the key from the array once done.
67 //return from recursion, as a note with cdata cannot have child nodes.
68 return $node;
69 }
70 }
71
72 //create subnodes using recursion
73 if(is_array($arr)){
74 // recurse to get the node for that key
75 foreach($arr as $key=>$value){
76 if(!self::isValidTagName($key)) {
77 throw new Exception('[Array2XML] Illegal character in tag name. tag: '.$key.' in node: '.$node_name);
78 }
79 if(is_array($value) && is_numeric(key($value))) {
80 // MORE THAN ONE NODE OF ITS KIND;
81 // if the new array is numeric index, means it is array of nodes of the same kind
82 // it should follow the parent key name
83 foreach($value as $k=>$v){
84 $node->appendChild(self::convert($key, $v));
85 }
86 } else {
87 // ONLY ONE NODE OF ITS KIND
88 $node->appendChild(self::convert($key, $value));
89 }
90 unset($arr[$key]); //remove the key from the array once done.
91 }
92 }
93
94 // after we are done with all the keys in the array (if it is one)
95 // we check if it has any text value, if yes, append it.
96 if(!is_array($arr)) {
97 $node->appendChild($xml->createTextNode(self::bool2str($arr)));
98 }
99
100 return $node;
101 }
102
103 /*
104 * Get the root XML node, if there isn't one, create it.
105 */
106 private static function getXMLRoot(){
107 if(empty(self::$xml)) {
108 self::init();
109 }
110 return self::$xml;
111 }
112
113 /*
114 * Get string representation of boolean value
115 */
116 private static function bool2str($v){
117 //convert boolean to text value.
118 $v = $v === true ? 'true' : $v;
119 $v = $v === false ? 'false' : $v;
120 return $v;
121 }
122
123 /*
124 * Check if the tag name or attribute name contains illegal characters
125 * Ref: http://www.w3.org/TR/xml/#sec-common-syn
126 */
127 private static function isValidTagName($tag){
128 $pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i';
129 return preg_match($pattern, $tag, $matches) && $matches[0] == $tag;
130 }
131 }
132 ?>
133