-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.php
More file actions
155 lines (125 loc) · 4.41 KB
/
Parser.php
File metadata and controls
155 lines (125 loc) · 4.41 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
<?php
namespace romston\TemplatedXmlParser;
use SimpleXMLElement;
class Parser
{
private static $default = '';
private static $deleteNamespaces = false;
public static function parse(string $xmlString, array $items, array $options = []): array
{
if (isset($options['default'])) {
self::$default = $options['default'];
}
if (isset($options['delete_namespaces'])) {
self::$deleteNamespaces = $options['delete_namespaces'];
}
if (self::$deleteNamespaces) {
$xmlString = self::deleteNamespacesFromXml($xmlString);
}
$xml = new SimpleXMLElement($xmlString);
$result = [];
foreach ($items as $field => $item) {
$result[$field] = self::processItem($xml, $item);
}
return $result;
}
private static function processItem(SimpleXMLElement $xml, $item)
{
if (!is_array($item)) {
return self::getScalar($xml, $item);
}
if (isset($item['value'])) {
return $item['value'];
}
$type = $item['type'] ?? null;
if ($type === 'array') {
return self::extractArray($xml, $item['path'], $item['fields']);
}
if ($type === 'join') {
return self::extractJoined($xml, $item['paths']);
}
if (isset($item['path_one_of'])) {
$value = self::extractOneOf($xml, $item['path_one_of']);
} else {
$value = self::getScalar($xml, $item['path']);
}
if ($type === null || $type === 'string') {
return $value;
}
if ($type === 'int') {
return (int)$value;
}
if ($type === 'bool' || $type === 'boolean') {
if (strtolower($value) === 'true') {
return true;
}
if (strtolower($value) === 'false') {
return false;
}
return self::$default;
}
// For unknown types.
return $value;
}
private static function getScalar(SimpleXMLElement $xml, string $path): string
{
if (self::$deleteNamespaces) {
$path = self::deleteNamespacesFromPath($path);
}
return $xml->xpath($path) ? (string)$xml->xpath($path)[0] : self::$default;
}
private static function extractArray(SimpleXMLElement $xml, string $rootPath, array $fields): array
{
if (self::$deleteNamespaces) {
$rootPath = self::deleteNamespacesFromPath($rootPath);
}
$elements = $xml->xpath($rootPath);
$result = [];
foreach ($elements as $element) {
$row = [];
foreach ($fields as $field => $item) {
$row[$field] = self::processItem($element, $item);
}
$result[] = $row;
}
return $result;
}
private static function extractJoined(SimpleXMLElement $xml, array $paths, string $separator = ' '): string
{
$result = [];
foreach ($paths as $path) {
$pathResult = self::getScalar($xml, $path);
if ($pathResult) {
$result[] = $pathResult;
}
}
return implode($separator, $result);
}
private static function extractOneOf(SimpleXMLElement $xml, array $paths): string
{
$value = self::$default;
foreach ($paths as $path) {
$value = self::getScalar($xml, $path);
if ($value !== self::$default) {
break;
}
}
return $value;
}
private static function deleteNamespacesFromXml(string $xmlString): string
{
// Delete all namespaces declaration.
$xmlString = preg_replace(['/xmlns[^=]*="[^"]*"/i', '/xsi[^=]*="[^"]*"/i'], '', $xmlString);
// Delete all namespaces usage.
$xmlString = preg_replace('/(<\/|<)[a-zA-Z0-9]+:([a-zA-Z0-9]+[ =>])/', '$1$2', $xmlString);
return $xmlString;
}
private static function deleteNamespacesFromPath(string $path): string
{
$elements = explode('/', $path);
array_walk($elements, static function (&$element) {
$element = preg_replace('/.*?:/', '', $element);
});
return implode('/', $elements);
}
}