1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Config.php |
5
|
|
|
* |
6
|
|
|
* An immutable class for config options. |
7
|
|
|
* |
8
|
|
|
* @package jaxon-config |
9
|
|
|
* @author Thierry Feuzeu <[email protected]> |
10
|
|
|
* @copyright 2025 Thierry Feuzeu <[email protected]> |
11
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
12
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Jaxon\Config; |
16
|
|
|
|
17
|
|
|
use Jaxon\Config\Reader\Value; |
18
|
|
|
|
19
|
|
|
use function array_combine; |
20
|
|
|
use function array_key_exists; |
21
|
|
|
use function array_keys; |
22
|
|
|
use function array_map; |
23
|
|
|
use function trim; |
24
|
|
|
|
25
|
|
|
class Config |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* The constructor |
29
|
|
|
* |
30
|
|
|
* @param array $aValues |
31
|
|
|
* @param bool $bChanged |
32
|
|
|
*/ |
33
|
|
|
public function __construct(private array $aValues = [], private bool $bChanged = true) |
34
|
|
|
{} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get the config values |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
public function getValues(): array |
42
|
|
|
{ |
43
|
|
|
return $this->aValues; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* If the values has changed |
48
|
|
|
* |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
public function changed(): bool |
52
|
|
|
{ |
53
|
|
|
return $this->bChanged; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the value of a config option |
58
|
|
|
* |
59
|
|
|
* @param string $sName The option name |
60
|
|
|
* @param mixed $xDefault The default value, to be returned if the option is not defined |
61
|
|
|
* |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
public function getOption(string $sName, $xDefault = null) |
65
|
|
|
{ |
66
|
|
|
return $this->aValues[$sName] ?? $xDefault; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Check the presence of a config option |
71
|
|
|
* |
72
|
|
|
* @param string $sName The option name |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function hasOption(string $sName): bool |
77
|
|
|
{ |
78
|
|
|
return array_key_exists($sName, $this->aValues); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the names of the options under a given key |
83
|
|
|
* |
84
|
|
|
* @param string $sKey The prefix to match |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
public function getOptionNames(string $sKey): array |
89
|
|
|
{ |
90
|
|
|
$sKey = trim($sKey, ' .'); |
91
|
|
|
$aKeys = Value::explodeName($sKey); |
92
|
|
|
$aValues = $this->aValues; |
93
|
|
|
foreach($aKeys as $_sKey) |
94
|
|
|
{ |
95
|
|
|
$aValues = $aValues[$_sKey] ?? []; |
96
|
|
|
} |
97
|
|
|
if(!Value::containsOptions($aValues)) |
98
|
|
|
{ |
99
|
|
|
return []; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// The returned value is an array with short names as keys and full names as values. |
103
|
|
|
$aNames = array_keys($aValues); |
104
|
|
|
$aFullNames = array_map(fn($sName) => "$sKey.$sName", $aNames); |
105
|
|
|
return array_combine($aNames, $aFullNames); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|