1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Stdlib |
5
|
|
|
* |
6
|
|
|
* Platine Stdlib is a the collection of frequently used php features |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2020 Platine Stdlib |
11
|
|
|
* |
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
14
|
|
|
* in the Software without restriction, including without limitation the rights |
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
17
|
|
|
* furnished to do so, subject to the following conditions: |
18
|
|
|
* |
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
20
|
|
|
* copies or substantial portions of the Software. |
21
|
|
|
* |
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
28
|
|
|
* SOFTWARE. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @file AbstractConfiguration.php |
33
|
|
|
* |
34
|
|
|
* The base class for application |
35
|
|
|
* |
36
|
|
|
* @package Platine\Stdlib\Config |
37
|
|
|
* @author Platine Developers Team |
38
|
|
|
* @copyright Copyright (c) 2020 |
39
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
40
|
|
|
* @link https://www.platine-php.com |
41
|
|
|
* @version 1.0.0 |
42
|
|
|
* @filesource |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
declare(strict_types=1); |
46
|
|
|
|
47
|
|
|
namespace Platine\Stdlib\Config; |
48
|
|
|
|
49
|
|
|
use Error; |
50
|
|
|
use InvalidArgumentException; |
51
|
|
|
use Platine\Stdlib\Contract\ConfigurationInterface; |
52
|
|
|
use Platine\Stdlib\Helper\Arr; |
53
|
|
|
use Platine\Stdlib\Helper\Str; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @class AbstractConfiguration |
57
|
|
|
* @package Platine\Stdlib\Config |
58
|
|
|
*/ |
59
|
|
|
abstract class AbstractConfiguration implements ConfigurationInterface |
60
|
|
|
{ |
61
|
|
|
/** |
62
|
|
|
* The raw configuration array |
63
|
|
|
* @var array<string, mixed> |
64
|
|
|
*/ |
65
|
|
|
protected array $config = []; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritedoc} |
69
|
|
|
*/ |
70
|
|
|
public function __construct(array $config = []) |
71
|
|
|
{ |
72
|
|
|
$configuration = array_merge($this->getDefault(), $config); |
73
|
|
|
$this->load($configuration); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritedoc} |
78
|
|
|
*/ |
79
|
|
|
public function get(string $name): mixed |
80
|
|
|
{ |
81
|
|
|
if ($this->has($name) === false) { |
82
|
|
|
throw new InvalidArgumentException(sprintf( |
83
|
|
|
'Configuration [%s] does not exist', |
84
|
|
|
$name |
85
|
|
|
)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return Arr::get($this->config, $name); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritedoc} |
93
|
|
|
*/ |
94
|
|
|
public function set(string $name, mixed $value): void |
95
|
|
|
{ |
96
|
|
|
$rules = $this->getValidationRules(); |
97
|
|
|
if (array_key_exists($name, $rules)) { |
98
|
|
|
$type = $rules[$name]; |
99
|
|
|
$this->checkType($name, $type, $value); |
100
|
|
|
} |
101
|
|
|
Arr::set($this->config, $name, $value); |
102
|
|
|
$setters = $this->getSetterMaps(); |
103
|
|
|
$key = Str::camel($name, true); |
104
|
|
|
if (Arr::has($setters, $key)) { |
105
|
|
|
$method = Arr::get($setters, $key); |
106
|
|
|
$this->{$method}($value); |
107
|
|
|
} else { |
108
|
|
|
$setterMethod = 'set' . ucfirst($key); |
109
|
|
|
if (method_exists($this, $setterMethod)) { |
110
|
|
|
$this->{$setterMethod}($value); |
111
|
|
|
} else { |
112
|
|
|
$this->{$key} = $value; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritedoc} |
119
|
|
|
*/ |
120
|
|
|
public function has(string $name): bool |
121
|
|
|
{ |
122
|
|
|
return Arr::has($this->config, $name); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritedoc} |
127
|
|
|
*/ |
128
|
|
|
public function load(array $config): void |
129
|
|
|
{ |
130
|
|
|
$this->config = $config; |
131
|
|
|
|
132
|
|
|
foreach ($config as $name => $value) { |
133
|
|
|
$this->set($name, $value); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritedoc} |
139
|
|
|
*/ |
140
|
|
|
public function getValidationRules(): array |
141
|
|
|
{ |
142
|
|
|
return []; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritedoc} |
147
|
|
|
*/ |
148
|
|
|
public function getSetterMaps(): array |
149
|
|
|
{ |
150
|
|
|
return []; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritedoc} |
155
|
|
|
*/ |
156
|
|
|
public function getDefault(): array |
157
|
|
|
{ |
158
|
|
|
return []; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Check the configuration for the given type |
163
|
|
|
* @param string $key the configuration |
164
|
|
|
* key to be checked can be dot notation |
165
|
|
|
* @param string $type |
166
|
|
|
* @param mixed $value |
167
|
|
|
* @return void |
168
|
|
|
*/ |
169
|
|
|
private function checkType(string $key, string $type, mixed $value = null): void |
170
|
|
|
{ |
171
|
|
|
if (!Arr::has($this->config, $key) && $value === null) { |
172
|
|
|
return; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if ($value === null) { |
176
|
|
|
$value = Arr::get($this->config, $key, null); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$valueType = gettype($value); |
180
|
|
|
$className = null; |
181
|
|
|
if (strpos($type, 'object::') === 0) { |
182
|
|
|
$className = substr($type, 8); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$error = null; |
186
|
|
|
|
187
|
|
|
if ($className !== null) { |
188
|
|
|
if (!($value instanceof $className)) { |
189
|
|
|
$error = 'Invalid configuration [%s] instance value, expected [%s], but got [%s]'; |
190
|
|
|
} |
191
|
|
|
} elseif ($type !== $valueType) { |
192
|
|
|
$error = 'Invalid configuration [%s] value, expected [%s], but got [%s]'; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if ($error !== null) { |
196
|
|
|
throw new Error(sprintf( |
197
|
|
|
$error, |
198
|
|
|
Str::snake($key), |
199
|
|
|
$className ?? $type, |
200
|
|
|
is_object($value) ? get_class($value) : gettype($value) |
201
|
|
|
)); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|