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
|
|
|
* Work mode of return |
23
|
|
|
* |
24
|
|
|
* @deprecated |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
private $_return_object = false; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Config constructor. |
31
|
|
|
* |
32
|
|
|
* @param array $parameters List of parameters which can be set on object creation stage |
33
|
|
|
* @throws \UON\Exceptions\ConfigException |
34
|
|
|
* @since 1.9 |
35
|
|
|
*/ |
36
|
|
|
public function __construct(array $parameters = []) |
37
|
|
|
{ |
38
|
|
|
foreach ($parameters as $key => $value) { |
39
|
|
|
$this->set($key, $value); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get return type (object by default) |
45
|
|
|
* |
46
|
|
|
* @return bool |
47
|
|
|
* @deprecated |
48
|
|
|
*/ |
49
|
|
|
public function isObject() |
50
|
|
|
{ |
51
|
|
|
return $this->_return_object; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set work mode (object => true, array => false) |
56
|
|
|
* |
57
|
|
|
* @param bool $object |
58
|
|
|
* @return \UON\Interfaces\ConfigInterface |
59
|
|
|
* @deprecated |
60
|
|
|
*/ |
61
|
|
|
public function setReturn($object = true) |
62
|
|
|
{ |
63
|
|
|
$this->_return_object = $object; |
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Set parameter by name |
69
|
|
|
* |
70
|
|
|
* @param string $name |
71
|
|
|
* @param string|bool|int $value |
72
|
|
|
* @return \UON\Interfaces\ConfigInterface |
73
|
|
|
* @throws \UON\Exceptions\ConfigException |
74
|
|
|
*/ |
75
|
|
|
public function set($name, $value) |
76
|
|
|
{ |
77
|
|
|
if (!\in_array($name, $this->getAllowed(), false)) { |
78
|
|
|
throw new ConfigException("Parameter \"$name\" is not in available list [" . implode(',', $this->getAllowed()) . ']'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Add parameters into array |
82
|
|
|
$this->_parameters[$name] = $value; |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get all available parameters on only one |
88
|
|
|
* |
89
|
|
|
* @param string $name |
90
|
|
|
* @return string|bool|int |
91
|
|
|
*/ |
92
|
|
|
public function get($name) |
93
|
|
|
{ |
94
|
|
|
return isset($this->_parameters[$name]) |
95
|
|
|
? $this->_parameters[$name] |
96
|
|
|
: false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return all allowed parameters |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public function getAllowed() |
105
|
|
|
{ |
106
|
|
|
return self::ALLOWED; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return all preconfigured parameters |
111
|
|
|
* |
112
|
|
|
* @param bool $ignore Ignore parameters which is not important for client |
113
|
|
|
* @param array $ignore_items Which items should be excluded from array |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
public function getParameters($ignore = false, array $ignore_items = ['token', 'tries', 'seconds']) |
117
|
|
|
{ |
118
|
|
|
$parameters = $this->_parameters; |
119
|
|
|
// Remove ignored items from array |
120
|
|
|
if ($ignore) { |
121
|
|
|
foreach ($parameters as $name => $value) { |
122
|
|
|
if (in_array($name, $ignore_items, false)) { |
123
|
|
|
unset($parameters[$name]); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
return $parameters; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|