Completed
Push — master ( 4d94db...b0fc40 )
by Mr
03:38
created

Query::where()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace RouterOS;
4
5
use RouterOS\Exceptions\ClientException;
6
use RouterOS\Exceptions\QueryException;
7
use RouterOS\Interfaces\QueryInterface;
8
9
/**
10
 * Class Query for building queries
11
 *
12
 * @package RouterOS
13
 * @since   0.1
14
 */
15
class Query implements QueryInterface
16
{
17
    /**
18
     * Array of query attributes
19
     *
20
     * @var array
21
     */
22
    private $_attributes = [];
23
24
    /**
25
     * Some additional operations
26
     *
27
     * @var string
28
     */
29
    private $_operations;
30
31
    /**
32
     * Tag of query
33
     *
34
     * @var string
35
     */
36
    private $_tag;
37
38
    /**
39
     * Endpoint of query
40
     *
41
     * @var string
42
     */
43
    private $_endpoint;
44
45
    /**
46
     * List of available operators for "->where()" method
47
     */
48
    public const AVAILABLE_OPERATORS = [
49
        '-',  // Does not have
50
        '=',  // Equal
51
        '>',  // More than
52
        '<'   // Less than
53
    ];
54
55
    /**
56
     * Query constructor.
57
     *
58
     * @param array|string $endpoint   Path of endpoint
59
     * @param array        $attributes List of attributes which should be set
60
     *
61
     * @throws \RouterOS\Exceptions\QueryException
62
     */
63 31
    public function __construct($endpoint, array $attributes = [])
64
    {
65 31
        if (\is_string($endpoint)) {
66 27
            $this->setEndpoint($endpoint);
67 27
            $this->setAttributes($attributes);
68 4
        } elseif (\is_array($endpoint)) {
69 3
            $query = array_shift($endpoint);
70 3
            $this->setEndpoint($query);
71 3
            $this->setAttributes($endpoint);
72
        } else {
73 1
            throw new QueryException('Specified endpoint is incorrect');
74
        }
75 30
    }
76
77
    /**
78
     * Where logic of query
79
     *
80
     * @param string          $key      Key which need to find
81
     * @param bool|string|int $value    Value which need to check (by default true)
82
     * @param bool|string|int $operator It may be one from list [-,=,>,<]
83
     *
84
     * @return \RouterOS\Query
85
     * @throws \RouterOS\Exceptions\QueryException
86
     * @since 1.0.0
87
     */
88 5
    public function where(string $key, $operator = null, $value = null): self
89
    {
90 5
        return $this->world('?' . $key, $operator, $value);
91
    }
92
93
    /**
94
     * Setter for write/update queries
95
     *
96
     * @param string          $key   Key which need to find
97
     * @param bool|string|int $value Value which need to check (by default true)
98
     *
99
     * @return \RouterOS\Query
100
     * @throws \RouterOS\Exceptions\QueryException
101
     * @since 1.1
102
     */
103
    public function equal(string $key, $value = null): self
104
    {
105
        return $this->world('=' . $key, null, $value);
106
    }
107
108
    /**
109
     * Write world to RouterOS (the work is mine)
110
     *
111
     * @param string          $key      Key which need to find
112
     * @param bool|string|int $value    Value which need to check (by default true)
113
     * @param bool|string|int $operator It may be one from list [-,=,>,<]
114
     *
115
     * @return \RouterOS\Query
116
     * @throws \RouterOS\Exceptions\QueryException
117
     */
118 5
    private function world(string $key, $operator = null, $value = null): self
119
    {
120 5
        if (null !== $operator && null === $value) {
121
122
            // Client may set only two parameters, that mean what $operator is $value
123 4
            $value = $operator;
124
125
            // And operator should be "="
126 4
            $operator = null;
127
        }
128
129 5
        if (null !== $operator && null !== $value) {
130
            // If operator is available in list
131 2
            if (\in_array($operator, self::AVAILABLE_OPERATORS, true)) {
132 1
                $key = $operator . $key;
133
            } else {
134 1
                throw new QueryException('Operator "' . $operator . '" in not in allowed list [' . implode(',', self::AVAILABLE_OPERATORS) . ']');
135
            }
136
        }
137
138 4
        if (null !== $value) {
139 4
            $value = '=' . $value;
140
        }
141
142 4
        $this->add($key . $value);
143 4
        return $this;
144
    }
145
146
    /**
147
     * Append additional operations
148
     *
149
     * @param string $operations
150
     *
151
     * @return \RouterOS\Query
152
     * @since 1.0.0
153
     */
154 2
    public function operations(string $operations): self
155
    {
156 2
        $this->_operations = '?#' . $operations;
157 2
        return $this;
158
    }
159
160
    /**
161
     * Append tag to query (it should be at end)
162
     *
163
     * @param string $name
164
     *
165
     * @return \RouterOS\Query
166
     * @since 1.0.0
167
     */
168 2
    public function tag(string $name): self
169
    {
170 2
        $this->_tag = '.tag=' . $name;
171 2
        return $this;
172
    }
173
174
    /**
175
     * Append to array yet another attribute of query
176
     *
177
     * @param string $word
178
     *
179
     * @return \RouterOS\Query
180
     */
181 6
    public function add(string $word): Query
182
    {
183 6
        $this->_attributes[] = $word;
184 6
        return $this;
185
    }
186
187
    /**
188
     * Get attributes array of current query
189
     *
190
     * @return array
191
     */
192 22
    public function getAttributes(): array
193
    {
194 22
        return $this->_attributes;
195
    }
196
197
    /**
198
     * Set array of attributes
199
     *
200
     * @param array $attributes
201
     *
202
     * @return \RouterOS\Query
203
     * @since 0.7
204
     */
205 30
    public function setAttributes(array $attributes): Query
206
    {
207 30
        $this->_attributes = $attributes;
208 30
        return $this;
209
    }
210
211
    /**
212
     * Get endpoint of current query
213
     *
214
     * @return string|null
215
     */
216 3
    public function getEndpoint()
217
    {
218 3
        return $this->_endpoint;
219
    }
220
221
    /**
222
     * Set endpoint of query
223
     *
224
     * @param string|null $endpoint
225
     *
226
     * @return \RouterOS\Query
227
     * @since 0.7
228
     */
229 30
    public function setEndpoint(string $endpoint = null): Query
230
    {
231 30
        $this->_endpoint = $endpoint;
232 30
        return $this;
233
    }
234
235
    /**
236
     * Build body of query
237
     *
238
     * @return array
239
     * @throws \RouterOS\Exceptions\QueryException
240
     */
241 19
    public function getQuery(): array
242
    {
243 19
        if ($this->_endpoint === null) {
244 1
            throw new QueryException('Endpoint of query is not set');
245
        }
246
247
        // Get all attributes and prepend endpoint to this list
248 18
        $attributes = $this->getAttributes();
249 18
        array_unshift($attributes, $this->_endpoint);
250
251
        // If operations is set then add to query
252 18
        if (is_string($this->_operations) && !empty($this->_operations)) {
253 2
            $attributes[] = $this->_operations;
254
        }
255
256
        // If tag is set then added to query
257 18
        if (is_string($this->_tag) && !empty($this->_tag)) {
258 2
            $attributes[] = $this->_tag;
259
        }
260
261 18
        return $attributes;
262
    }
263
}
264