Completed
Push — master ( c4a020...d491e3 )
by Mr
03:42
created

Query::where()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 12
cts 12
cp 1
rs 8.5546
c 0
b 0
f 0
cc 7
nc 10
nop 3
crap 7
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
     * @throws \RouterOS\Exceptions\QueryException
61
     */
62 31
    public function __construct($endpoint, array $attributes = [])
63
    {
64 31
        if (\is_string($endpoint)) {
65 27
            $this->setEndpoint($endpoint);
66 27
            $this->setAttributes($attributes);
67 4
        } elseif (\is_array($endpoint)) {
68 3
            $query = array_shift($endpoint);
69 3
            $this->setEndpoint($query);
70 3
            $this->setAttributes($endpoint);
71
        } else {
72 1
            throw new QueryException('Specified endpoint is incorrect');
73
        }
74 30
    }
75
76
    /**
77
     * Where logic of query
78
     *
79
     * @param string          $key      Key which need to find
80
     * @param bool|string|int $value    Value which need to check (by default true)
81
     * @param bool|string|int $operator It may be one from list [-,=,>,<]
82
     * @return \RouterOS\Query
83
     * @throws \RouterOS\Exceptions\QueryException
84
     * @since 1.0.0
85
     */
86 5
    public function where(string $key, $operator = null, $value = null): self
87
    {
88 5
        if (null !== $operator && null === $value) {
89
90
            // Client may set only two parameters, that mean what $operator is $value
91 4
            $value = $operator;
92
93
            // And operator should be "="
94 4
            $operator = null;
95
        }
96
97 5
        if (null !== $operator && null !== $value) {
98
            // If operator is available in list
99 2
            if (\in_array($operator, self::AVAILABLE_OPERATORS, true)) {
100 1
                $key = $operator . $key;
101
            } else {
102 1
                throw new QueryException('Operator "' . $operator . '" in not in allowed list [' . implode(',', self::AVAILABLE_OPERATORS) . ']');
103
            }
104
        }
105
106 4
        if (null !== $value) {
107 4
            $value = '=' . $value;
108
        }
109
110 4
        $this->add('?' . $key . $value);
111 4
        return $this;
112
    }
113
114
    /**
115
     * Append additional operations
116
     *
117
     * @param string $operations
118
     * @return \RouterOS\Query
119
     * @since 1.0.0
120
     */
121 2
    public function operations(string $operations): self
122
    {
123 2
        $this->_operations = '?#' . $operations;
124 2
        return $this;
125
    }
126
127
    /**
128
     * Append tag to query (it should be at end)
129
     *
130
     * @param string $name
131
     * @return \RouterOS\Query
132
     * @since 1.0.0
133
     */
134 2
    public function tag(string $name): self
135
    {
136 2
        $this->_tag = '.tag=' . $name;
137 2
        return $this;
138
    }
139
140
    /**
141
     * Append to array yet another attribute of query
142
     *
143
     * @param string $word
144
     * @return \RouterOS\Query
145
     */
146 6
    public function add(string $word): Query
147
    {
148 6
        $this->_attributes[] = $word;
149 6
        return $this;
150
    }
151
152
    /**
153
     * Get attributes array of current query
154
     *
155
     * @return array
156
     */
157 22
    public function getAttributes(): array
158
    {
159 22
        return $this->_attributes;
160
    }
161
162
    /**
163
     * Set array of attributes
164
     *
165
     * @param array $attributes
166
     * @return \RouterOS\Query
167
     * @since 0.7
168
     */
169 30
    public function setAttributes(array $attributes): Query
170
    {
171 30
        $this->_attributes = $attributes;
172 30
        return $this;
173
    }
174
175
    /**
176
     * Get endpoint of current query
177
     *
178
     * @return string|null
179
     */
180 3
    public function getEndpoint()
181
    {
182 3
        return $this->_endpoint;
183
    }
184
185
    /**
186
     * Set endpoint of query
187
     *
188
     * @param string|null $endpoint
189
     * @return \RouterOS\Query
190
     * @since 0.7
191
     */
192 30
    public function setEndpoint(string $endpoint = null): Query
193
    {
194 30
        $this->_endpoint = $endpoint;
195 30
        return $this;
196
    }
197
198
    /**
199
     * Build body of query
200
     *
201
     * @return array
202
     * @throws \RouterOS\Exceptions\QueryException
203
     */
204 19
    public function getQuery(): array
205
    {
206 19
        if ($this->_endpoint === null) {
207 1
            throw new QueryException('Endpoint of query is not set');
208
        }
209
210
        // Get all attributes and prepend endpoint to this list
211 18
        $attributes = $this->getAttributes();
212 18
        array_unshift($attributes, $this->_endpoint);
213
214
        // If operations is set then add to query
215 18
        if (is_string($this->_operations) && !empty($this->_operations)) {
216 2
            $attributes[] = $this->_operations;
217
        }
218
219
        // If tag is set then added to query
220 18
        if (is_string($this->_tag) && !empty($this->_tag)) {
221 2
            $attributes[] = $this->_tag;
222
        }
223
224 18
        return $attributes;
225
    }
226
}
227