Completed
Push — master ( 9f3343...c80390 )
by Casey
13:53
created

ArraySettingProvider::__construct()   A

Complexity

Conditions 6
Paths 17

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 6
eloc 10
c 4
b 0
f 0
nc 17
nop 4
dl 0
loc 20
rs 9.2222
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 ArraySettingProvider 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
    public function __construct(
61
        array $settings,
62
        SettingDefinitionRegistry $definitionRegistry,
63
        string $name,
64
        string $displayName
65
    ) {
66
67
        $this->name = $name;
68
        $this->displayName = $displayName;
69
70
        // Validate all of the settings as they are read from the input
71
        foreach ($settings as $settingName => $value) {
72
            $mutable = (is_array($value) && isset($value['mutable'])) ? (bool) $value['mutable'] : true;
73
            $value = (is_array($value) && isset($value['value'])) ? $value['value'] : $value;
74
75
            $this->settings[$settingName] = new SettingValue(
76
                $settingName,
77
                $this,
78
                $mutable,
79
                $definitionRegistry->get($settingName)->processValue($value)
80
            );
81
        }
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getName(): string
88
    {
89
        return $this->name;
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getDisplayName(): string
96
    {
97
        return $this->displayName;
98
    }
99
100
    /**
101
     * Return a key/value set of setting names/values
102
     *
103
     * @return iterable|SettingValue[]
104
     */
105
    public function getSettingValues(): iterable
106
    {
107
        return $this->settings;
108
    }
109
110
    /**
111
     * @param string $settingName
112
     * @return SettingProvider|null
113
     */
114
    public function findValueInstance(string $settingName): ?SettingValue
115
    {
116
        return $this->settings[$settingName] ?? null;
117
    }
118
}
119