1
|
|
|
<?php |
2
|
|
|
namespace pastuhov\xml2object; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* XML to object parser. |
6
|
|
|
*/ |
7
|
|
|
class Parser |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* XML content. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
public $xml; |
15
|
|
|
/** |
16
|
|
|
* Tag names for 'to property conversion' |
17
|
|
|
* |
18
|
|
|
* @var string[] |
19
|
|
|
*/ |
20
|
|
|
public $convertToAttribute = [ |
21
|
|
|
'properties', |
22
|
|
|
'options', |
23
|
|
|
'features', |
24
|
|
|
'rows', |
25
|
|
|
]; |
26
|
|
|
/** |
27
|
|
|
* Children property. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
public $childrenProperty = 'children'; |
32
|
|
|
/** |
33
|
|
|
* XML parser. |
34
|
|
|
* |
35
|
|
|
* @var \XMLReader |
36
|
|
|
*/ |
37
|
|
|
protected $reader; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Convert. |
41
|
|
|
* |
42
|
|
|
* @return Node |
43
|
|
|
*/ |
44
|
|
|
public function process() |
45
|
|
|
{ |
46
|
|
|
$reader = $this->getReader(); |
47
|
|
|
|
48
|
|
|
$recipientChain = []; |
49
|
|
|
|
50
|
|
|
while($reader->read()) |
51
|
|
|
{ |
52
|
|
|
$nodeType = $reader->nodeType; |
53
|
|
|
|
54
|
|
|
switch ($nodeType) |
55
|
|
|
{ |
56
|
|
|
case \XMLReader::END_ELEMENT: |
57
|
|
|
if (!in_array($reader->name, $this->convertToAttribute, true) && count($recipientChain) !== 1) { |
58
|
|
|
$recipientChain = array_slice($recipientChain, 0, -1); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
break; |
62
|
|
|
case \XMLReader::ELEMENT: |
63
|
|
|
$recipient = end($recipientChain); |
64
|
|
|
$isConverted = false; |
65
|
|
|
|
66
|
|
|
if ($recipient && $recipient->_childrenProperty === null) { |
67
|
|
|
if ($isConverted = in_array($reader->name, $this->convertToAttribute, true)) { |
68
|
|
|
$recipient->_childrenProperty = $reader->name; |
69
|
|
|
|
70
|
|
|
break; |
71
|
|
|
} else { |
72
|
|
|
$recipient->_childrenProperty = $this->childrenProperty; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$node = new Node(); |
77
|
|
|
$node->_tagName = $reader->name; |
78
|
|
|
|
79
|
|
|
$hasAttributes = $reader->hasAttributes; |
80
|
|
|
$isEmptyElement = $reader->isEmptyElement; |
81
|
|
|
|
82
|
|
|
if ($hasAttributes) { |
83
|
|
|
while($reader->moveToNextAttribute()) { |
84
|
|
|
$node->{$reader->name} = $reader->value; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($recipient) { |
89
|
|
|
$recipient->{$recipient->_childrenProperty}[] = $node; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (!$isEmptyElement && !$isConverted) { |
93
|
|
|
$recipientChain[] = $node; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
break; |
97
|
|
|
case \XMLReader::TEXT: |
|
|
|
|
98
|
|
|
|
99
|
|
|
break; |
100
|
|
|
case \XMLReader::ATTRIBUTE: |
|
|
|
|
101
|
|
|
|
102
|
|
|
break; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$reader->close(); |
107
|
|
|
|
108
|
|
|
return $recipientChain[0]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return \XMLReader |
113
|
|
|
*/ |
114
|
|
|
protected function getReader() |
115
|
|
|
{ |
116
|
|
|
$reader = new \XMLReader(); |
117
|
|
|
$reader->XML($this->xml); |
118
|
|
|
|
119
|
|
|
return $reader; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.