Test Failed
Push — main ( bc775a...919c7e )
by Stefan
07:40
created

XMLConfig   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 11
c 1
b 0
f 1
dl 0
loc 33
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A parseFile() 0 18 3
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Config;
5
6
/**
7
 * Class for config component getting data from XML file.
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
class XMLConfig extends AbstractConfig
18
{
19
    /**
20
     * The constructor expects an valid filename/path to the JSON file.
21
     * @param string $strConfigFile
22
     */
23
    public function __construct(string $strConfigFile)
24
    {
25
        $this->aConfig = $this->parseFile($strConfigFile);
26
    }
27
    
28
    /**
29
     * Parse the given file an add all settings to the internal configuration.
30
     * @param string $strConfigFile
31
     */
32
    protected function parseFile(string $strConfigFile) : array
33
    {
34
        if (!file_exists($strConfigFile)) {
35
            trigger_error('Config File (' . $strConfigFile . ') does not exist!', E_USER_WARNING);
36
        }
37
        
38
        // first read XML data into stdclass object using the SimpleXML
39
        // to convert this object into associative array by encode it as JSON string and
40
        // decode it with the $assoc parameter set to true...
41
        try {
42
            $oXML = new \SimpleXMLElement($strConfigFile, 0, true);
43
        } catch (\Exception $e) {
44
            trigger_error('Invalid config file (' . $strConfigFile . '): ' . $e->getMessage(), E_USER_ERROR);
45
        }
46
        $strJSON = json_encode($oXML);
47
        $aXML = json_decode($strJSON, true);
48
        
49
        return $aXML;
50
    }
51
}
52