Passed
Push — main ( 5396d8...bc775a )
by Stefan
01:33
created

INIConfig::parseFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 3
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Config;
5
6
/**
7
 * Class for config component getting data from INI file.
8
 * Lines begin with semikolon are ignored as comments.
9
 * The begin of a section have to be set in square brackets.
10
 * A section is active until next section begins or to the end of file.
11
 *
12
 * #### History
13
 * - *2021-01-01*   initial version
14
 *
15
 * @package SKien/Config
16
 * @version 1.0.0
17
 * @author Stefanius <[email protected]>
18
 * @copyright MIT License - see the LICENSE file for details
19
 */
20
class INIConfig extends AbstractConfig
21
{
22
    /**
23
     * Get the boolean value specified by path.
24
     * Accepted values are (case insensitiv): <ul>
25
     * <li> true, on, yes, 1 </li>
26
     * <li> false, off, no, none, 0 </li></ul>
27
     * for all other values the default is returned!
28
     * @param string $strPath
29
     * @param bool $bDefault
30
     * @return bool
31
     */
32
    public function getBool(string $strPath, bool $bDefault = false) : bool
33
    {
34
        $value = (string) $this->getValue($strPath, $bDefault);
35
        if ($this->isTrue($value)) {
36
            return true;
37
        } else if ($this->isFalse($value)) {
38
            return false;
39
        } else {
40
            return $bDefault;
41
        }
42
    }
43
    
44
    /**
45
     * Parse the given file an add all settings to the internal configuration. 
46
     * @param string $strConfigFile
47
     */
48
    protected function parseFile(string $strConfigFile) : array
49
    {
50
        if (!file_exists($strConfigFile)) {
51
            trigger_error('Config File (' . $strConfigFile . ') does not exist!', E_USER_WARNING);
52
            return [];
53
        }
54
        $aINI = parse_ini_file($strConfigFile, TRUE);
55
        if ($aINI === false) {
56
            $aINI = [];
57
        }
58
        return $aINI;
59
    }
60
}
61