1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Resova; |
4
|
|
|
|
5
|
|
|
class Config |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* List of allowed parameters |
9
|
|
|
*/ |
10
|
|
|
public const ALLOWED = [ |
11
|
|
|
'user_agent', |
12
|
|
|
'base_uri', |
13
|
|
|
'api_key', |
14
|
|
|
'timeout', |
15
|
|
|
'tries', |
16
|
|
|
'seconds', |
17
|
|
|
'debug', |
18
|
|
|
'track_redirects' |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* List of minimal required parameters |
23
|
|
|
*/ |
24
|
|
|
public const REQUIRED = [ |
25
|
|
|
'user_agent', |
26
|
|
|
'base_uri', |
27
|
|
|
'timeout', |
28
|
|
|
'api_key', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* List of configured parameters |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $_parameters; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Config constructor. |
40
|
|
|
* |
41
|
|
|
* @param array $parameters List of parameters which can be set on object creation stage |
42
|
|
|
* @throws \ErrorException |
43
|
|
|
*/ |
44
|
15 |
|
public function __construct(array $parameters = []) |
45
|
|
|
{ |
46
|
|
|
// Set default parameters of client |
47
|
15 |
|
$this->_parameters = [ |
48
|
|
|
// Errors must be disabled by default, because we need to get error codes |
49
|
|
|
// @link http://docs.guzzlephp.org/en/stable/request-options.html#http-errors |
50
|
|
|
'http_errors' => false, |
51
|
|
|
|
52
|
|
|
// Wrapper settings |
53
|
|
|
'tries' => 2, // Count of tries |
54
|
|
|
'seconds' => 10, // Waiting time per each try |
55
|
|
|
|
56
|
|
|
// Optional parameters |
57
|
|
|
'debug' => false, |
58
|
|
|
'track_redirects' => false, |
59
|
|
|
|
60
|
|
|
// Main parameters |
61
|
|
|
'timeout' => 20, |
62
|
|
|
'user_agent' => 'Resova PHP Client', |
63
|
|
|
'base_uri' => 'https://api.resova.eu/v1' |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
// Overwrite parameters by client input |
67
|
15 |
|
foreach ($parameters as $key => $value) { |
68
|
11 |
|
$this->set($key, $value); |
69
|
|
|
} |
70
|
14 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Set parameter by name |
74
|
|
|
* |
75
|
|
|
* @param string $name Name of parameter |
76
|
|
|
* @param string|bool|int|null $value Value of parameter |
77
|
|
|
* @return \Resova\Config |
78
|
|
|
* @throws \ErrorException |
79
|
|
|
*/ |
80
|
11 |
|
private function set($name, $value): self |
81
|
|
|
{ |
82
|
11 |
|
if (!\in_array($name, self::ALLOWED, false)) { |
83
|
1 |
|
throw new \ErrorException("Parameter \"$name\" is not in available list [" . implode(',', self::ALLOWED) . ']'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Add parameters into array |
87
|
10 |
|
$this->_parameters[$name] = $value; |
88
|
10 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get available parameter by name |
93
|
|
|
* |
94
|
|
|
* @param string $name |
95
|
|
|
* @return string|bool|int|null |
96
|
|
|
*/ |
97
|
12 |
|
public function get(string $name) |
98
|
|
|
{ |
99
|
12 |
|
return $this->_parameters[$name] ?? null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get all available parameters |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
1 |
|
public function all(): array |
108
|
|
|
{ |
109
|
1 |
|
return $this->_parameters; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Return all ready for Guzzle parameters |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
11 |
|
public function guzzle(): array |
118
|
|
|
{ |
119
|
|
|
return [ |
120
|
|
|
// 'base_uri' => $this->get('base_uri'), // By some reasons base_uri option is not work anymore |
121
|
11 |
|
'timeout' => $this->get('timeout'), |
122
|
11 |
|
'track_redirects' => $this->get('track_redirects'), |
123
|
11 |
|
'debug' => $this->get('debug'), |
124
|
|
|
'headers' => [ |
125
|
11 |
|
'User-Agent' => $this->get('user_agent'), |
126
|
11 |
|
'X-API-KEY' => $this->get('api_key'), |
127
|
|
|
] |
128
|
|
|
]; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|