forked from jtrumbull/xml-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml-parser.class.php
More file actions
139 lines (123 loc) · 3.57 KB
/
xml-parser.class.php
File metadata and controls
139 lines (123 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace XMLParser;
use Exception;
use InvalidArgumentException;
use SimpleXMLElement;
class XMLParser {
private static $_defaultRootNode = 'root';
private static $_defaultListNode = 'list';
private static $_defaultItemNode = 'item';
private static $_defaultEncoding = 'UTF-8';
private static $_defaultVersion = '1.0';
private static $_defaultAttrTag = 'attr:';
public static function encode ($data, $root = null)
{
if ($data instanceof SimpleXMLElement) {
return $data;
}
try {
$data = self::_validateEncodeData($data);
$version = self::$_defaultVersion;
$encoding = self::$_defaultEncoding;
$xml_string = "<?xml version=\"{$version}\" encoding=\"{$encoding}\" ?>";
$node = self::_formatName( is_null($root) ?
self::$_defaultRootNode :
$root);
$value = ($is_array = is_array($data)) ? NULL : self::_formatValue($data);
$xml_string .= "<$node>$value</$node>";
$xml = new SimpleXMLElement($xml_string);
if ($is_array) {
$xml = self::_addChildren($xml,$data);
}
}
catch (Exception $e) {
trigger_error($e->getMessage(), E_USER_ERROR);
}
return isset($xml) ? $xml : null; // isset() essentially to make editor happy.
}
public static function objectToArray($std) {
if (is_object($std)) {
$std = get_object_vars($std);
}
if (is_array($std)) {
return array_map(['self','objectToArray'], $std);
}
else {
return $std;
}
}
private static function _validateEncodeData ($data)
{
if(is_object($data)){ // Try conversion
$data = self::objectToArray($data);
}
if (is_object($data)) { // If it's still an object throw exception
throw new InvalidArgumentException(
"Invalid data type supplied for XMLParser::encode"
);
}
return $data;
}
private static function _addChildren (SimpleXMLElement $element, $data)
{
foreach ($data as $key => $value) {
$regex = '/^'.self::$_defaultAttrTag.'([a-z0-9\._-]*)/';
$is_attr = preg_match($regex, $key, $attr);
if ($is_attr) {
if (is_array($value)) {
foreach( $value as $k=>$v ) {
$element->addAttribute(
self::_formatName($k),
self::_formatValue($v));
}
} else {
$element->addAttribute(
self::_formatName($attr[1]),
self::_formatValue($value)
);
}
continue;
}
$node = self::_formatName(
is_numeric($key) ?
(is_array($value) ?
self::$_defaultListNode :
self::$_defaultItemNode) : $key
);
if (is_array($value)) {
$child = $element->addChild($node);
self::_addChildren($child,$value);
continue;
}
$element->addChild($node, $value);
}
return $element;
}
private static function _formatName ($string)
{
$p = [
'/[^a-z0-9\._ -]/i' => '',
'/(?=^[[0-9]\.\-\:^xml])/i' => self::$_defaultItemNode.'_',
'/ /' => '_'
];
$string = preg_replace(array_keys($p), array_values($p), $string);
return strtolower($string);
}
private static function _formatValue ($string)
{
$string = is_null($string) ? 'NULL' : $string;
return is_bool($string) ? self::_bool($string) : $string;
}
private static function _bool ($bool)
{
return $bool ? 'TRUE' : 'FALSE';
}
public static function decode ($xml)
{
if (!$xml instanceof SimpleXMLElement)
{
$xml = new SimpleXMLElement($xml);
}
return json_decode(json_encode($xml),false);
}
}