1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Serkin Alexander <[email protected]> |
5
|
|
|
* @license https://github.com/serkin/ymlparser/LICENSE MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace YMLParser\Driver; |
9
|
|
|
|
10
|
|
|
class XMLReader implements DriverInterface { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var \XMLReader |
14
|
|
|
*/ |
15
|
|
|
private $xml; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Link to stored xml file. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $filename; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Gets categories. |
26
|
|
|
* |
27
|
|
|
* @return arry Array of \YMLParser\Node\Category instances or empty array |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
public function getCategories() { |
|
|
|
|
30
|
|
|
$returnArr = []; |
31
|
|
|
$this->moveToStart(); |
32
|
|
|
$xml = $this->xml; |
33
|
|
|
|
34
|
|
|
while ($xml->read()) { |
35
|
|
|
if ($xml->nodeType == \XMLReader::ELEMENT && $xml->name == 'categories') { |
36
|
|
|
while ($xml->read() && $xml->name != 'categories') { |
37
|
|
|
if ($xml->nodeType == \XMLReader::ELEMENT) { |
38
|
|
|
$arr = []; |
39
|
|
|
|
40
|
|
|
if ($xml->hasAttributes) { |
41
|
|
|
|
42
|
|
|
while ($xml->moveToNextAttribute()) { |
43
|
|
|
$arr[strtolower($xml->name)] = $xml->value; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$xml->read(); |
48
|
|
|
$arr['value'] = $xml->value; |
49
|
|
|
$returnArr[] = new \YMLParser\Node\Category($arr); |
50
|
|
|
|
51
|
|
|
unset($arr); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $returnArr; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Gets currencies. |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function getCurrencies() { |
|
|
|
|
66
|
|
|
|
67
|
|
|
$returnArr = []; |
68
|
|
|
$this->moveToStart(); |
69
|
|
|
$xml = $this->xml; |
70
|
|
|
|
71
|
|
|
while ($xml->read()) { |
72
|
|
|
if ($xml->nodeType == \XMLReader::ELEMENT && $xml->name == 'currencies') { |
73
|
|
|
while ($xml->read() && $xml->name != 'currencies') { |
74
|
|
|
if ($xml->nodeType == \XMLReader::ELEMENT) { |
75
|
|
|
$arr = []; |
76
|
|
|
|
77
|
|
|
if ($xml->hasAttributes) { |
78
|
|
|
|
79
|
|
|
while ($xml->moveToNextAttribute()) { |
80
|
|
|
$arr[strtolower($xml->name)] = $xml->value; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$xml->read(); |
85
|
|
|
$arr['value'] = $xml->value; |
86
|
|
|
$returnArr[] = new \YMLParser\Node\Currency($arr); |
87
|
|
|
|
88
|
|
|
unset($arr); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $returnArr; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Gets offers. |
99
|
|
|
* |
100
|
|
|
* @param \Closure $filter |
101
|
|
|
* |
102
|
|
|
* @return \Iterator Array of \YMLParser\Node\Offer instances or empty array |
103
|
|
|
*/ |
104
|
|
|
public function getOffers(\Closure $filter = null) { |
105
|
|
|
$this->moveToStart(); |
106
|
|
|
$xml = $this->xml; |
107
|
|
|
|
108
|
|
|
while ($xml->read()) { |
109
|
|
|
if ($xml->nodeType == \XMLReader::ELEMENT && $xml->name == 'offers') { |
110
|
|
|
while ($xml->read() && $xml->name != 'offers') { |
111
|
|
|
if ($xml->nodeType == \XMLReader::ELEMENT && $xml->name == 'offer') { |
112
|
|
|
$arr = $this->getElementAttributes($xml); |
113
|
|
|
|
114
|
|
|
while ($xml->read() && $xml->name != 'offer') { |
115
|
|
|
|
116
|
|
|
if ($xml->nodeType == \XMLReader::ELEMENT) { |
117
|
|
|
|
118
|
|
|
$name = mb_strtolower($xml->name); |
119
|
|
|
|
120
|
|
|
if ($name == 'param') { |
121
|
|
|
$tmpArr = ['name' => $xml->getAttribute('name')]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$xml->read(); |
125
|
|
|
|
126
|
|
|
if ($name == 'param') { |
127
|
|
|
$arr['params'][] = array_merge(['value' => $xml->value], $tmpArr); |
|
|
|
|
128
|
|
|
} else { |
129
|
|
|
if ($name == 'picture') { |
130
|
|
|
$arr['pictures'][] = $xml->value; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$arr[$name] = $xml->value; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$returnValue = new \YMLParser\Node\Offer($arr); |
139
|
|
|
|
140
|
|
|
if (!is_null($filter)) { |
141
|
|
|
if ($filter($returnValue)) { |
142
|
|
|
yield $returnValue; |
143
|
|
|
} |
144
|
|
|
} else { |
145
|
|
|
yield $returnValue; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Gets attributes from element. |
155
|
|
|
* |
156
|
|
|
* @param \XMLReader $element |
157
|
|
|
* |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
|
|
private function getElementAttributes(\XMLReader $element) { |
161
|
|
|
$returnArr = []; |
162
|
|
|
|
163
|
|
|
if ($element->hasAttributes) { |
164
|
|
|
while ($element->moveToNextAttribute()) { |
165
|
|
|
$returnArr[mb_strtolower($element->name)] = $element->value; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $returnArr; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Opens file. |
174
|
|
|
* |
175
|
|
|
* @param type $filename |
176
|
|
|
* |
177
|
|
|
* @throws \Exception |
178
|
|
|
* |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
|
|
public function open($filename) { |
182
|
|
|
$this->filename = $filename; |
|
|
|
|
183
|
|
|
$this->xml = new \XMLReader(); |
184
|
|
|
|
185
|
|
|
return $this->xml->open($filename); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Gets amount of offers. |
190
|
|
|
* |
191
|
|
|
* @param \Closure $filter |
192
|
|
|
* |
193
|
|
|
* @return int |
194
|
|
|
*/ |
195
|
|
|
public function countOffers(\Closure $filter = null) { |
196
|
|
|
$returnValue = 0; |
197
|
|
|
|
198
|
|
|
foreach ($this->getOffers($filter) as $el): |
199
|
|
|
$returnValue++; |
200
|
|
|
endforeach; |
201
|
|
|
|
202
|
|
|
return $returnValue; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Rewinds cursor to the first element. |
207
|
|
|
* |
208
|
|
|
* @return bool |
209
|
|
|
*/ |
210
|
|
|
private function moveToStart() { |
211
|
|
|
$this->xml->close(); |
212
|
|
|
|
213
|
|
|
return $this->xml->open($this->filename); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
} |
217
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.