Completed
Pull Request — master (#33)
by Mr
08:17
created

ResponseIterator::valid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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
 *
16
 * @return mixed
17
 *
18
 * Based on RouterOSResponseArray solution by @arily
19
 *
20
 * @package RouterOS\Iterators
21
 * @link    https://github.com/arily/RouterOSResponseArray
22
 * @since   1.0.0
23
 */
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)
60
    {
61
        // Set current to default
62 4
        $this->rewind();
63
64
        // Save client as parameter of object
65 4
        $this->client = $client;
66
67
        // Read RAW data from client
68 4
        $raw = $client->read(false);
69
70
        // This RAW should't be an error
71 4
        $positions = array_keys($raw, '!re');
72 4
        $count     = count($raw);
73 4
        $result    = [];
74
75 4
        if (isset($positions[1])) {
76
77 3
            foreach ($positions as $key => $position) {
78
79
                // Get length of future block
80 3
                $length = isset($positions[$key + 1])
81 3
                    ? $positions[$key + 1] - $position + 1
82 3
                    : $count - $position;
83
84
                // Convert array to simple items, save as result
85 3
                $result[] = array_slice($raw, $position, $length);
86
            }
87
88
        } else {
89 2
            $result = [$raw];
90
        }
91
92 4
        $this->raw = $result;
93 4
    }
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
161
    {
162 1
        return count($this->raw);
163
    }
164
165
    /**
166
     * Rewind the Iterator to the first element
167
     */
168 4
    public function rewind()
169
    {
170 4
        $this->current = 0;
171 4
    }
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
196
    {
197
        return isset($this->raw[$offset]) && $this->raw[$offset] !== ['!re'];
198
    }
199
200
    /**
201
     * Offset to unset
202
     *
203
     * @param mixed $offset
204
     */
205
    public function offsetUnset($offset)
206
    {
207
        unset($this->parsed[$offset], $this->raw[$offset]);
208
    }
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
239
    {
240 1
        return serialize($this->raw);
241
    }
242
243
    /**
244
     * Constructs the object
245
     *
246
     * @param string $serialized
247
     */
248
    public function unserialize($serialized)
249
    {
250
        $this->raw = unserialize($serialized, null);
251
    }
252
}
253