1 | <?php |
||
24 | class ResponseIterator implements Iterator, ArrayAccess, Countable, Serializable |
||
25 | { |
||
26 | /** |
||
27 | * List of parser results from array |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | private $parsed = []; |
||
32 | |||
33 | /** |
||
34 | * List of RAW results from RouterOS |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | private $raw = []; |
||
39 | |||
40 | /** |
||
41 | * Initial value of array position |
||
42 | * |
||
43 | * @var int |
||
44 | */ |
||
45 | private $current = 0; |
||
46 | |||
47 | /** |
||
48 | * Object of main client |
||
49 | * |
||
50 | * @var \RouterOS\Client |
||
51 | */ |
||
52 | private $client; |
||
53 | |||
54 | /** |
||
55 | * ResponseIterator constructor. |
||
56 | * |
||
57 | * @param Client $client |
||
58 | */ |
||
59 | 4 | public function __construct(Client $client) |
|
94 | |||
95 | /** |
||
96 | * Move forward to next element |
||
97 | */ |
||
98 | public function next() |
||
99 | { |
||
100 | ++$this->current; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Previous value |
||
105 | */ |
||
106 | public function prev() |
||
107 | { |
||
108 | --$this->current; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Return the current element |
||
113 | * |
||
114 | * @return mixed |
||
115 | */ |
||
116 | public function current() |
||
117 | { |
||
118 | if (isset($this->parsed[$this->current])) { |
||
119 | return $this->parsed[$this->current]; |
||
120 | } |
||
121 | |||
122 | if ($this->valid()) { |
||
123 | |||
124 | if (!isset($this->parsed[$this->current])) { |
||
125 | $value = $this->client->parseResponse($this->raw[$this->current])[0]; |
||
126 | $this->offsetSet($this->current, $value); |
||
127 | } |
||
128 | |||
129 | return $this->parsed[$this->current]; |
||
130 | } |
||
131 | |||
132 | return null; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Return the key of the current element |
||
137 | * |
||
138 | * @return mixed |
||
139 | */ |
||
140 | public function key() |
||
141 | { |
||
142 | return $this->current; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Checks if current position is valid |
||
147 | * |
||
148 | * @return bool |
||
149 | */ |
||
150 | public function valid(): bool |
||
151 | { |
||
152 | return isset($this->raw[$this->current]); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Count elements of an object |
||
157 | * |
||
158 | * @return int |
||
159 | */ |
||
160 | 1 | public function count(): int |
|
164 | |||
165 | /** |
||
166 | * Rewind the Iterator to the first element |
||
167 | */ |
||
168 | 4 | public function rewind() |
|
172 | |||
173 | /** |
||
174 | * Offset to set |
||
175 | * |
||
176 | * @param mixed $offset |
||
177 | * @param mixed $value |
||
178 | */ |
||
179 | public function offsetSet($offset, $value) |
||
180 | { |
||
181 | if (null === $offset) { |
||
182 | $this->parsed[] = $value; |
||
183 | } else { |
||
184 | $this->parsed[$offset] = $value; |
||
185 | } |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Whether a offset exists |
||
190 | * |
||
191 | * @param mixed $offset |
||
192 | * |
||
193 | * @return bool |
||
194 | */ |
||
195 | public function offsetExists($offset): bool |
||
199 | |||
200 | /** |
||
201 | * Offset to unset |
||
202 | * |
||
203 | * @param mixed $offset |
||
204 | */ |
||
205 | public function offsetUnset($offset) |
||
209 | |||
210 | /** |
||
211 | * Offset to retrieve |
||
212 | * |
||
213 | * @param mixed $offset |
||
214 | * |
||
215 | * @return bool|mixed |
||
216 | */ |
||
217 | 1 | public function offsetGet($offset) |
|
218 | { |
||
219 | 1 | if (isset($this->parsed[$offset])) { |
|
220 | return $this->parsed[$offset]; |
||
221 | } |
||
222 | |||
223 | 1 | if (isset($this->raw[$offset]) && $this->raw[$offset] !== null) { |
|
224 | 1 | $f = $this->client->parseResponse($this->raw[$offset]); |
|
225 | if ($f !== []) { |
||
226 | return $this->parsed[$offset] = $f[0]; |
||
227 | } |
||
228 | } |
||
229 | |||
230 | return false; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * String representation of object |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | 1 | public function serialize(): string |
|
242 | |||
243 | /** |
||
244 | * Constructs the object |
||
245 | * |
||
246 | * @param string $serialized |
||
247 | */ |
||
248 | public function unserialize($serialized) |
||
252 | } |
||
253 |