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