1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PiHole; |
4
|
|
|
|
5
|
|
|
class Config |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* List of allowed parameters |
9
|
|
|
*/ |
10
|
|
|
public const ALLOWED = [ |
11
|
|
|
'webpassword', |
12
|
|
|
'json_force_object', |
13
|
|
|
'proxy', |
14
|
|
|
'base_url', |
15
|
|
|
'user_agent', |
16
|
|
|
'timeout', |
17
|
|
|
'tries', |
18
|
|
|
'seconds', |
19
|
|
|
'debug', |
20
|
|
|
'track_redirects', |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* List of minimal required parameters |
25
|
|
|
*/ |
26
|
|
|
public const REQUIRED = [ |
27
|
|
|
'user_agent', |
28
|
|
|
'base_url', |
29
|
|
|
'timeout', |
30
|
|
|
'webpassword', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* List of configured parameters |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $_parameters; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Config constructor. |
42
|
|
|
* |
43
|
|
|
* @param array $parameters List of parameters which can be set on object creation stage |
44
|
|
|
* |
45
|
|
|
* @throws \InvalidArgumentException |
46
|
|
|
*/ |
47
|
|
|
public function __construct(array $parameters = []) |
48
|
|
|
{ |
49
|
|
|
// Set default parameters of client |
50
|
|
|
$this->_parameters = [ |
51
|
|
|
// Errors must be disabled by default, because we need to get error codes |
52
|
|
|
// @link http://docs.guzzlephp.org/en/stable/request-options.html#http-errors |
53
|
|
|
'http_errors' => false, |
54
|
|
|
|
55
|
|
|
// Wrapper settings |
56
|
|
|
'tries' => 2, // Count of tries |
57
|
|
|
'seconds' => 10, // Waiting time per each try |
58
|
|
|
|
59
|
|
|
// Optional parameters |
60
|
|
|
'debug' => false, |
61
|
|
|
'track_redirects' => false, |
62
|
|
|
|
63
|
|
|
// Main parameters |
64
|
|
|
'json_force_object' => false, |
65
|
|
|
'timeout' => 20, |
66
|
|
|
'user_agent' => 'Pi-Hole PHP Client', |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
// Overwrite parameters by client input |
70
|
|
|
foreach ($parameters as $key => $value) { |
71
|
|
|
$this->set($key, $value); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Magic setter parameter by name |
77
|
|
|
* |
78
|
|
|
* @param string $name Name of parameter |
79
|
|
|
* @param string|bool|int|null $value Value of parameter |
80
|
|
|
*/ |
81
|
|
|
public function __set(string $name, $value) |
82
|
|
|
{ |
83
|
|
|
$this->set($name, $value); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Check if parameter if available |
88
|
|
|
* |
89
|
|
|
* @param string $name Name of parameter |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
* @throws \InvalidArgumentException |
93
|
|
|
*/ |
94
|
|
|
public function __isset($name): bool |
95
|
|
|
{ |
96
|
|
|
return isset($this->_parameters[$name]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get parameter via magic call |
101
|
|
|
* |
102
|
|
|
* @param string $name Name of parameter |
103
|
|
|
* |
104
|
|
|
* @return bool|int|string|null |
105
|
|
|
* @throws \InvalidArgumentException |
106
|
|
|
*/ |
107
|
|
|
public function __get($name) |
108
|
|
|
{ |
109
|
|
|
return $this->get($name); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Remove parameter from array |
114
|
|
|
* |
115
|
|
|
* @param string $name Name of parameter |
116
|
|
|
*/ |
117
|
|
|
public function __unset($name) |
118
|
|
|
{ |
119
|
|
|
unset($this->_parameters[$name]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set parameter by name |
124
|
|
|
* |
125
|
|
|
* @param string $name Name of parameter |
126
|
|
|
* @param string|bool|int|null $value Value of parameter |
127
|
|
|
* |
128
|
|
|
* @return $this |
129
|
|
|
* @throws \InvalidArgumentException |
130
|
|
|
*/ |
131
|
|
|
public function set(string $name, $value): self |
132
|
|
|
{ |
133
|
|
|
if (!\in_array($name, self::ALLOWED, false)) { |
134
|
|
|
throw new \InvalidArgumentException("Parameter \"$name\" is not in available list [" . implode(',', self::ALLOWED) . ']'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// Add parameters into array |
138
|
|
|
$this->_parameters[$name] = $value; |
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get available parameter by name |
144
|
|
|
* |
145
|
|
|
* @param string $name Name of parameter |
146
|
|
|
* |
147
|
|
|
* @return bool|int|string|null |
148
|
|
|
* @throws \InvalidArgumentException |
149
|
|
|
*/ |
150
|
|
|
public function get(string $name) |
151
|
|
|
{ |
152
|
|
|
if (!isset($this->_parameters[$name])) { |
153
|
|
|
throw new \InvalidArgumentException("Parameter \"$name\" is not in set"); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $this->_parameters[$name]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get all available parameters |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
public function all(): array |
165
|
|
|
{ |
166
|
|
|
return $this->_parameters; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Generate query by parameters |
171
|
|
|
* |
172
|
|
|
* @param array $url |
173
|
|
|
* @param bool $auth |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public function getBaseUrl(array $url = [], bool $auth = false): string |
178
|
|
|
{ |
179
|
|
|
if ($auth) { |
180
|
|
|
$url['auth'] = $this->get('webpassword'); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
if ($this->get('json_force_object')) { |
184
|
|
|
$url['jsonForceObject'] = null; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $this->get('base_url') . '?' . http_build_query($url); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Return all ready for Guzzle parameters |
192
|
|
|
* |
193
|
|
|
* @return array |
194
|
|
|
*/ |
195
|
|
|
public function guzzle(): array |
196
|
|
|
{ |
197
|
|
|
$options = [ |
198
|
|
|
'timeout' => $this->get('timeout'), |
199
|
|
|
'track_redirects' => $this->get('track_redirects'), |
200
|
|
|
'debug' => $this->get('debug'), |
201
|
|
|
'headers' => [ |
202
|
|
|
'User-Agent' => $this->get('user_agent'), |
203
|
|
|
] |
204
|
|
|
]; |
205
|
|
|
|
206
|
|
|
// Proxy is optional |
207
|
|
|
if (isset($this->proxy)) { |
208
|
|
|
$options['proxy'] = $this->proxy; |
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $options; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.