1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RouterOS; |
4
|
|
|
|
5
|
|
|
use \Iterator, |
6
|
|
|
\ArrayAccess, |
7
|
|
|
\Countable, |
8
|
|
|
\Serializable; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This class was created by memory save reasons, it convert response |
12
|
|
|
* from RouterOS to readable array in safe way. |
13
|
|
|
* |
14
|
|
|
* @param array $raw Array RAW response from server |
15
|
|
|
* @return mixed |
16
|
|
|
* |
17
|
|
|
* Based on RouterOSResponseArray solution by @arily |
18
|
|
|
* |
19
|
|
|
* @package RouterOS\Iterators |
20
|
|
|
* @link https://github.com/arily/RouterOSResponseArray |
21
|
|
|
* @since 1.0.0 |
22
|
|
|
*/ |
23
|
|
|
class ResponseIterator implements Iterator, ArrayAccess, Countable, Serializable |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* List of parser results from array |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $parsed = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* List of RAW results from RouterOS |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $raw = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Initial value of array position |
41
|
|
|
* |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
private $current = 0; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Object of main client |
48
|
|
|
* |
49
|
|
|
* @var \RouterOS\Client |
50
|
|
|
*/ |
51
|
|
|
private $client; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* ResponseIterator constructor. |
55
|
|
|
* |
56
|
|
|
* @param Client $client |
57
|
|
|
*/ |
58
|
4 |
|
public function __construct(Client $client) |
59
|
|
|
{ |
60
|
|
|
// Set current to default |
61
|
4 |
|
$this->rewind(); |
62
|
|
|
|
63
|
|
|
// Save client as parameter of object |
64
|
4 |
|
$this->client = $client; |
65
|
|
|
|
66
|
|
|
// Read RAW data from client |
67
|
4 |
|
$raw = $client->read(false); |
68
|
|
|
|
69
|
|
|
// This RAW should't be an error |
70
|
4 |
|
$positions = array_keys($raw, '!re'); |
71
|
4 |
|
$count = count($raw); |
72
|
4 |
|
$result = []; |
73
|
|
|
|
74
|
4 |
|
if (isset($positions[1])) { |
75
|
|
|
|
76
|
3 |
|
foreach ($positions as $key => $position) { |
77
|
|
|
|
78
|
|
|
// Get length of future block |
79
|
3 |
|
$length = isset($positions[$key + 1]) |
80
|
3 |
|
? $positions[$key + 1] - $position + 1 |
81
|
3 |
|
: $count - $position; |
82
|
|
|
|
83
|
|
|
// Convert array to simple items, save as result |
84
|
3 |
|
$result[] = array_slice($raw, $position, $length); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} else { |
88
|
2 |
|
$result = [$raw]; |
89
|
|
|
} |
90
|
|
|
|
91
|
4 |
|
$this->raw = $result; |
92
|
4 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Move forward to next element |
96
|
|
|
*/ |
97
|
1 |
|
public function next() |
98
|
|
|
{ |
99
|
1 |
|
++$this->current; |
100
|
1 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Previous value |
104
|
|
|
*/ |
105
|
1 |
|
public function prev() |
106
|
|
|
{ |
107
|
1 |
|
--$this->current; |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Return the current element |
112
|
|
|
* |
113
|
|
|
* @return mixed |
114
|
|
|
*/ |
115
|
1 |
|
public function current() |
116
|
|
|
{ |
117
|
1 |
|
if (isset($this->parsed[$this->current])) { |
118
|
1 |
|
return $this->parsed[$this->current]; |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
if ($this->valid()) { |
122
|
|
|
|
123
|
1 |
|
if (!isset($this->parsed[$this->current])) { |
124
|
1 |
|
$value = $this->client->parseResponse($this->raw[$this->current])[0]; |
125
|
1 |
|
$this->offsetSet($this->current, $value); |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
return $this->parsed[$this->current]; |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Return the key of the current element |
136
|
|
|
* |
137
|
|
|
* @return mixed |
138
|
|
|
*/ |
139
|
1 |
|
public function key() |
140
|
|
|
{ |
141
|
1 |
|
return $this->current; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Checks if current position is valid |
146
|
|
|
* |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
1 |
|
public function valid(): bool |
150
|
|
|
{ |
151
|
1 |
|
return isset($this->raw[$this->current]); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Count elements of an object |
156
|
|
|
* |
157
|
|
|
* @return int |
158
|
|
|
*/ |
159
|
1 |
|
public function count(): int |
160
|
|
|
{ |
161
|
1 |
|
return count($this->raw); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Rewind the Iterator to the first element |
166
|
|
|
*/ |
167
|
4 |
|
public function rewind() |
168
|
|
|
{ |
169
|
4 |
|
$this->current = 0; |
170
|
4 |
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Offset to set |
174
|
|
|
* |
175
|
|
|
* @param mixed $offset |
176
|
|
|
* @param mixed $value |
177
|
|
|
*/ |
178
|
1 |
|
public function offsetSet($offset, $value) |
179
|
|
|
{ |
180
|
1 |
|
if (null === $offset) { |
181
|
|
|
$this->parsed[] = $value; |
182
|
|
|
} else { |
183
|
1 |
|
$this->parsed[$offset] = $value; |
184
|
|
|
} |
185
|
1 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Whether a offset exists |
189
|
|
|
* |
190
|
|
|
* @param mixed $offset |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
|
|
public function offsetExists($offset): bool |
194
|
|
|
{ |
195
|
|
|
return isset($this->raw[$offset]) && $this->raw[$offset] !== ['!re']; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Offset to unset |
200
|
|
|
* |
201
|
|
|
* @param mixed $offset |
202
|
|
|
*/ |
203
|
|
|
public function offsetUnset($offset) |
204
|
|
|
{ |
205
|
|
|
unset($this->parsed[$offset], $this->raw[$offset]); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Offset to retrieve |
210
|
|
|
* |
211
|
|
|
* @param mixed $offset |
212
|
|
|
* @return bool|mixed |
213
|
|
|
*/ |
214
|
1 |
|
public function offsetGet($offset) |
215
|
|
|
{ |
216
|
1 |
|
if (isset($this->parsed[$offset])) { |
217
|
|
|
return $this->parsed[$offset]; |
218
|
|
|
} |
219
|
|
|
|
220
|
1 |
|
if (isset($this->raw[$offset]) && $this->raw[$offset] !== null) { |
221
|
1 |
|
$f = $this->client->parseResponse($this->raw[$offset]); |
222
|
1 |
|
if ($f !== []) { |
223
|
1 |
|
return $this->parsed[$offset] = $f[0]; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return false; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* String representation of object |
232
|
|
|
* |
233
|
|
|
* @return string |
234
|
|
|
*/ |
235
|
1 |
|
public function serialize(): string |
236
|
|
|
{ |
237
|
1 |
|
return serialize($this->raw); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Constructs the object |
242
|
|
|
* |
243
|
|
|
* @param string $serialized |
244
|
|
|
*/ |
245
|
|
|
public function unserialize($serialized) |
246
|
|
|
{ |
247
|
|
|
$this->raw = unserialize($serialized, null); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|