Completed
Push — master ( 62c183...87995b )
by Mr
03:30
created

Rosario::key()   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
9
/**
10
 * Class Rosario, created for work with response array from RouterOS
11
 * as with multiple chunks of values, this class was created by memory save reasons.
12
 *
13
 * Based on RouterOSResponseArray solution by @arily
14
 *
15
 * @link    https://github.com/arily/RouterOSResponseArray
16
 * @package RouterOS
17
 * @since   0.10
18
 */
19
class Rosario extends Client implements Iterator, ArrayAccess, Countable
20
{
21
    /**
22
     * List of parsed variables
23
     *
24
     * @var array
25
     */
26
    protected $parsed = [];
27
28
    /**
29
     * List of RAW variables
30
     *
31
     * @var array
32
     */
33
    protected $raw = [];
34
35
    /**
36
     * Current position of array
37
     *
38
     * @var mixed
39
     */
40
    protected $current;
41
42
    /**
43
     * Read answer from server after query was executed
44
     *
45
     * A Mikrotik reply is formed of blocks
46
     * Each block starts with a word, one of ('!re', '!trap', '!done', '!fatal')
47
     * Each block end with an zero byte (empty line)
48
     * Reply ends with a complete !done or !fatal block (ended with 'empty line')
49
     * A !fatal block precedes TCP connexion close
50
     *
51
     * @param bool $parse
52
     * @return array
53
     */
54
    public function read(bool $parse = true): array
55
    {
56
        // Read answer from original client
57
        $raw = $orig = parent::read($parse);
58
59
        // Refresh current position
60
        $this->current = 0;
61
62
        // This RAW should't be an error
63
        $position = array_keys($raw, '!re');
64
65
        // Split RAW to chinks or use as subarray
66
        if (isset($position[1])) {
67
            $length = $position[1] - $position[0];
68
            $raw    = array_chunk($raw, $length);
69
            array_pop($raw);
70
        } else {
71
            $raw = [$raw];
72
        }
73
74
        // Store parsed RAW data
75
        $this->raw = $raw;
76
77
        // Return ready to use array
78
        return $orig;
79
    }
80
81
    /**
82
     * Move forward to next element
83
     */
84
    public function next()
85
    {
86
        ++$this->current;
87
    }
88
89
    /**
90
     * Return the current element
91
     *
92
     * @return mixed
93
     */
94
    public function current()
95
    {
96
        if (isset($this->parsed[$this->current])) {
97
            return $this->parsed[$this->current];
98
        }
99
100
        if (isset($this->raw[$this->current])) {
101
            return $this->parseResponse($this->raw[$this->current])[0];
102
        }
103
104
        return false;
105
    }
106
107
    /**
108
     * Return the key of the current element
109
     *
110
     * @return mixed
111
     */
112
    public function key()
113
    {
114
        return $this->current;
115
    }
116
117
    /**
118
     * Checks if current position is valid
119
     *
120
     * @return bool
121
     */
122
    public function valid(): bool
123
    {
124
        return isset($this->raw[$this->current]);
125
    }
126
127
    /**
128
     * Count elements of an object
129
     *
130
     * @return int
131
     */
132
    public function count(): int
133
    {
134
        return count($this->raw);
135
    }
136
137
    /**
138
     * Rewind the Iterator to the first element
139
     */
140
    public function rewind()
141
    {
142
        $this->current = 0;
143
    }
144
145
    /**
146
     * Offset to set
147
     *
148
     * @param mixed $offset
149
     * @param mixed $value
150
     */
151
    public function offsetSet($offset, $value)
152
    {
153
        if (null === $offset) {
154
            $this->parsed[] = $value;
155
        }
156
        $this->parsed[$offset] = $value;
157
    }
158
159
    /**
160
     * Whether a offset exists
161
     *
162
     * @param mixed $offset
163
     * @return bool
164
     */
165
    public function offsetExists($offset): bool
166
    {
167
        return isset($this->raw[$offset]);
168
    }
169
170
    /**
171
     * Offset to unset
172
     *
173
     * @param mixed $offset
174
     */
175
    public function offsetUnset($offset)
176
    {
177
        unset($this->parsed[$offset], $this->raw[$offset]);
178
    }
179
180
    /**
181
     * Offset to retrieve
182
     *
183
     * @param mixed $offset
184
     * @return mixed
185
     */
186
    public function offsetGet($offset)
187
    {
188
        if (isset($this->parsed[$offset])) {
189
            return $this->parsed[$offset];
190
        }
191
192
        if (isset($this->raw[$offset])) {
193
            return $this->parsed[$offset] = $this->parseResponse($this->raw[$offset])[0];
194
        }
195
196
        // For empty() function
197
        return null;
198
    }
199
200
    /**
201
     * Cleanup the array
202
     */
203
    public function flush()
204
    {
205
        $this->raw    = [];
206
        $this->parsed = [];
207
    }
208
209
}
210