1 | <?php |
||
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 = []) |
|
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 |
|
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 |
||
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 |
|
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 |
|
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 |
|
186 | |||
187 | /** |
||
188 | * Get attributes array of current query |
||
189 | * |
||
190 | * @return array |
||
191 | */ |
||
192 | 19 | public function getAttributes(): array |
|
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 |
|
210 | |||
211 | /** |
||
212 | * Get endpoint of current query |
||
213 | * |
||
214 | * @return string|null |
||
215 | */ |
||
216 | 3 | public function getEndpoint() |
|
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 |
|
234 | |||
235 | /** |
||
236 | * Build body of query |
||
237 | * |
||
238 | * @return array |
||
239 | * @throws \RouterOS\Exceptions\QueryException |
||
240 | */ |
||
241 | 16 | public function getQuery(): array |
|
263 | } |
||
264 |