1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RouterOS; |
4
|
|
|
|
5
|
|
|
use RouterOS\Exceptions\QueryException; |
6
|
|
|
use RouterOS\Interfaces\QueryInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Query for building queries |
10
|
|
|
* |
11
|
|
|
* @package RouterOS |
12
|
|
|
* @since 0.1 |
13
|
|
|
*/ |
14
|
|
|
class Query implements QueryInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Array of query attributes |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $_attributes = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Endpoint of query |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $_endpoint; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Query constructor. |
32
|
|
|
* |
33
|
|
|
* @param array|string $endpoint Path of endpoint |
34
|
|
|
* @param array $attributes List of attributes which should be set |
35
|
|
|
* @throws QueryException |
36
|
|
|
*/ |
37
|
17 |
|
public function __construct($endpoint, array $attributes = []) |
38
|
|
|
{ |
39
|
17 |
|
if (\is_string($endpoint)) { |
40
|
14 |
|
$this->setEndpoint($endpoint); |
41
|
14 |
|
$this->setAttributes($attributes); |
42
|
3 |
|
} elseif (\is_array($endpoint)) { |
43
|
2 |
|
$query = array_shift($endpoint); |
44
|
2 |
|
$this->setEndpoint($query); |
45
|
2 |
|
$this->setAttributes($endpoint); |
46
|
|
|
} else { |
47
|
1 |
|
throw new QueryException('Specified endpoint is not correct'); |
48
|
|
|
} |
49
|
16 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Append to array yet another attribute of query |
53
|
|
|
* |
54
|
|
|
* @param string $word |
55
|
|
|
* @return \RouterOS\Query |
56
|
|
|
*/ |
57
|
8 |
|
public function add(string $word): Query |
58
|
|
|
{ |
59
|
8 |
|
$this->_attributes[] = $word; |
60
|
8 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get attributes array of current query |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
10 |
|
public function getAttributes(): array |
69
|
|
|
{ |
70
|
10 |
|
return $this->_attributes; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set array of attributes |
75
|
|
|
* |
76
|
|
|
* @param array $attributes |
77
|
|
|
* @since 0.7 |
78
|
|
|
* @return \RouterOS\Query |
79
|
|
|
*/ |
80
|
16 |
|
public function setAttributes(array $attributes): Query |
81
|
|
|
{ |
82
|
16 |
|
$this->_attributes = $attributes; |
83
|
16 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get endpoint of current query |
88
|
|
|
* |
89
|
|
|
* @return string|null |
90
|
|
|
*/ |
91
|
10 |
|
public function getEndpoint() |
92
|
|
|
{ |
93
|
10 |
|
return $this->_endpoint; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set endpoint of query |
98
|
|
|
* |
99
|
|
|
* @param string|null $endpoint |
100
|
|
|
* @since 0.7 |
101
|
|
|
* @return \RouterOS\Query |
102
|
|
|
*/ |
103
|
16 |
|
public function setEndpoint(string $endpoint = null): Query |
104
|
|
|
{ |
105
|
16 |
|
$this->_endpoint = $endpoint; |
106
|
16 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Build body of query |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
* @throws \RouterOS\Exceptions\QueryException |
114
|
|
|
*/ |
115
|
7 |
|
public function getQuery(): array |
116
|
|
|
{ |
117
|
7 |
|
if ($this->getEndpoint() === null) { |
118
|
|
|
throw new QueryException('Endpoint of query is not set'); |
119
|
|
|
} |
120
|
|
|
|
121
|
7 |
|
$endpoint = $this->getEndpoint(); |
122
|
7 |
|
$attributes = $this->getAttributes(); |
123
|
7 |
|
array_unshift($attributes, $endpoint); |
124
|
|
|
|
125
|
7 |
|
return $attributes; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|