|
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
|
28 |
|
public function __construct($endpoint, array $attributes = []) |
|
64
|
|
|
{ |
|
65
|
28 |
|
if (\is_string($endpoint)) { |
|
66
|
24 |
|
$this->setEndpoint($endpoint); |
|
67
|
24 |
|
$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
|
27 |
|
} |
|
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
|
4 |
|
public function where(string $key, $operator = null, $value = null): self |
|
89
|
|
|
{ |
|
90
|
4 |
|
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
|
4 |
|
private function world(string $key, $operator = null, $value = null): self |
|
119
|
|
|
{ |
|
120
|
4 |
|
if (null !== $operator && null === $value) { |
|
121
|
|
|
|
|
122
|
|
|
// Client may set only two parameters, that mean what $operator is $value |
|
123
|
3 |
|
$value = $operator; |
|
124
|
|
|
|
|
125
|
|
|
// And operator should be "=" |
|
126
|
3 |
|
$operator = null; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
4 |
|
if (null !== $operator && null !== $value) { |
|
130
|
|
|
// If operator is available in list |
|
131
|
1 |
|
if (\in_array($operator, self::AVAILABLE_OPERATORS, true)) { |
|
132
|
|
|
$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
|
3 |
|
if (null !== $value) { |
|
139
|
3 |
|
$value = '=' . $value; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
3 |
|
$this->add($key . $value); |
|
143
|
3 |
|
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
|
1 |
|
public function operations(string $operations): self |
|
155
|
|
|
{ |
|
156
|
1 |
|
$this->_operations = '?#' . $operations; |
|
157
|
1 |
|
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
|
1 |
|
public function tag(string $name): self |
|
169
|
|
|
{ |
|
170
|
1 |
|
$this->_tag = '.tag=' . $name; |
|
171
|
1 |
|
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
|
5 |
|
public function add(string $word): Query |
|
182
|
|
|
{ |
|
183
|
5 |
|
$this->_attributes[] = $word; |
|
184
|
5 |
|
return $this; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Get attributes array of current query |
|
189
|
|
|
* |
|
190
|
|
|
* @return array |
|
191
|
|
|
*/ |
|
192
|
19 |
|
public function getAttributes(): array |
|
193
|
|
|
{ |
|
194
|
19 |
|
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
|
27 |
|
public function setAttributes(array $attributes): Query |
|
206
|
|
|
{ |
|
207
|
27 |
|
$this->_attributes = $attributes; |
|
208
|
27 |
|
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
|
27 |
|
public function setEndpoint(string $endpoint = null): Query |
|
230
|
|
|
{ |
|
231
|
27 |
|
$this->_endpoint = $endpoint; |
|
232
|
27 |
|
return $this; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Build body of query |
|
237
|
|
|
* |
|
238
|
|
|
* @return array |
|
239
|
|
|
* @throws \RouterOS\Exceptions\QueryException |
|
240
|
|
|
*/ |
|
241
|
16 |
|
public function getQuery(): array |
|
242
|
|
|
{ |
|
243
|
16 |
|
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
|
15 |
|
$attributes = $this->getAttributes(); |
|
249
|
15 |
|
array_unshift($attributes, $this->_endpoint); |
|
250
|
|
|
|
|
251
|
|
|
// If operations is set then add to query |
|
252
|
15 |
|
if (is_string($this->_operations) && !empty($this->_operations)) { |
|
253
|
1 |
|
$attributes[] = $this->_operations; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
// If tag is set then added to query |
|
257
|
15 |
|
if (is_string($this->_tag) && !empty($this->_tag)) { |
|
258
|
1 |
|
$attributes[] = $this->_tag; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
15 |
|
return $attributes; |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|