1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UON; |
4
|
|
|
|
5
|
|
|
use UON\Exceptions\ConfigException; |
6
|
|
|
use UON\Interfaces\ConfigInterface; |
7
|
|
|
|
8
|
|
|
class Config implements ConfigInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* List of configured parameters |
12
|
|
|
* |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private $_parameters = [ |
16
|
|
|
// Errors must be disabled by default, because we need to get error codes |
17
|
|
|
// @link http://docs.guzzlephp.org/en/stable/request-options.html#http-errors |
18
|
|
|
'http_errors' => false |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Config constructor. |
23
|
|
|
* |
24
|
|
|
* @param array $parameters List of parameters which can be set on object creation stage |
25
|
|
|
* @throws \UON\Exceptions\ConfigException |
26
|
|
|
* @since 1.9 |
27
|
|
|
*/ |
28
|
|
|
public function __construct(array $parameters = []) |
29
|
|
|
{ |
30
|
|
|
foreach ($parameters as $key => $value) { |
31
|
|
|
$this->set($key, $value); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Set parameter by name |
37
|
|
|
* |
38
|
|
|
* @param string $name |
39
|
|
|
* @param string|bool|int $value |
40
|
|
|
* @return \UON\Interfaces\ConfigInterface |
41
|
|
|
* @throws \UON\Exceptions\ConfigException |
42
|
|
|
*/ |
43
|
|
|
public function set($name, $value) |
44
|
|
|
{ |
45
|
|
|
if (!\in_array($name, $this->getAllowed(), false)) { |
46
|
|
|
throw new ConfigException("Parameter \"$name\" is not in available list [" . implode(',', $this->getAllowed()) . ']'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// Add parameters into array |
50
|
|
|
$this->_parameters[$name] = $value; |
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get all available parameters on only one |
56
|
|
|
* |
57
|
|
|
* @param string $name |
58
|
|
|
* @return string|bool|int |
59
|
|
|
*/ |
60
|
2 |
|
public function get($name) |
61
|
|
|
{ |
62
|
2 |
|
return isset($this->_parameters[$name]) |
63
|
2 |
|
? $this->_parameters[$name] |
64
|
2 |
|
: false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Return all allowed parameters |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public function getAllowed() |
73
|
|
|
{ |
74
|
|
|
return self::ALLOWED; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Return all preconfigured parameters |
79
|
|
|
* |
80
|
|
|
* @param bool $ignore Ignore parameters which is not important for client |
81
|
|
|
* @param array $ignore_items Which items should be excluded from array |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
2 |
|
public function getParameters($ignore = false, array $ignore_items = ['token', 'tries', 'seconds', 'maxRequestTimeout']) |
85
|
|
|
{ |
86
|
2 |
|
$parameters = $this->_parameters; |
87
|
|
|
// Remove ignored items from array |
88
|
2 |
|
if ($ignore) { |
89
|
2 |
|
foreach ($parameters as $name => $value) { |
90
|
2 |
|
if (in_array($name, $ignore_items, false)) { |
91
|
2 |
|
unset($parameters[$name]); |
92
|
2 |
|
} |
93
|
2 |
|
} |
94
|
2 |
|
} |
95
|
2 |
|
return $parameters; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|