Completed
Push — master ( fbdbe0...f7dc06 )
by Mr
13s queued 11s
created

ResponseIterator::offsetExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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