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
|
45 |
|
public function parse($payload) |
26
|
|
|
{ |
27
|
45 |
|
if ($payload) { |
28
|
|
|
try { |
29
|
42 |
|
$xml = simplexml_load_string($payload, 'SimpleXMLElement', LIBXML_NOCDATA); |
30
|
39 |
|
$ns = ['' => null] + $xml->getDocNamespaces(true); |
31
|
39 |
|
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
|
39 |
|
protected function recursive_parse($xml, $ns) |
41
|
|
|
{ |
42
|
39 |
|
$xml_string = (string) $xml; |
43
|
|
|
|
44
|
39 |
|
if($xml->count() == 0 and $xml_string != '') { |
|
|
|
|
45
|
39 |
|
if(count($xml->attributes()) == 0){ |
46
|
36 |
|
$result = $xml_string; |
47
|
36 |
|
}else{ |
48
|
6 |
|
$result = array($xml_string); |
49
|
|
|
} |
50
|
39 |
|
}else{ |
51
|
36 |
|
$result = null; |
52
|
|
|
} |
53
|
|
|
|
54
|
39 |
|
foreach ($ns as $nsName => $nsUri) { |
55
|
39 |
|
foreach ($xml->attributes($nsUri) as $attName => $attValue) { |
56
|
21 |
|
if ( ! empty($nsName)) { |
57
|
|
|
$attName = "{$nsName}:{$attName}"; |
58
|
|
|
} |
59
|
|
|
|
60
|
21 |
|
$result = ["@{$attName}" => (string) $attValue] + (array) $result; |
61
|
39 |
|
} |
62
|
|
|
|
63
|
39 |
|
foreach ($xml->children($nsUri) as $childName => $child) { |
64
|
36 |
|
if ( ! empty($nsName)) { |
65
|
6 |
|
$childName = "{$nsName}:{$childName}"; |
66
|
6 |
|
} |
67
|
|
|
|
68
|
36 |
|
$child = $this->recursive_parse($child, $ns); |
69
|
|
|
|
70
|
36 |
|
if (is_array($result) and array_key_exists($childName, $result)) { |
|
|
|
|
71
|
15 |
|
if (is_array($result[$childName]) and is_numeric(key($result[$childName]))) { |
|
|
|
|
72
|
9 |
|
$result[$childName][] = $child; |
73
|
9 |
|
} else { |
74
|
15 |
|
$temp = $result[$childName]; |
75
|
15 |
|
$result[$childName] = [$temp, $child]; |
76
|
|
|
} |
77
|
15 |
|
} else { |
78
|
36 |
|
$result[$childName] = $child; |
79
|
|
|
} |
80
|
39 |
|
} |
81
|
39 |
|
} |
82
|
|
|
|
83
|
39 |
|
return $result; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.