|
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 http://www.iacademy.cf |
|
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
|
|
|
/** |
|
63
|
|
|
* The raw configuration array |
|
64
|
|
|
* @var array<string, mixed> |
|
65
|
|
|
*/ |
|
66
|
|
|
protected array $config = []; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritedoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
public function __construct(array $config = []) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->load($config); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritedoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
public function get(string $name) |
|
80
|
|
|
{ |
|
81
|
|
|
if (!$this->has($name)) { |
|
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 has(string $name): bool |
|
95
|
|
|
{ |
|
96
|
|
|
return Arr::has($this->config, $name); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritedoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function load(array $config): void |
|
103
|
|
|
{ |
|
104
|
|
|
$this->config = $config; |
|
105
|
|
|
$rules = $this->getValidationRules(); |
|
106
|
|
|
$setters = $this->getSetterMaps(); |
|
107
|
|
|
|
|
108
|
|
|
foreach ($rules as $name => $type) { |
|
109
|
|
|
$this->checkType($name, $type); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
foreach ($config as $name => $value) { |
|
113
|
|
|
$key = Str::camel($name, true); |
|
114
|
|
|
|
|
115
|
|
|
if (Arr::has($setters, $key)) { |
|
116
|
|
|
$method = Arr::get($setters, $key); |
|
117
|
|
|
$this->{$method}($value); |
|
118
|
|
|
} else { |
|
119
|
|
|
$setterMethod = 'set' . ucfirst($key); |
|
120
|
|
|
if (method_exists($this, $setterMethod)) { |
|
121
|
|
|
$this->{$setterMethod}($value); |
|
122
|
|
|
} else { |
|
123
|
|
|
$this->{$key} = $value; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* {@inheritedoc} |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getValidationRules(): array |
|
133
|
|
|
{ |
|
134
|
|
|
return []; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* {@inheritedoc} |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getSetterMaps(): array |
|
141
|
|
|
{ |
|
142
|
|
|
return []; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Check the configuration for the given type |
|
147
|
|
|
* @param string $key the configuration |
|
148
|
|
|
* key to be checked can be dot notation |
|
149
|
|
|
* @param string $type |
|
150
|
|
|
* @return void |
|
151
|
|
|
*/ |
|
152
|
|
|
private function checkType(string $key, string $type): void |
|
153
|
|
|
{ |
|
154
|
|
|
if (!Arr::has($this->config, $key)) { |
|
155
|
|
|
return; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$value = Arr::get($this->config, $key, null); |
|
159
|
|
|
|
|
160
|
|
|
$valueType = gettype($value); |
|
161
|
|
|
$className = null; |
|
162
|
|
|
if (strpos($type, 'object::') === 0) { |
|
163
|
|
|
$className = substr($type, 8); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$error = null; |
|
167
|
|
|
|
|
168
|
|
|
if ($className !== null) { |
|
169
|
|
|
if (!($value instanceof $className)) { |
|
170
|
|
|
$error = 'Invalid configuration [%s] instance value, expected [%s], but got [%s]'; |
|
171
|
|
|
} |
|
172
|
|
|
} elseif ($type !== $valueType) { |
|
173
|
|
|
$error = 'Invalid configuration [%s] value, expected [%s], but got [%s]'; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
if ($error !== null) { |
|
177
|
|
|
throw new Error(sprintf( |
|
178
|
|
|
$error, |
|
179
|
|
|
Str::snake($key), |
|
180
|
|
|
$className ?? $type, |
|
181
|
|
|
is_object($value) ? get_class($value) : gettype($value) |
|
182
|
|
|
)); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|