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