Test Failed
Push — main ( 1f2fe7...7271c1 )
by Stefan
01:47
created

AbstractConfig::getInt()   A

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 2
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Config;
5
6
/**
7
 * Abstract base class for config components.
8
 *
9
 * #### History
10
 * - *2021-01-01*   initial version
11
 *
12
 * @package SKien/Config
13
 * @version 1.0.0
14
 * @author Stefanius <[email protected]>
15
 * @copyright MIT License - see the LICENSE file for details
16
 */
17
abstract class AbstractConfig implements ConfigInterface
18
{
19
    /** @var array holding the config data    */
20
    protected ?array $aConfig = null;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '?', expecting T_FUNCTION or T_CONST
Loading history...
21
    /** @var string format for date parameters     */
22
    protected string $strDateFormat = 'Y-m-d';
23
    /** @var string format for datetime parameters     */
24
    protected string $strDateTimeFormat = 'Y-m-d H:i';
25
    
26
    /**
27
     * Set the format for date parameters.
28
     * See the formatting options DateTime::createFromFormat. 
29
     * In most cases, the same letters as for the date() can be used. 
30
     * @param string $strFormat
31
     * @link https://www.php.net/manual/en/datetime.createfromformat.php
32
     */
33
    public function setDateFormat(string $strFormat) : void
34
    {
35
        $this->strDateFormat = $strFormat;
36
    }
37
    
38
    /**
39
     * Set the format for datetime parameters.
40
     * See the formatting options DateTime::createFromFormat. 
41
     * In most cases, the same letters as for the date() can be used. 
42
     * @param string $strFormat
43
     * @link https://www.php.net/manual/en/datetime.createfromformat.php
44
     */
45
    public function setDateTimeFormat(string $strFormat) : void
46
    {
47
        $this->strDateTimeFormat = $strFormat;
48
    }
49
    
50
    /**
51
     * Get the value specified by path.
52
     * @param string $strPath
53
     * @param mixed $default
54
     * @return mixed
55
     */
56
    public function getValue(string $strPath, $default = null)
57
    {
58
        if ($this->aConfig === null) {
59
            // without valid config file just return the default value
60
            return $default;
61
        }
62
        $aPath = $this->splitPath($strPath);
63
        $iDepth = count($aPath);
64
        $value = null;
65
        $aValues = $this->aConfig;
66
        for ($i = 0; $i < $iDepth; $i++) {
67
            if (!is_array($aValues)) {
68
                $value = null;
69
                break;
70
            }
71
            $value = $aValues[$aPath[$i]] ?? null;
72
            if ($value === null) {
73
                break;
74
            }
75
            $aValues = $value;
76
        }
77
        return $value ?? $default;
78
    }
79
    
80
    /**
81
     * Get the string value specified by path.
82
     * @param string $strPath
83
     * @param string $strDefault
84
     * @return string
85
     */
86
    public function getString(string $strPath, string $strDefault = '') : string
87
    {
88
        return (string) $this->getValue($strPath, $strDefault);
89
    }
90
    
91
    /**
92
     * Get the integer value specified by path.
93
     * @param string $strPath
94
     * @param int $iDefault
95
     * @return int
96
     */
97
    public function getInt(string $strPath, int $iDefault = 0) : int
98
    {
99
        return intval($this->getValue($strPath, $iDefault));
100
    }
101
    
102
    /**
103
     * Get the integer value specified by path.
104
     * @param string $strPath
105
     * @param float $fltDefault
106
     * @return float
107
     */
108
    public function getFloat(string $strPath, float $fltDefault = 0.0) : float
109
    {
110
        return floatval($this->getValue($strPath, $fltDefault));
111
    }
112
    
113
    /**
114
     * Get the boolean value specified by path.
115
     * @param string $strPath
116
     * @param bool $bDefault
117
     * @return bool
118
     */
119
    public function getBool(string $strPath, bool $bDefault = false) : bool
120
    {
121
        $value = $this->getValue($strPath, $bDefault);
122
        if (!is_bool($value)) {
123
            $value = $this->boolFromString((string) $value, $bDefault);
124
        }
125
        return $value;
126
    }
127
    
128
    /**
129
     * Get the date value specified by path.
130
     * @param string $strPath
131
     * @param mixed $default default value (unix timestamp, DateTime object or date string)
132
     * @return int date as unix timestamp
133
     */
134
    public function getDate(string $strPath, $default = 0) : int
135
    {
136
        $date = (string) $this->getValue($strPath, $default);
137
        if (!ctype_digit($date)) {
138
            $dt = \DateTime::createFromFormat($this->strDateFormat, $date);
139
            $date = $default;
140
            if ($dt !== false) {
141
                $aError = $dt->getLastErrors();
142
                if ($aError['error_count'] == 0) {
143
                    $dt->setTime(0, 0);
144
                    $date = $dt->getTimestamp();
145
                }
146
            }
147
        } else {
148
            $date = intval($date);
149
        }
150
        return $date;
151
    }
152
    
153
    /**
154
     * Get the date and time value specified by path as unix timestamp.
155
     * @param string $strPath
156
     * @param int $default default value (unix timestamp)
157
     * @return int unix timestamp
158
     */
159
    public function getDateTime(string $strPath, $default = 0) : int
160
    {
161
        $date = (string) $this->getValue($strPath, $default);
162
        if (!ctype_digit($date)) {
163
            $dt = \DateTime::createFromFormat($this->strDateTimeFormat, $date);
164
            $date = $default;
165
            if ($dt !== false) {
166
                $aError = $dt->getLastErrors();
167
                if ($aError['error_count'] == 0) {
168
                    $date = $dt->getTimestamp();
169
                }
170
            }
171
        } else {
172
            $date = intval($date);
173
        }
174
        return $date;
175
    }
176
    
177
    /**
178
     * Get the array specified by path.
179
     * @param string $strPath
180
     * @param array $aDefault
181
     * @return array
182
     */
183
    public function getArray(string $strPath, array $aDefault = []) : array
184
    {
185
        $value = $this->getValue($strPath, $aDefault);
186
        return is_array($value) ? $value : $aDefault;
187
    }
188
    
189
    /**
190
     * Split the given path in its components.
191
     * @param string $strPath
192
     * @return array
193
     */
194
    protected function splitPath(string $strPath) : array
195
    {
196
        return explode('.', $strPath);
197
    }
198
    
199
    /**
200
     * Convert string to bool.
201
     * Accepted values are (case insensitiv): <ul>
202
     * <li> true, on, yes, 1 </li>
203
     * <li> false, off, no, none, 0 </li></ul>
204
     * for all other values the default value is returned!
205
     * @param string $strValue
206
     * @param bool $bDefault
207
     * @return bool
208
     */
209
    protected function boolFromString(string $strValue, bool $bDefault = false) : bool
210
    {
211
        if ($this->isTrue($strValue)) {
212
            return true;
213
        } else if ($this->isFalse($strValue)) {
214
            return false;
215
        } else {
216
            return $bDefault;
217
        }
218
    }
219
    
220
    /**
221
     * Checks whether the passed value is a valid expression for bool 'True'.
222
     * Accepted values for bool 'true' are (case insensitiv): <i>true, on, yes, 1</i>
223
     * @param string $strValue
224
     * @return bool
225
     */
226
    protected function isTrue(string $strValue) : bool
227
    {
228
        $strValue = strtolower($strValue);
229
        return in_array($strValue, ['true', 'on', 'yes', '1']);
230
    }
231
    
232
    /**
233
     * Checks whether the passed value is a valid expression for bool 'False'.
234
     * Accepted values for bool 'false' are (case insensitiv): <i>false, off, no, none, 0</i>
235
     * @param string $strValue
236
     * @return bool
237
     */
238
    protected function isFalse(string $strValue) : bool
239
    {
240
        $strValue = strtolower($strValue);
241
        return in_array($strValue, ['false', 'off', 'no', 'none', '0']);
242
    }
243
}