Completed
Push — dev ( fe3822...8e18b7 )
by Zach
02:29
created

Config::has()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yarak\Config;
4
5
class Config
6
{
7
    /**
8
     * Instance of self.
9
     *
10
     * @var Config
11
     */
12
    private static $instance;
13
14
    /**
15
     * Yarak config array.
16
     *
17
     * @var array
18
     */
19
    protected $configArray;
20
21
    /**
22
     * Default setting values.
23
     *
24
     * @var array
25
     */
26
    const DEFAULTS = [
27
        'migratorType' => 'fileDate',
28
        'migrationRepository' => 'database',
29
    ];
30
31
    /**
32
     * Private constructor.
33
     */
34
    private function __construct(array $configArray)
35
    {
36
        $this->configArray = $configArray;
37
    }
38
39
    /**
40
     * Get instance of self with config array set.
41
     *
42
     * @param array $configArray
43
     *
44
     * @return Config
45
     */
46
    public static function getInstance(array $configArray = [])
47
    {
48
        if (empty(self::$instance)) {
49
            if (empty($configArray)) {
50
                $di = \Phalcon\DI::getDefault();
51
52
                $configArray = $di->getShared('yarak')->getConfig();
53
            }
54
55
            self::$instance = new self($configArray);
56
        }
57
58
        return self::$instance;
59
    }
60
61
    /**
62
     * Get a value from the config array.
63
     *
64
     * @param string|array $value
65
     *
66
     * @return mixed
67
     */
68
    public function get($value)
69
    {
70
        $value = $this->makeArray($value);
71
72
        $current = $this->configArray;
73
74
        foreach ($value as $configItem) {
75
            if (!isset($current[$configItem])) {
76
                return $this->getDefault($configItem);
77
            } else {
78
                $current = $current[$configItem];
79
            }
80
        }
81
82
        return $current;
83
    }
84
85
    /**
86
     * Return true if config array has given value.
87
     *
88
     * @param mixed $value
89
     *
90
     * @return bool
91
     */
92
    public function has($value)
93
    {
94
        if ($this->get($value) === null) {
95
            return false;
96
        }
97
98
        return true;
99
    }
100
101
    /**
102
     * Get a setting's default value.
103
     *
104
     * @param string $value
105
     *
106
     * @return mixed|null
107
     */
108
    public function getDefault($value)
109
    {
110
        if (array_key_exists($value, self::DEFAULTS)) {
111
            return self::DEFAULTS[$value];
112
        }
113
    }
114
115
    /**
116
     * Return config array.
117
     *
118
     * @return array
119
     */
120
    public function getAll()
121
    {
122
        return $this->configArray;
123
    }
124
125
    /**
126
     * Set an item in the config.
127
     *
128
     * @param mixed $keys
129
     * @param mixed $value
130
     */
131
    public function set($keys, $value)
132
    {
133
        $keys = $this->makeArray($keys);
134
135
        $temp = &$this->configArray;
136
137
        foreach ($keys as $key) {
138
            $temp = &$temp[$key];
139
        }
140
141
        $temp = $value;
142
    }
143
144
    /**
145
     * Remove an item from the config.
146
     *
147
     * @param mixed $keys
148
     */
149
    public function remove($keys)
150
    {
151
        $keys = $this->makeArray($keys);
152
153
        $temp = &$this->configArray;
154
155
        foreach ($keys as $key) {
156
            if ($key === $keys[count($keys) - 1]) {
157
                unset($temp[$key]);
158
            } else {
159
                $temp = &$temp[$key];
160
            }
161
        }
162
    }
163
164
    /**
165
     * Return the database directory path.
166
     *
167
     * @return string
168
     */
169
    public function getDatabaseDirectory()
170
    {
171
        return $this->addFinalSlash($this->get(['application', 'databaseDir']));
172
    }
173
174
    /**
175
     * Return the migration directory path.
176
     *
177
     * @param string $path
178
     *
179
     * @return string
180
     */
181
    public function getMigrationDirectory($path = '')
182
    {
183
        return $this->getDatabaseDirectory().'migrations/'.$path;
184
    }
185
186
    /**
187
     * Return the factory directory path.
188
     *
189
     * @param string $path
190
     *
191
     * @return string
192
     */
193
    public function getFactoryDirectory($path = '')
194
    {
195
        return $this->getDatabaseDirectory().'factories/'.$path;
196
    }
197
198
    /**
199
     * Return the seeds directory path.
200
     *
201
     * @param string $path
202
     *
203
     * @return string
204
     */
205
    public function getSeedDirectory($path = '')
206
    {
207
        return $this->getDatabaseDirectory().'seeds/'.$path;
208
    }
209
210
    /**
211
     * Make database directory structure if it doesn't exist.
212
     */
213
    public function getAllDatabaseDirectories()
214
    {
215
        return [
216
            $this->getDatabaseDirectory(),
217
            $this->getMigrationDirectory(),
218
            $this->getFactoryDirectory(),
219
            $this->getSeedDirectory(),
220
        ];
221
    }
222
223
    /**
224
     * Get the commands directory path.
225
     *
226
     * @param string $path
227
     *
228
     * @return string
229
     */
230
    public function getCommandsDirectory($path = '')
231
    {
232
        return $this->addFinalSlash(
233
            $this->get(['application', 'commandsDir'])
234
        ).$path;
235
    }
236
237
    /**
238
     * Return the config array.
239
     *
240
     * @return array
241
     */
242
    public function toArray()
243
    {
244
        return $this->getAll();
245
    }
246
247
    /**
248
     * Add a final slash to a path if it doesn't exist.
249
     *
250
     * @param string $path
251
     *
252
     * @return string
253
     */
254
    protected function addFinalSlash($path)
255
    {
256
        if (substr($path, -1) !== '/') {
257
            $path .= '/';
258
        }
259
260
        return $path;
261
    }
262
263
    /**
264
     * Make a variable an array if not one already.
265
     *
266
     * @param mixed $value
267
     *
268
     * @return array
269
     */
270
    protected function makeArray($value)
271
    {
272
        if (!is_array($value)) {
273
            $value = [$value];
274
        }
275
276
        return $value;
277
    }
278
}
279