Completed
Push — master ( 07aad8...237a74 )
by Mr
09:22
created

Query::operations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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