|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stu\Module\Config\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Noodlehaus\ConfigInterface; |
|
6
|
|
|
use Stu\Module\Config\StuConfigException; |
|
7
|
|
|
|
|
8
|
|
|
class SettingsCore implements SettingsCoreInterface |
|
9
|
|
|
{ |
|
10
|
17 |
|
public function __construct( |
|
11
|
|
|
private ?SettingsInterface $parentSetting, |
|
12
|
|
|
private string $configPath, |
|
13
|
|
|
private ConfigInterface $config |
|
14
|
|
|
) { |
|
15
|
17 |
|
} |
|
16
|
|
|
|
|
17
|
16 |
|
public function getPath(): string |
|
18
|
|
|
{ |
|
19
|
16 |
|
if ($this->parentSetting === null) { |
|
20
|
14 |
|
return $this->configPath; |
|
21
|
|
|
} |
|
22
|
2 |
|
return sprintf('%s.%s', $this->parentSetting->getPath(), $this->configPath); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
15 |
|
public function getConfig(): ConfigInterface |
|
26
|
|
|
{ |
|
27
|
15 |
|
return $this->config; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
4 |
|
public function getIntegerConfigValue(string $setting, ?int $default = null): int |
|
31
|
|
|
{ |
|
32
|
4 |
|
$path = $this->getSettingPath($setting); |
|
33
|
4 |
|
$value = $this->getConfigValue($path, $default); |
|
34
|
|
|
|
|
35
|
3 |
|
if (!is_numeric($value)) { |
|
36
|
1 |
|
throw new StuConfigException(sprintf('The value "%s" with path "%s" is no valid integer.', $value, $path)); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
2 |
|
return (int)$value; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
7 |
|
public function getStringConfigValue(string $setting, ?string $default = null): string |
|
43
|
|
|
{ |
|
44
|
7 |
|
$path = $this->getSettingPath($setting); |
|
45
|
7 |
|
$value = $this->getConfigValue($path, $default); |
|
46
|
|
|
|
|
47
|
7 |
|
if (!is_string($value)) { |
|
48
|
1 |
|
throw new StuConfigException(sprintf('The value "%s" with path "%s" is no valid string.', $value, $path)); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
6 |
|
return $value; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
7 |
|
public function getBooleanConfigValue(string $setting, ?bool $default = null): bool |
|
55
|
|
|
{ |
|
56
|
7 |
|
$path = $this->getSettingPath($setting); |
|
57
|
7 |
|
$value = $this->getConfigValue($path, $default); |
|
58
|
|
|
|
|
59
|
7 |
|
if (!is_bool($value)) { |
|
60
|
1 |
|
throw new StuConfigException(sprintf('The value "%s" with path "%s" is no valid boolean.', $value, $path)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
6 |
|
return $value; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string[]|null $default |
|
68
|
|
|
* |
|
69
|
|
|
* @return array<string> |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getArrayConfigValue(string $setting, ?array $default = null): array |
|
72
|
|
|
{ |
|
73
|
|
|
$path = $this->getSettingPath($setting); |
|
74
|
|
|
|
|
75
|
|
|
return $this->getConfigArray($path, $default); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function exists(string $setting): bool |
|
79
|
|
|
{ |
|
80
|
|
|
$path = $this->getSettingPath($setting); |
|
81
|
|
|
$value = $this->getConfig()->get($path); |
|
82
|
|
|
|
|
83
|
|
|
return $value !== null; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return int|string|bool |
|
88
|
|
|
*/ |
|
89
|
14 |
|
private function getConfigValue(string $path, int|null|string|bool $default = null) |
|
90
|
|
|
{ |
|
91
|
14 |
|
$value = $this->getConfig()->get($path); |
|
92
|
|
|
|
|
93
|
14 |
|
if ($value !== null) { |
|
94
|
10 |
|
return $value; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
//throw exception if setting mandatory |
|
98
|
4 |
|
if ($default === null) { |
|
99
|
1 |
|
throw new StuConfigException(sprintf('There is no corresponding config setting on path "%s"', $path)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
3 |
|
return $default; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string[]|null $default |
|
107
|
|
|
* |
|
108
|
|
|
* @return array<string> |
|
109
|
|
|
*/ |
|
110
|
|
|
private function getConfigArray(string $path, ?array $default = null): array |
|
111
|
|
|
{ |
|
112
|
|
|
$value = $this->getConfig()->get($path); |
|
113
|
|
|
|
|
114
|
|
|
if ($value !== null) { |
|
115
|
|
|
if (!is_array($value)) { |
|
116
|
|
|
throw new StuConfigException(sprintf('The value "%s" with path "%s" is no valid array.', $value, $path)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $value; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
//throw exception if setting mandatory |
|
123
|
|
|
if ($default === null) { |
|
124
|
|
|
throw new StuConfigException(sprintf('There is no corresponding config setting on path "%s"', $path)); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $default; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
14 |
|
private function getSettingPath(string $setting): string |
|
131
|
|
|
{ |
|
132
|
14 |
|
return sprintf('%s.%s', $this->getPath(), $setting); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|