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