Config::mustGetString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace AbterPhp\Framework\Config;
7
8
use Opulence\Framework\Configuration\Config as OpulenceConfig;
9
use RuntimeException;
10
11
class Config extends OpulenceConfig
12
{
13
    /**
14
     * Gets a setting, but throws an exception if it's empty
15
     *
16
     * @param string $category The category of setting to get
17
     * @param string $setting  The name of the setting to get
18
     * @param mixed  $default  The default value if one does not exist
19
     *
20
     * @return string|int|float|bool The value of the setting
21
     * @throws \RuntimeException
22
     */
23
    public static function mustGet(string $category, string $setting, $default = null)
24
    {
25
        $value = parent::get($category, $setting, $default);
26
27
        if ($value === null) {
28
            throw new RuntimeException(sprintf("missing config: %s:%s", $category, $setting));
29
        }
30
31
        if (!is_scalar($value)) {
32
            throw new RuntimeException(
33
                sprintf("invalid config: %s:%s, type: %s", $category, $setting, gettype($value))
34
            );
35
        }
36
37
        return $value;
38
    }
39
40
    /**
41
     * @param string $category
42
     * @param string $setting
43
     * @param string $default
44
     *
45
     * @return string
46
     */
47
    public static function mustGetString(string $category, string $setting, string $default = ""): string
48
    {
49
        return (string)static::mustGet($category, $setting, $default);
50
    }
51
52
    /**
53
     * @param string $category
54
     * @param string $setting
55
     * @param bool   $default
56
     *
57
     * @return bool
58
     */
59
    public static function mustGetBool(string $category, string $setting, bool $default = false): bool
60
    {
61
        $value = static::mustGet($category, $setting, $default);
62
63
        if (is_bool($value)) {
64
            return $value;
65
        }
66
67
        if (in_array($value, [0, 1, '0', '1', ''], true)) {
68
            return (bool)$value;
69
        }
70
71
        if (is_string($value)) {
72
            $l = strtolower($value);
73
74
            if ($l === 'false') {
75
                return false;
76
            } elseif ($l === 'true') {
77
                return true;
78
            }
79
        }
80
81
        throw new RuntimeException(
82
            sprintf("non-bool config: %s:%s, type: %s, value: %s", $category, $setting, gettype($value), $value)
83
        );
84
    }
85
86
    /**
87
     * @param string $category
88
     * @param string $setting
89
     * @param int    $default
90
     *
91
     * @return int
92
     */
93
    public static function mustGetInt(string $category, string $setting, int $default = 0): int
94
    {
95
        $value = static::mustGet($category, $setting, $default);
96
97
        if (!is_numeric($value)) {
98
            throw new RuntimeException(
99
                sprintf("non-int config: %s:%s, type: %s, value: %s", $category, $setting, gettype($value), $value)
100
            );
101
        }
102
103
        return (int)$value;
104
    }
105
106
    /**
107
     * @param string $category
108
     * @param string $setting
109
     * @param float  $default
110
     *
111
     * @return float
112
     */
113
    public static function mustGetFloat(string $category, string $setting, float $default = 0.0): float
114
    {
115
        $value = static::mustGet($category, $setting, $default);
116
117
        if (!is_numeric($value)) {
118
            throw new RuntimeException(
119
                sprintf("non-float config: %s:%s, type: %s, value: %s", $category, $setting, gettype($value), $value)
120
            );
121
        }
122
123
        return (float)$value;
124
    }
125
}
126