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