|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nathanmac\Utilities\Parser\Formats; |
|
4
|
|
|
|
|
5
|
|
|
use Nathanmac\Utilities\Parser\Exceptions\ParserException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* XML Formatter |
|
9
|
|
|
* |
|
10
|
|
|
* @package Nathanmac\Utilities\Parser\Formats |
|
11
|
|
|
* @author Nathan Macnamara <[email protected]> |
|
12
|
|
|
* @license https://github.com/nathanmac/Parser/blob/master/LICENSE.md MIT |
|
13
|
|
|
*/ |
|
14
|
|
|
class XML implements FormatInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Parse Payload Data |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $payload |
|
20
|
|
|
* |
|
21
|
|
|
* @throws ParserException |
|
22
|
|
|
* @return array |
|
23
|
|
|
* |
|
24
|
|
|
*/ |
|
25
|
48 |
|
public function parse($payload) |
|
26
|
|
|
{ |
|
27
|
48 |
|
if ($payload) { |
|
28
|
|
|
try { |
|
29
|
45 |
|
$xml = simplexml_load_string($payload, 'SimpleXMLElement', (LIBXML_VERSION >= 20700) ? (LIBXML_PARSEHUGE | LIBXML_NOCDATA) : LIBXML_NOCDATA); |
|
30
|
42 |
|
$ns = ['' => null] + $xml->getDocNamespaces(true); |
|
31
|
42 |
|
return $this->recursive_parse($xml, $ns); |
|
|
|
|
|
|
32
|
3 |
|
} catch (\Exception $ex) { |
|
33
|
3 |
|
throw new ParserException('Failed To Parse XML'); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
3 |
|
return []; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
42 |
|
protected function recursive_parse($xml, $ns) |
|
41
|
|
|
{ |
|
42
|
42 |
|
$xml_string = (string)$xml; |
|
43
|
|
|
|
|
44
|
42 |
|
if ($xml->count() == 0 and $xml_string != '') { |
|
45
|
42 |
|
if (count($xml->attributes()) == 0) { |
|
46
|
39 |
|
if (trim($xml_string) == '') { |
|
47
|
3 |
|
$result = null; |
|
48
|
3 |
|
} else { |
|
49
|
39 |
|
$result = $xml_string; |
|
50
|
|
|
} |
|
51
|
39 |
|
} else { |
|
52
|
6 |
|
$result = array('#text' => $xml_string); |
|
53
|
|
|
} |
|
54
|
42 |
|
} else { |
|
55
|
39 |
|
$result = null; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
42 |
|
foreach ($ns as $nsName => $nsUri) { |
|
59
|
42 |
|
foreach ($xml->attributes($nsUri) as $attName => $attValue) { |
|
60
|
24 |
|
if (!empty($nsName)) { |
|
61
|
|
|
$attName = "{$nsName}:{$attName}"; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
24 |
|
$result["@{$attName}"] = (string)$attValue; |
|
65
|
42 |
|
} |
|
66
|
|
|
|
|
67
|
42 |
|
foreach ($xml->children($nsUri) as $childName => $child) { |
|
68
|
39 |
|
if (!empty($nsName)) { |
|
69
|
6 |
|
$childName = "{$nsName}:{$childName}"; |
|
70
|
6 |
|
} |
|
71
|
|
|
|
|
72
|
39 |
|
$child = $this->recursive_parse($child, $ns); |
|
73
|
|
|
|
|
74
|
39 |
|
if (is_array($result) and array_key_exists($childName, $result)) { |
|
75
|
18 |
|
if (is_array($result[$childName]) and is_numeric(key($result[$childName]))) { |
|
76
|
12 |
|
$result[$childName][] = $child; |
|
77
|
12 |
|
} else { |
|
78
|
18 |
|
$temp = $result[$childName]; |
|
79
|
18 |
|
$result[$childName] = [$temp, $child]; |
|
80
|
|
|
} |
|
81
|
18 |
|
} else { |
|
82
|
39 |
|
$result[$childName] = $child; |
|
83
|
|
|
} |
|
84
|
42 |
|
} |
|
85
|
42 |
|
} |
|
86
|
|
|
|
|
87
|
42 |
|
return $result; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|