Completed
Pull Request — master (#40)
by Mr
06:35
created

Query::operations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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