-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyXMLArray.php
More file actions
executable file
·321 lines (260 loc) · 10.7 KB
/
MyXMLArray.php
File metadata and controls
executable file
·321 lines (260 loc) · 10.7 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
/*
* Copyright (c) 2025 Bloxtor (http://bloxtor.com) and Joao Pinto (http://jplpinto.com)
*
* Multi-licensed: BSD 3-Clause | Apache 2.0 | GNU LGPL v3 | HLNC License (http://bloxtor.com/LICENSE_HLNC.md)
* Choose one license that best fits your needs.
*
* Original XML to PHP Array Repo: https://github.com/a19836/xml-to-php-array/
* Original Bloxtor Repo: https://github.com/a19836/bloxtor
*
* YOU ARE NOT AUTHORIZED TO MODIFY OR REMOVE ANY PART OF THIS NOTICE!
*/
include get_lib("util.xml.MyXMLArrayItem");
include get_lib("util.xml.exception.MyXMLArrayException");
include_once get_lib("util.xml.UnicodeUTF8");
class MyXMLArray {
private $data;
public function __construct($data) {
$this->data = is_array($data) ? $data : array();
}
public function toXML($options = false) {
$prefix_tab = isset($options["prefix_tab"]) ? $options["prefix_tab"] : null;
return self::toXMLAux($this->data, $options, $prefix_tab);
//return self::toXMLAux2($this->data, $options, $prefix_tab);
}
//pretifies better the xml
private static function toXMLAux($arr, $options = false, $prefix_tab = "") {
$xml = "";
$to_decimal = isset($options["to_decimal"]) ? $options["to_decimal"] : null;
foreach ($arr as $key => $items) {
$key = !empty($options["lower_case_keys"]) ? strtolower($key) : (!empty($options["upper_case_keys"]) ? strtoupper($key) : $key);
$t = $items ? count($items) : 0;
for ($i = 0; $i < $t; $i++) {
$item = $items[$i];
$item_attrs = "";
if (!empty($item["@"])) {
foreach($item["@"] as $attr_name => $attr_value) {
$attr_name = !empty($options["lower_case_keys"]) ? strtolower($attr_name) : (!empty($options["upper_case_keys"]) ? strtoupper($attr_name) : $attr_name);
$attr_value = htmlentities($attr_value);
if($to_decimal)
$attr_value = UnicodeUTF8::to_decimal(htmlentities(html_entity_decode($attr_value)));
else
$attr_value = str_replace("&", "&", str_replace("&", "&", $attr_value));
$item_attrs .= " {$attr_name}=\"{$attr_value}\"";
}
}
$sub_xml = "";
if (isset($item["value"]))
$sub_xml .= is_numeric($item["value"]) || is_bool($item["value"]) ? $item["value"] : (!empty($item["value"]) ? "<![CDATA[" . $item["value"] . "]]>" : $item["value"]);
if (!empty($item["childs"]))
$sub_xml .= self::toXMLAux($item["childs"], $options, $prefix_tab . "\t");
$xml .= "\n$prefix_tab<{$key}" . $item_attrs;
$xml .= empty($sub_xml) && !is_numeric($sub_xml) ? " />" : ">$sub_xml" . (!empty($item["childs"]) ? "\n$prefix_tab" : "") . "</{$key}>";
}
}
return $xml;
}
/*
* This is other way that works too but the entities are converted into decimal. However the xml pretify doesn't work very well
* TODO: This doesn't add the CDATA, so untill this gets implemented, please use the method: self::toXMLAux(...)
*/
private static function toXMLAux2($arr, $options = false, $prefix_tab = "") {
$xml = "";
$root_node = new SimpleXMLElement("<root/>");
self::prepareXMLNode($root_node, $arr, $options);
foreach ($root_node->children() as $child)
$xml .= $child->asXML();
return $xml;
}
private static function prepareXMLNode(&$node, $arr, $options = false) {
$to_decimal = isset($options["to_decimal"]) ? $options["to_decimal"] : null;
if ($arr)
foreach($arr as $key => $items) {
$key = !empty($options["lower_case_keys"]) ? strtolower($key) : (!empty($options["upper_case_keys"]) ? strtoupper($key) : $key);
$t = $items ? count($items) : 0;
for ($i = 0; $i < $t; $i++) {
$item = $items[$i];
$child = $node->addChild($key, isset($item["value"]) ? $item["value"] : null);
if (!empty($item["@"]))
foreach($item["@"] as $attr_name => $attr_value) {
$attr_name = !empty($options["lower_case_keys"]) ? strtolower($attr_name) : (!empty($options["upper_case_keys"]) ? strtoupper($attr_name) : $attr_name);
if($to_decimal)
$attr_value = UnicodeUTF8::to_decimal(htmlentities($attr_value));
//$attr_value = UnicodeUTF8::to_decimal(htmlentities(html_entity_decode(htmlentities($attr_value))));
$child->addAttribute($attr_name, $attr_value);
}
self::prepareXMLNode($child, isset($item["childs"]) ? $item["childs"] : null, $options);
}
}
}
public function getNodeValue($nodes_path = "", $conditions = false, $index = 0) {
$nodes = $this->getNodes($nodes_path, $conditions, true);
$item = isset($nodes[$index]) ? $nodes[$index] : null;
return $item ? $item->getValue() : "";
}
public function getNodes($nodes_path = "", $conditions = false, $item_obj = false) {
$items = $this->getItemsPath($nodes_path, $this->data);
$nodes = array();
$t = count($items);
for($i = 0; $i < $t; $i++) {
$item = $this->getItem($i, $items);
if($item && $item->checkAttributes($conditions)) {
$nodes[] = $item_obj ? $item : $items[$i];
}
}
return $nodes;
}
private function getItemsPath($nodes_path, $data, $path_index = -1) {
$node_path = "";
$node_index_num = false;
$node_conditions = false;
$sub_nodes_path_exists = false;
$paths = explode("/", $nodes_path);//$nodes_path should be a path splited with '/' like in XSLT
$t = count($paths);
for($i = 0; $i < $t; $i++) {
$key = $paths[$i];
if($key && $i > $path_index) {
$path_index = $i;
$explode = explode("[", $key);
$node_path = $explode[0];
$node_condition = !empty($explode[1]) ? explode("]", $explode[1]) : false;
$node_condition = $node_condition && trim($node_condition[0]) ? trim($node_condition[0]) : false;
if(is_numeric($node_condition)) {// sample: /RESULT/VARS/XX[1]
$node_index_num = $node_condition - 1;
}
elseif($node_condition) {// sample: /RESULT/VARS/XX[@guid = '123' or (@guid = 456 && @name='jo\'ao' and @type ="x\"xx") || ID=2]
$node_conditions = $this->parseNodeCondition($node_condition);
}
for($j = $i + 1; $j < $t; $j++) {
if($paths[$j]) {
$sub_nodes_path_exists = true;
break;
}
}
break;
}
}
if(!$node_path) {
return $data;
}
$nodes = array();
if(!empty($data[ $node_path ])) {
$items = $data[ $node_path ];
$t = count($items);
for($i = 0; $i < $t; $i++) {
$item = is_numeric($node_index_num) ? (isset($items[$node_index_num]) ? $items[$node_index_num] : null) : $items[$i];
$continue = $node_conditions ? $this->checkNodeConditions($item, $node_conditions) : true;
if($continue) {
if($sub_nodes_path_exists) {
$childs = isset($item["childs"]) ? $item["childs"] : null;
if($childs) {
$sub_nodes = $this->getItemsPath($nodes_path, $childs, $path_index);
$t2 = count($sub_nodes);
for($j = 0; $j < $t2; $j++) {
$nodes[] = $sub_nodes[$j];
}
}
}
else {
$nodes[] = $item;
}
}
if(is_numeric($node_index_num)) {
break;
}
}
}
return $nodes;
}
private function getItem($index = 0, $data = false) {
$data = $this->getConfiguredData($data);
$item_data = !empty($data[$index]) ? $data[$index] : false;
if($item_data) {
$item = new MyXMLArrayItem($item_data);
return $item;
}
return false;
}
private function getConfiguredData($data) {
$data = $data ? $data : $this->data;
return is_array($data) ? $data : array();
}
private function checkNodeConditions($node, $node_conditions) {
$php_conditions_code = "";
$t = $node_conditions ? count($node_conditions) : 0;
for ($i = 0; $i < $t; $i++) {
$node_condition = $node_conditions[$i];
$node_condition_type = isset($node_condition["type"]) ? $node_condition["type"] : null;
$node_condition_value = isset($node_condition["value"]) ? $node_condition["value"] : null;
if ($node_condition_type == "node_or_attr") {
$node_condition_name = isset($node_condition["name"]) ? $node_condition["name"] : null;
$node_condition_operator = isset($node_condition["operator"]) ? $node_condition["operator"] : null;
$php_conditions_code .= "\$node[\"@\"][\"".$node_condition_name."\"] ".$node_condition_operator." ".$node_condition_value;
}
else {
$php_conditions_code .= " ".$node_condition_value." ";
}
}
if($php_conditions_code) {
try {
$php_conditions_code = "\$status = (".$php_conditions_code.") ? true : false;";
eval($php_conditions_code);
return $status;
}
catch(Exception $e) {
return false;
}
}
return true;
}
private function parseNodeCondition($node_condition) {
$conditions = array();
$regex_to_get_conditions = '/(((@?)([\w\-\+]+)(\s*)(=|==|\\!=|<>|<=|>=|<|>)(\s*)([0-9]+))|((@?)([\w\-\+]+)(\s*)(=|==|\\!=|<>|<=|>=|<|>)(\s*)((?<!\\\\)\')(([^\']*)(((?<=\\\\)\')*)([^\']*))((?<!\\\\)\'))|((@?)([\w\-\+]+)(\s*)(=|==|\\!=|<>|<=|>=|<|>)(\s*)((?<!\\\\)")(([^"]*)(((?<=\\\\)")*)([^"]*))((?<!\\\\)"))|((\s*)(or|and|&&|\|\||\(|\))*(\s*)))/iu'; //'\w' means all words with '_' and '/u' means with accents and ç too.
$regex_to_parse_condition = '/(@?)([\w\-\+]+)(\s*)(=|==|\\!=|<>|<=|>=|<|>)(\s*)(.*)/u'; //'\w' means all words with '_' and '/u' means with accents and ç too.
preg_match_all($regex_to_get_conditions, $node_condition, $matches);
$matches = isset($matches[1]) ? $matches[1] : null;
$t = is_array($matches) ? count($matches) : 0;
for ($i = 0; $i < $t; $i++) {
$match = $matches[$i];
preg_match_all($regex_to_parse_condition, $match, $sub_matches);
if (!empty($sub_matches[0])) {
$is_attribute = $sub_matches[1][0] == "@";
$condition_name = $sub_matches[2][0];
$condition_operator = $sub_matches[4][0] == "=" ? "==" : $sub_matches[4][0];
$condition_value = $sub_matches[6][0];
if(!$is_attribute) {
if(function_exists("launch_exception")) {
launch_exception(new MyXMLArrayException(1, $condition_name));
}
else {
throw new MyXMLArrayException(1, $condition_name);
}
return false;
}
$conditions[] = array("type" => "node_or_attr", "name" => $condition_name, "operator" => $condition_operator, "value" => $condition_value);
}
else {
$match = strtolower(trim($match));
if(count($conditions) && ($match == "and" || $match == "&&" || $match == "or" || $match == "||")) {
$match = $match == "and" ? "&&" : ($match == "or" ? "||" : $match);
$conditions[] = array("type" => "gate", "value" => $match);
}
else if($match == "(" || $match == ")") {
$conditions[] = array("type" => "separator", "value" => $match);
}
else if($match) {
if(function_exists("launch_exception")) {
launch_exception(new MyXMLArrayException(2, $match));
}
else {
throw new MyXMLArrayException(2, $match);
}
return false;
}
}
}
return $conditions;
}
}
?>