Completed
Push — master ( 6854cf...239ab7 )
by Mr
03:34
created

Query::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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