| 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/GCalendar | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |      * Get the boolean value specified by path. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Accepted values are (case insensitiv): <ul> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * <li> true, on, yes, 1 </li> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * <li> false, off, no, none, 0 </li></ul> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * for all other values the default is returned! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param string $strPath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param bool $bDefault | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function getBool(string $strPath, bool $bDefault = false) : bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $value = (string) $this->getValue($strPath, $bDefault); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         if ($this->isTrue($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         } else if ($this->isFalse($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             return $bDefault; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Parse the given file an add all settings to the internal configuration. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param string $strConfigFile | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     protected function parseFile(string $strConfigFile) : array | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         if (!file_exists($strConfigFile)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             trigger_error('Config File (' . $strConfigFile . ') does not exist!', E_USER_WARNING); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             return []; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         $aXML = []; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         // first read XML data into stdclass object using the SimpleXML | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         // to convert this object into associative array by encode it as JSON string and | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         // decode it with the $assoc parameter set to true... | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $oXML = new \SimpleXMLElement($strConfigFile, 0, true); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $strJSON = json_encode($oXML); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $aXML = json_decode($strJSON, true); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         return $aXML; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |  |