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
|
|
|
'user_agent', |
14
|
|
|
'base_uri', |
15
|
|
|
'api_key', |
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_uri', |
29
|
|
|
'timeout', |
30
|
|
|
'api_key', |
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
|
|
|
* @throws \ErrorException |
45
|
|
|
*/ |
46
|
15 |
|
public function __construct(array $parameters = []) |
47
|
|
|
{ |
48
|
|
|
// Set default parameters of client |
49
|
15 |
|
$this->_parameters = [ |
50
|
|
|
// Errors must be disabled by default, because we need to get error codes |
51
|
|
|
// @link http://docs.guzzlephp.org/en/stable/request-options.html#http-errors |
52
|
|
|
'http_errors' => false, |
53
|
|
|
|
54
|
|
|
// Wrapper settings |
55
|
|
|
'tries' => 2, // Count of tries |
56
|
|
|
'seconds' => 10, // Waiting time per each try |
57
|
|
|
|
58
|
|
|
// Optional parameters |
59
|
|
|
'debug' => false, |
60
|
|
|
'track_redirects' => false, |
61
|
|
|
|
62
|
|
|
// Main parameters |
63
|
|
|
'timeout' => 20, |
64
|
|
|
'user_agent' => 'Resova PHP Client', |
65
|
|
|
'base_uri' => 'https://api.resova.eu/v1' |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
// Overwrite parameters by client input |
69
|
15 |
|
foreach ($parameters as $key => $value) { |
70
|
11 |
|
$this->set($key, $value); |
71
|
|
|
} |
72
|
14 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Magic setter parameter by name |
76
|
|
|
* |
77
|
|
|
* @param string $name Name of parameter |
78
|
|
|
* @param string|bool|int|null $value Value of parameter |
79
|
|
|
* |
80
|
|
|
* @throws \ErrorException |
81
|
|
|
*/ |
82
|
1 |
|
public function __set(string $name, $value) |
83
|
|
|
{ |
84
|
1 |
|
$this->set($name, $value); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Check if parameter if available |
89
|
|
|
* |
90
|
|
|
* @param string $name Name of parameter |
91
|
|
|
* |
92
|
|
|
* @return bool |
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 ErrorException |
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 ErrorException |
130
|
|
|
*/ |
131
|
12 |
|
public function set(string $name, $value): self |
132
|
|
|
{ |
133
|
12 |
|
if (!\in_array($name, self::ALLOWED, false)) { |
134
|
1 |
|
throw new \ErrorException("Parameter \"$name\" is not in available list [" . implode(',', self::ALLOWED) . ']'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// Add parameters into array |
138
|
11 |
|
$this->_parameters[$name] = $value; |
139
|
11 |
|
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 ErrorException |
149
|
|
|
*/ |
150
|
12 |
|
public function get(string $name) |
151
|
|
|
{ |
152
|
12 |
|
if (!isset($this->_parameters[$name])) { |
153
|
1 |
|
throw new \ErrorException("Parameter \"$name\" is not in set"); |
154
|
|
|
} |
155
|
|
|
|
156
|
12 |
|
return $this->_parameters[$name]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get all available parameters |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
1 |
|
public function all(): array |
165
|
|
|
{ |
166
|
1 |
|
return $this->_parameters; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Return all ready for Guzzle parameters |
171
|
|
|
* |
172
|
|
|
* @return array |
173
|
|
|
* @throws ErrorException |
174
|
|
|
*/ |
175
|
11 |
|
public function guzzle(): array |
176
|
|
|
{ |
177
|
|
|
return [ |
178
|
|
|
// 'base_uri' => $this->get('base_uri'), // By some reasons base_uri option is not work anymore |
179
|
11 |
|
'timeout' => $this->get('timeout'), |
180
|
11 |
|
'track_redirects' => $this->get('track_redirects'), |
181
|
11 |
|
'debug' => $this->get('debug'), |
182
|
|
|
'headers' => [ |
183
|
11 |
|
'User-Agent' => $this->get('user_agent'), |
184
|
11 |
|
'X-API-KEY' => $this->get('api_key'), |
185
|
|
|
] |
186
|
|
|
]; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|