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
|
|
|
* Query constructor. |
47
|
|
|
* |
48
|
|
|
* @param array|string $endpoint Path of endpoint |
49
|
|
|
* @param array $attributes List of attributes which should be set |
50
|
|
|
* @throws QueryException |
51
|
|
|
*/ |
52
|
23 |
|
public function __construct($endpoint, array $attributes = []) |
53
|
|
|
{ |
54
|
23 |
|
if (\is_string($endpoint)) { |
55
|
19 |
|
$this->setEndpoint($endpoint); |
56
|
19 |
|
$this->setAttributes($attributes); |
57
|
4 |
|
} elseif (\is_array($endpoint)) { |
58
|
3 |
|
$query = array_shift($endpoint); |
59
|
3 |
|
$this->setEndpoint($query); |
60
|
3 |
|
$this->setAttributes($endpoint); |
61
|
|
|
} else { |
62
|
1 |
|
throw new QueryException('Specified endpoint is not correct'); |
63
|
|
|
} |
64
|
22 |
|
} |
65
|
|
|
|
66
|
|
|
const AVAILABLE_OPERATORS = [ |
67
|
|
|
'-', // Does not have |
68
|
|
|
'=', // Equal |
69
|
|
|
'>', // More than |
70
|
|
|
'<' // Less than |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Where logic of query |
75
|
|
|
* |
76
|
|
|
* @param string $key |
77
|
|
|
* @param bool $value |
78
|
|
|
* @param string|null $operator |
79
|
|
|
* @return \RouterOS\Query |
80
|
|
|
* @throws \RouterOS\Exceptions\ClientException |
81
|
|
|
* @since 1.0.0 |
82
|
|
|
*/ |
83
|
|
|
public function where(string $key, $value = true, string $operator = ''): self |
84
|
|
|
{ |
85
|
|
|
if (!empty($operator)) { |
86
|
|
|
// If operator is available in list |
87
|
|
|
if (\in_array($operator, self::AVAILABLE_OPERATORS, true)) { |
88
|
|
|
// Overwrite key |
89
|
|
|
$key = $operator . $key; |
90
|
|
|
} else { |
91
|
|
|
throw new ClientException('Operator "' . $operator . '" in not in allowed list [' . implode(',', self::AVAILABLE_OPERATORS) . ']'); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->add('?' . $key . '=' . $value); |
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Append additional operations |
101
|
|
|
* |
102
|
|
|
* @param string $operations |
103
|
|
|
* @return \RouterOS\Query |
104
|
|
|
* @since 1.0.0 |
105
|
|
|
*/ |
106
|
|
|
public function operations(string $operations): self |
107
|
|
|
{ |
108
|
|
|
$this->_operations = '?#' . $operations; |
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Append tag to query (it should be at end) |
114
|
|
|
* |
115
|
|
|
* @param string $name |
116
|
|
|
* @return \RouterOS\Query |
117
|
|
|
* @since 1.0.0 |
118
|
|
|
*/ |
119
|
|
|
public function tag(string $name): self |
120
|
|
|
{ |
121
|
|
|
$this->_tag = '.tag=' . $name; |
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Append to array yet another attribute of query |
127
|
|
|
* |
128
|
|
|
* @param string $word |
129
|
|
|
* @return \RouterOS\Query |
130
|
|
|
*/ |
131
|
2 |
|
public function add(string $word): Query |
132
|
|
|
{ |
133
|
2 |
|
$this->_attributes[] = $word; |
134
|
2 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get attributes array of current query |
139
|
|
|
* |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
15 |
|
public function getAttributes(): array |
143
|
|
|
{ |
144
|
15 |
|
return $this->_attributes; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set array of attributes |
149
|
|
|
* |
150
|
|
|
* @param array $attributes |
151
|
|
|
* @return \RouterOS\Query |
152
|
|
|
* @since 0.7 |
153
|
|
|
*/ |
154
|
22 |
|
public function setAttributes(array $attributes): Query |
155
|
|
|
{ |
156
|
22 |
|
$this->_attributes = $attributes; |
157
|
22 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get endpoint of current query |
162
|
|
|
* |
163
|
|
|
* @return string|null |
164
|
|
|
*/ |
165
|
3 |
|
public function getEndpoint() |
166
|
|
|
{ |
167
|
3 |
|
return $this->_endpoint; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Set endpoint of query |
172
|
|
|
* |
173
|
|
|
* @param string|null $endpoint |
174
|
|
|
* @return \RouterOS\Query |
175
|
|
|
* @since 0.7 |
176
|
|
|
*/ |
177
|
22 |
|
public function setEndpoint(string $endpoint = null): Query |
178
|
|
|
{ |
179
|
22 |
|
$this->_endpoint = $endpoint; |
180
|
22 |
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Build body of query |
185
|
|
|
* |
186
|
|
|
* @return array |
187
|
|
|
* @throws \RouterOS\Exceptions\QueryException |
188
|
|
|
*/ |
189
|
13 |
|
public function getQuery(): array |
190
|
|
|
{ |
191
|
13 |
|
if ($this->_endpoint === null) { |
192
|
1 |
|
throw new QueryException('Endpoint of query is not set'); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// Get all attributes and prepend endpoint to this list |
196
|
12 |
|
$attributes = $this->getAttributes(); |
197
|
12 |
|
array_unshift($attributes, $this->_endpoint); |
198
|
|
|
|
199
|
|
|
// If operations is set then add to query |
200
|
12 |
|
if (is_string($this->_operations) && !empty($this->_operations)) { |
201
|
|
|
$attributes[] = $this->_operations; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// If tag is set then added to query |
205
|
12 |
|
if (is_string($this->_tag) && !empty($this->_tag)) { |
206
|
|
|
$attributes[] = $this->_tag; |
207
|
|
|
} |
208
|
|
|
|
209
|
12 |
|
return $attributes; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|