1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Settings Manager |
5
|
|
|
* |
6
|
|
|
* @license http://opensource.org/licenses/MIT |
7
|
|
|
* @link https://github.com/caseyamcl/settings-manager |
8
|
|
|
* @package caseyamcl/settings-manager |
9
|
|
|
* @author Casey McLaughlin <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
* |
14
|
|
|
* ------------------------------------------------------------------ |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
declare(strict_types=1); |
18
|
|
|
|
19
|
|
|
namespace SettingsManager\Provider; |
20
|
|
|
|
21
|
|
|
use SettingsManager\Behavior\SettingProviderTrait; |
22
|
|
|
use SettingsManager\Contract\SettingProvider; |
23
|
|
|
use SettingsManager\Model\SettingValue; |
24
|
|
|
use SettingsManager\Registry\SettingDefinitionRegistry; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Array Settings Provider |
28
|
|
|
* |
29
|
|
|
* Reads settings from an array |
30
|
|
|
* |
31
|
|
|
* @author Casey McLaughlin <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class ArrayValuesProvider implements SettingProvider |
34
|
|
|
{ |
35
|
|
|
use SettingProviderTrait; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $settings = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $name; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $displayName; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* ArraySettingProvider constructor. |
54
|
|
|
* |
55
|
|
|
* @param array $settings Key/value pairs |
56
|
|
|
* @param SettingDefinitionRegistry $definitionRegistry |
57
|
|
|
* @param string $name |
58
|
|
|
* @param string $displayName |
59
|
|
|
*/ |
60
|
60 |
|
public function __construct( |
61
|
|
|
array $settings, |
62
|
|
|
SettingDefinitionRegistry $definitionRegistry, |
63
|
|
|
string $name = 'array', |
64
|
|
|
string $displayName = 'Array Values Provider' |
65
|
|
|
) { |
66
|
|
|
|
67
|
60 |
|
$this->name = $name; |
68
|
60 |
|
$this->displayName = $displayName; |
69
|
|
|
|
70
|
|
|
// Validate all of the settings as they are read from the input |
71
|
60 |
|
foreach ($settings as $settingName => $value) { |
72
|
60 |
|
$mutable = (is_array($value) && isset($value['mutable'])) ? (bool) $value['mutable'] : true; |
73
|
60 |
|
$value = (is_array($value) && isset($value['value'])) ? $value['value'] : $value; |
74
|
|
|
|
75
|
60 |
|
$this->settings[$settingName] = new SettingValue( |
76
|
60 |
|
$settingName, |
77
|
40 |
|
$this, |
78
|
40 |
|
$mutable, |
79
|
60 |
|
$definitionRegistry->get($settingName)->processValue($value) |
80
|
|
|
); |
81
|
|
|
} |
82
|
60 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
60 |
|
public function getName(): string |
88
|
|
|
{ |
89
|
60 |
|
return $this->name; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
3 |
|
public function getDisplayName(): string |
96
|
|
|
{ |
97
|
3 |
|
return $this->displayName; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return a key/value set of setting names/values |
102
|
|
|
* |
103
|
|
|
* @return iterable|SettingValue[] |
104
|
|
|
*/ |
105
|
36 |
|
public function getSettingValues(): iterable |
106
|
|
|
{ |
107
|
36 |
|
return $this->settings; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $settingName |
112
|
|
|
* @return SettingProvider|null |
113
|
|
|
*/ |
114
|
18 |
|
public function findValueInstance(string $settingName): ?SettingValue |
115
|
|
|
{ |
116
|
18 |
|
return $this->settings[$settingName] ?? null; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|