1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* LICENSE: This file is subject to the terms and conditions defined in |
5
|
|
|
* file 'LICENSE', which is part of this source code package. |
6
|
|
|
* |
7
|
|
|
* @copyright 2016 Copyright(c) - All rights reserved. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Rafrsr\LibArray2Xml; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Array2XML: A class to convert array in PHP to XML |
14
|
|
|
* It also takes into account attributes names unlike SimpleXML in PHP |
15
|
|
|
* It returns the XML in form of DOMDocument class for further manipulation. |
16
|
|
|
* It throws exception if the tag name or attribute name has illegal chars. |
17
|
|
|
* |
18
|
|
|
* Based on http://www.lalit.org/lab/convert-xml-to-array-in-php-xml2array/ |
19
|
|
|
* |
20
|
|
|
* - some minor bug fixes |
21
|
|
|
* - support for php7 |
22
|
|
|
* - tests |
23
|
|
|
* |
24
|
|
|
* Usage: |
25
|
|
|
* $xml = Array2XML::createXML('root_node_name', $php_array); |
26
|
|
|
* echo $xml->saveXML(); |
27
|
|
|
*/ |
28
|
|
|
class Array2XML |
29
|
|
|
{ |
30
|
|
|
use CommonsTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Convert an Array to XML |
34
|
|
|
* |
35
|
|
|
* @param string $nodeName - name of the root node to be converted |
36
|
|
|
* @param mixed $data - content to put into the xml |
37
|
|
|
* |
38
|
|
|
* @return \DOMDocument |
39
|
|
|
* @throws \InvalidArgumentException |
40
|
|
|
*/ |
41
|
|
|
public static function createXML($nodeName, $data) |
42
|
|
|
{ |
43
|
|
|
$xml = self::getXMLRoot(); |
44
|
|
|
$xml->appendChild(self::convert($nodeName, $data)); |
45
|
|
|
|
46
|
|
|
self::$xml = null;// clear the xml node in the class for 2nd time use. |
47
|
|
|
|
48
|
|
|
return $xml; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Convert an Array to XML |
53
|
|
|
* |
54
|
|
|
* @param string $nodeName - name of the root node to be converted |
55
|
|
|
* @param mixed $data - content to put into the xml |
56
|
|
|
* |
57
|
|
|
* @return \DOMNode |
58
|
|
|
* @throws \InvalidArgumentException |
59
|
|
|
*/ |
60
|
|
|
private static function convert($nodeName, $data) |
61
|
|
|
{ |
62
|
|
|
//print_arr($nodeName); |
63
|
|
|
$xml = self::getXMLRoot(); |
64
|
|
|
$node = $xml->createElement($nodeName); |
65
|
|
|
|
66
|
|
|
if (is_array($data)) { |
67
|
|
|
// get the attributes first.; |
68
|
|
|
if (array_key_exists(self::$prefixAttributes . 'attributes', $data)) { |
69
|
|
|
foreach ($data[self::$prefixAttributes . 'attributes'] as $key => $value) { |
70
|
|
View Code Duplication |
if (!self::isValidTagName($key)) { |
|
|
|
|
71
|
|
|
$msg = sprintf('[Array2XML] Illegal character in attribute name. attribute: %s in node: %s', $key, $nodeName); |
72
|
|
|
throw new \InvalidArgumentException($msg); |
73
|
|
|
} |
74
|
|
|
$node->setAttribute($key, self::bool2str($value)); |
75
|
|
|
} |
76
|
|
|
unset($data[self::$prefixAttributes . 'attributes']); //remove the key from the array once done. |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// check if it has a value stored in @value, if yes store the value and return |
80
|
|
|
// else check if its directly stored as string |
81
|
|
|
if (array_key_exists(self::$prefixAttributes . 'value', $data)) { |
82
|
|
|
$node->appendChild($xml->createTextNode(self::bool2str($data[self::$prefixAttributes . 'value']))); |
83
|
|
|
unset($data[self::$prefixAttributes . 'value']); //remove the key from the array once done. |
84
|
|
|
//return from recursion, as a note with value cannot have child nodes. |
85
|
|
|
return $node; |
86
|
|
|
} else { |
87
|
|
|
if (array_key_exists(self::$prefixAttributes . 'cdata', $data)) { |
88
|
|
|
$node->appendChild($xml->createCDATASection(self::bool2str($data[self::$prefixAttributes . 'cdata']))); |
89
|
|
|
unset($data[self::$prefixAttributes . 'cdata']); //remove the key from the array once done. |
90
|
|
|
//return from recursion, as a note with cdata cannot have child nodes. |
91
|
|
|
return $node; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
//create sub-nodes using recursion |
97
|
|
|
if (is_array($data)) { |
98
|
|
|
// recurse to get the node for that key |
99
|
|
|
foreach ($data as $key => $value) { |
100
|
|
View Code Duplication |
if (!self::isValidTagName($key)) { |
|
|
|
|
101
|
|
|
$msg = sprintf('[Array2XML] Illegal character in tag name. tag: %s in node: %s', $key, $nodeName); |
102
|
|
|
throw new \InvalidArgumentException($msg); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (is_array($value) && reset($value) && is_numeric(key($value))) { |
106
|
|
|
// MORE THAN ONE NODE OF ITS KIND; |
107
|
|
|
// if the new array is numeric index, means it is array of nodes of the same kind |
108
|
|
|
// it should follow the parent key name |
109
|
|
|
foreach ($value as $k => $v) { |
110
|
|
|
$node->appendChild(self::convert($key, $v)); |
111
|
|
|
} |
112
|
|
|
} else { |
113
|
|
|
// ONLY ONE NODE OF ITS KIND |
114
|
|
|
$node->appendChild(self::convert($key, $value)); |
115
|
|
|
} |
116
|
|
|
unset($data[$key]); //remove the key from the array once done. |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// after we are done with all the keys in the array (if it is one) |
121
|
|
|
// we check if it has any text value, if yes, append it. |
122
|
|
|
if (!is_array($data)) { |
123
|
|
|
$node->appendChild($xml->createTextNode(self::bool2str($data))); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $node; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/* |
130
|
|
|
* Get string representation of boolean value |
131
|
|
|
*/ |
132
|
|
|
private static function bool2str($v) |
133
|
|
|
{ |
134
|
|
|
//convert boolean to text value. |
135
|
|
|
$v = $v === true ? 'true' : $v; |
136
|
|
|
$v = $v === false ? 'false' : $v; |
137
|
|
|
|
138
|
|
|
return $v; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/* |
142
|
|
|
* Check if the tag name or attribute name contains illegal characters |
143
|
|
|
* Ref: http://www.w3.org/TR/xml/#sec-common-syn |
144
|
|
|
*/ |
145
|
|
|
private static function isValidTagName($tag) |
146
|
|
|
{ |
147
|
|
|
$pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i'; |
148
|
|
|
|
149
|
|
|
return preg_match($pattern, $tag, $matches) && $matches[0] === $tag; |
150
|
|
|
} |
151
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.