Config::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework\Libraries;
6
7
final class Config extends \WebServCo\Framework\AbstractLibrary implements
8
    \WebServCo\Framework\Interfaces\ConfigInterface
9
{
10
    /**
11
     * Stores configuration data.
12
     *
13
     * @var array<mixed>
14
     */
15
    private array $config = [];
16
17
    /**
18
     * Add base setting data.
19
     *
20
     * Keys will be preserved.
21
     * Existing values will be overwritten.
22
     *
23
     * @param string $setting Name of setting to load.
24
     * @param mixed $data Data to add.
25
     */
26
    public function add(string $setting, $data): bool
27
    {
28
        $this->config = \WebServCo\Framework\ArrayStorage::append(
29
            $this->config,
30
            [$setting => $data],
31
        );
32
        return true;
33
    }
34
35
    /**
36
     * @param mixed $setting Can be an array, a string,
37
     *                          or a special formatted string
38
     *                          (eg 'i18n/lang').
39
     * @param mixed $defaultValue
40
     * @return mixed
41
     */
42
    public function get($setting, $defaultValue = null)
43
    {
44
        return \WebServCo\Framework\ArrayStorage::get($this->config, $setting, $defaultValue);
45
    }
46
47
    /**
48
     * Load configuration data from a file.
49
     *
50
     * @param string $setting Name of setting to load.
51
     * @param string $pathProject Directory where the file is located.
52
     *                      File name must be <$setting>.php
53
     * @return array<mixed>
54
     */
55
    public function load(string $setting, string $pathProject): array
56
    {
57
        $pathFull = \sprintf('%sconfig/%s.php', $pathProject, $setting);
58
        if (!\is_readable($pathFull)) {
59
            return [];
60
        }
61
        $data = (include $pathFull);
62
        return \is_array($data)
63
            ? $data
64
            : [];
65
    }
66
67
    /**
68
     * Sets a configuration value.
69
     *
70
     * @param mixed $setting Can be an array, a string,
71
     *                          or a special formatted string
72
     *                          (eg 'i18n/lang').
73
     * @param mixed $value The value to be stored.
74
     * @return bool true on success and false on failure.
75
     */
76
    public function set($setting, $value): bool
77
    {
78
        if (!$setting) {
79
            return false;
80
        }
81
        $this->config = \WebServCo\Framework\ArrayStorage::set($this->config, $setting, $value);
82
        return true;
83
    }
84
}
85