1 | <?php |
||
29 | class ResponseIterator implements Iterator, ArrayAccess, Countable, Serializable |
||
30 | { |
||
31 | /** |
||
32 | * List of parser results from array |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | private $parsed = []; |
||
37 | |||
38 | /** |
||
39 | * List of RAW results from RouterOS |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | private $raw; |
||
44 | |||
45 | /** |
||
46 | * Initial value of array position |
||
47 | * |
||
48 | * @var int |
||
49 | */ |
||
50 | private $current = 0; |
||
51 | |||
52 | /** |
||
53 | * Object of main client |
||
54 | * |
||
55 | * @var \RouterOS\Client |
||
56 | */ |
||
57 | private $client; |
||
58 | |||
59 | /** |
||
60 | * ResponseIterator constructor. |
||
61 | * |
||
62 | * @param Client $client |
||
63 | */ |
||
64 | 1 | public function __construct(Client $client) |
|
65 | { |
||
66 | // Set current to default |
||
67 | 1 | $this->rewind(); |
|
68 | |||
69 | // Save client as parameter of object |
||
70 | 1 | $this->client = $client; |
|
71 | |||
72 | // Read RAW data from client |
||
73 | 1 | $raw = $client->read(false); |
|
74 | |||
75 | // This RAW should't be an error |
||
76 | 1 | $positions = array_keys($raw, '!re'); |
|
77 | 1 | $count = count($raw); |
|
78 | 1 | $result = []; |
|
79 | |||
80 | 1 | if (isset($positions[1])) { |
|
81 | |||
82 | foreach ($positions as $key => $position) { |
||
83 | |||
84 | // Get length of future block |
||
85 | $length = isset($positions[$key + 1]) |
||
86 | ? $positions[$key + 1] - $position + 1 |
||
87 | : $count - $position; |
||
88 | |||
89 | // Convert array to simple items, save as result |
||
90 | $result[] = array_slice($raw, $position, $length); |
||
91 | } |
||
92 | |||
93 | } else { |
||
94 | 1 | $result = [$raw]; |
|
95 | } |
||
96 | |||
97 | 1 | $this->raw = $result; |
|
98 | 1 | } |
|
99 | |||
100 | /** |
||
101 | * Move forward to next element |
||
102 | */ |
||
103 | public function next(): void |
||
107 | |||
108 | /** |
||
109 | * Previous value |
||
110 | */ |
||
111 | public function prev(): void |
||
115 | |||
116 | /** |
||
117 | * Return the current element |
||
118 | * |
||
119 | * @return mixed |
||
120 | */ |
||
121 | public function current() |
||
139 | |||
140 | /** |
||
141 | * Return the key of the current element |
||
142 | * |
||
143 | * @return mixed |
||
144 | */ |
||
145 | public function key() |
||
149 | |||
150 | /** |
||
151 | * Checks if current position is valid |
||
152 | * |
||
153 | * @return bool |
||
154 | */ |
||
155 | public function valid(): bool |
||
159 | |||
160 | /** |
||
161 | * Count elements of an object |
||
162 | * |
||
163 | * @return int |
||
164 | */ |
||
165 | public function count(): int |
||
169 | |||
170 | /** |
||
171 | * Rewind the Iterator to the first element |
||
172 | */ |
||
173 | 1 | public function rewind(): void |
|
174 | { |
||
175 | 1 | $this->current = 0; |
|
176 | 1 | } |
|
177 | |||
178 | /** |
||
179 | * Offset to set |
||
180 | * |
||
181 | * @param mixed $offset |
||
182 | * @param mixed $value |
||
183 | */ |
||
184 | public function offsetSet($offset, $value): void |
||
192 | |||
193 | /** |
||
194 | * Whether a offset exists |
||
195 | * |
||
196 | * @param mixed $offset |
||
197 | * |
||
198 | * @return bool |
||
199 | */ |
||
200 | public function offsetExists($offset): bool |
||
204 | |||
205 | /** |
||
206 | * Offset to unset |
||
207 | * |
||
208 | * @param mixed $offset |
||
209 | */ |
||
210 | public function offsetUnset($offset): void |
||
214 | |||
215 | /** |
||
216 | * Offset to retrieve |
||
217 | * |
||
218 | * @param mixed $offset |
||
219 | * |
||
220 | * @return bool|mixed |
||
221 | */ |
||
222 | public function offsetGet($offset) |
||
237 | |||
238 | /** |
||
239 | * String representation of object |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | public function serialize(): string |
||
247 | |||
248 | /** |
||
249 | * Constructs the object |
||
250 | * |
||
251 | * @param string $serialized |
||
252 | */ |
||
253 | public function unserialize($serialized): void |
||
257 | } |
||
258 |