Completed
Push — master ( 15d27a...559485 )
by Mr
04:51
created

ResponseIterator::unserialize()   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 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
 * @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
    public function __construct(Client $client)
59
    {
60
        // Set current to default
61
        $this->rewind();
62
63
        // Save client as parameter of object
64
        $this->client = $client;
65
66
        // Read RAW data from client
67
        $raw = $client->read(false);
68
69
        // This RAW should't be an error
70
        $positions = array_keys($raw, '!re');
71
        $count     = count($raw);
72
        $result    = [];
73
74
        if (isset($positions[1])) {
75
76
            foreach ($positions as $key => $position) {
77
78
                // Get length of future block
79
                $length = isset($positions[$key + 1])
80
                    ? $positions[$key + 1] - $position + 1
81
                    : $count - $position;
82
83
                // Convert array to simple items, save as result
84
                $result[] = array_slice($raw, $position, $length);
85
            }
86
87
        } else {
88
            $result = [$raw];
89
        }
90
91
        $this->raw = $result;
92
    }
93
94
    /**
95
     * Move forward to next element
96
     */
97
    public function next()
98
    {
99
        ++$this->current;
100
    }
101
102
    /**
103
     * Previous value
104
     */
105
    public function prev()
106
    {
107
        --$this->current;
108
    }
109
110
    /**
111
     * Return the current element
112
     *
113
     * @return mixed
114
     */
115
    public function current()
116
    {
117
        if (isset($this->parsed[$this->current])) {
118
            return $this->parsed[$this->current];
119
        }
120
121
        if ($this->valid()) {
122
123
            if (!isset($this->parsed[$this->current])) {
124
                $value = $this->client->parseResponse($this->raw[$this->current])[0];
125
                $this->offsetSet($this->current, $value);
126
            }
127
128
            return $this->parsed[$this->current];
129
        }
130
131
        return null;
132
    }
133
134
    /**
135
     * Return the key of the current element
136
     *
137
     * @return mixed
138
     */
139
    public function key()
140
    {
141
        return $this->current;
142
    }
143
144
    /**
145
     * Checks if current position is valid
146
     *
147
     * @return bool
148
     */
149
    public function valid(): bool
150
    {
151
        return isset($this->raw[$this->current]);
152
    }
153
154
    /**
155
     * Count elements of an object
156
     *
157
     * @return int
158
     */
159
    public function count(): int
160
    {
161
        return count($this->raw);
162
    }
163
164
    /**
165
     * Rewind the Iterator to the first element
166
     */
167
    public function rewind()
168
    {
169
        $this->current = 0;
170
    }
171
172
    /**
173
     * Offset to set
174
     *
175
     * @param mixed $offset
176
     * @param mixed $value
177
     */
178
    public function offsetSet($offset, $value)
179
    {
180
        if (null === $offset) {
181
            $this->parsed[] = $value;
182
        } else {
183
            $this->parsed[$offset] = $value;
184
        }
185
    }
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
    public function offsetGet($offset)
215
    {
216
        if (isset($this->parsed[$offset])) {
217
            return $this->parsed[$offset];
218
        }
219
220
        if (isset($this->raw[$offset]) && $this->raw[$offset] !== null) {
221
            $f = $this->client->parseResponse($this->raw[$offset]);
222
            if ($f !== []) {
223
                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
    public function serialize(): string
236
    {
237
        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