Completed
Push — master ( b4d615...44d46c )
by Zach
02:15
created

Config::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yarak\Config;
4
5
use Phalcon\DI;
6
use Yarak\Exceptions\InvalidConfig;
7
8
class Config
9
{
10
    use PathHelpers;
11
12
    /**
13
     * Instance of self.
14
     *
15
     * @var Config
16
     */
17
    private static $instance;
18
19
    /**
20
     * Yarak config array.
21
     *
22
     * @var array
23
     */
24
    protected $configArray;
25
26
    /**
27
     * Default setting values.
28
     *
29
     * @var array
30
     */
31
    const DEFAULTS = [
32
        'migratorType'        => 'fileDate',
33
        'migrationRepository' => 'database',
34
    ];
35
36
    /**
37
     * Private constructor.
38
     *
39
     * @param array $configArray
40
     */
41
    private function __construct(array $configArray)
42
    {
43
        $this->configArray = $configArray;
44
    }
45
46
    /**
47
     * Get instance of self with config array set.
48
     *
49
     * @param array $configArray
50
     *
51
     * @return Config
52
     */
53
    public static function getInstance(array $configArray = [])
54
    {
55
        if (empty(self::$instance)) {
56
            if (empty($configArray)) {
57
                $configArray = self::getSetConfigArray();
58
            }
59
60
            self::$instance = new self($configArray);
61
        }
62
63
        return self::$instance;
64
    }
65
66
    /**
67
     * Get the set config array.
68
     *
69
     * @return array
70
     */
71
    public static function getSetConfigArray()
72
    {
73
        return DI::getDefault()
74
            ->getShared('yarak')
75
            ->getConfigArray();
76
    }
77
78
    /**
79
     * Get a value from the config array.
80
     *
81
     * @param string|array $value
82
     *
83
     * @return mixed
84
     */
85
    public function get($value)
86
    {
87
        $current = $this->configArray;
88
89
        foreach ($this->makeArray($value) as $configItem) {
90
            if (!isset($current[$configItem])) {
91
                return $this->getDefault($configItem);
92
            }
93
94
            $current = $current[$configItem];
95
        }
96
97
        return $current;
98
    }
99
100
    /**
101
     * Return true if config array has given value.
102
     *
103
     * @param mixed $value
104
     *
105
     * @return bool
106
     */
107
    public function has($value)
108
    {
109
        return !($this->get($value) === null);
110
    }
111
112
    /**
113
     * Get a setting's default value.
114
     *
115
     * @param string $value
116
     *
117
     * @return mixed|null
118
     */
119
    public function getDefault($value)
120
    {
121
        if (array_key_exists($value, self::DEFAULTS)) {
122
            return self::DEFAULTS[$value];
123
        }
124
    }
125
126
    /**
127
     * Set an item in the config.
128
     *
129
     * @param mixed $keys
130
     * @param mixed $value
131
     */
132
    public function set($keys, $value)
133
    {
134
        $temp = &$this->configArray;
135
136
        foreach ($this->makeArray($keys) as $key) {
137
            $temp = &$temp[$key];
138
        }
139
140
        $temp = $value;
141
    }
142
143
    /**
144
     * Remove an item from the config.
145
     *
146
     * @param mixed $keys
147
     */
148
    public function remove($keys)
149
    {
150
        $keys = $this->makeArray($keys);
151
152
        $temp = &$this->configArray;
153
154
        foreach ($keys as $key) {
155
            if ($key === $keys[count($keys) - 1]) {
156
                unset($temp[$key]);
157
            } else {
158
                $temp = &$temp[$key];
159
            }
160
        }
161
    }
162
163
    /**
164
     * Set the config array to its original values.
165
     */
166
    public function refresh()
167
    {
168
        $this->configArray = self::getSetConfigArray();
169
    }
170
171
    /**
172
     * Return the config array.
173
     *
174
     * @return array
175
     */
176
    public function toArray()
177
    {
178
        return $this->configArray;
179
    }
180
181
    /**
182
     * Make a variable an array if not one already.
183
     *
184
     * @param mixed $value
185
     *
186
     * @return array
187
     */
188
    protected function makeArray($value)
189
    {
190
        if (!is_array($value)) {
191
            return [$value];
192
        }
193
194
        return $value;
195
    }
196
197
    /**
198
     * Validate that a setting exists.
199
     *
200
     * @param array $settings
201
     *
202
     * @throws InvalidConfig
203
     */
204
    public function validate(array $settings)
205
    {
206
        if (!$this->has($settings)) {
207
            throw InvalidConfig::configValueNotFound(implode(' -> ', $settings));
208
        }
209
    }
210
}
211