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