1 | <?php |
||
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 |
|
62 | |||
63 | /** |
||
64 | * Get attributes array of current query |
||
65 | * |
||
66 | * @return array |
||
67 | */ |
||
68 | 10 | public function getAttributes(): array |
|
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 |
|
85 | |||
86 | /** |
||
87 | * Get endpoint of current query |
||
88 | * |
||
89 | * @return string|null |
||
90 | */ |
||
91 | 10 | public function getEndpoint() |
|
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 |
|
108 | |||
109 | /** |
||
110 | * Build body of query |
||
111 | * |
||
112 | * @return array |
||
113 | * @throws \RouterOS\Exceptions\QueryException |
||
114 | */ |
||
115 | 7 | public function getQuery(): array |
|
127 | } |
||
128 |