Completed
Push — master ( 239ab7...dffbf0 )
by Mr
03:43
created

Query::where()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

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