-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariableDecorator.php
83 lines (69 loc) · 1.75 KB
/
VariableDecorator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
* Qubus\Config
*
* @link https://github.com/QubusPHP/config
* @copyright 2020 Joshua Parker <[email protected]>
* @copyright 2016 Sinergi
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types=1);
namespace Qubus\Config;
use function array_map;
use function is_array;
use function is_string;
use function strtr;
class VariableDecorator implements ConfigContainer
{
/** @var ConfigInterface $config */
public readonly ConfigContainer $config;
/** @var array $variables */
private array $variables = [];
public function __construct(ConfigContainer $configContainer)
{
$this->config = $configContainer;
}
public function setVariables(array $map): self
{
$this->variables = $map;
return $this;
}
/**
* {@inheritdoc}
*/
public function getConfigKey(string $key, mixed $default = null): mixed
{
return $this->replaceVariables($this->config->getConfigKey($key));
}
/**
* {@inheritdoc}
*/
public function setConfigKey(string $key, mixed $value)
{
return $this->config->setConfigKey($key, $value);
}
/**
* {@inheritdoc}
*/
public function hasConfigKey(string $key): bool
{
return $this->config->hasConfigKey($key);
}
/**
* @param mixed $value
* @return mixed
*/
private function replaceVariables(mixed $value): mixed
{
if (is_string($value)) {
return strtr($value, $this->variables);
}
if (is_array($value)) {
return array_map(
fn ($value) => $this->replaceVariables($value),
$value
);
}
return $value;
}
}