File: src/loader/XmlLoader.php

Recommend this page to a friend!
  Classes of Vitaly  >  Queasy PHP Config  >  src/loader/XmlLoader.php  >  Download  
File: src/loader/XmlLoader.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Queasy PHP Config
Read a configuration from files in several formats
Author: By
Last change:
Date: 5 months ago
Size: 1,573 bytes
 

Contents

Class file image Download
<?php

/*
 * Queasy PHP Framework - Configuration
 *
 * (c) Vitaly Demyanenko <vitaly_demyanenko@yahoo.com>
 *
 * For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
 */

namespace queasy\config\loader;

use
Exception;
use
SimpleXMLElement;

/**
 * XML configuration loader class
 */
class XmlLoader extends FileSystemLoader
{
   
/**
     * Load and return an array containing configuration.
     *
     * @return array Loaded configuration
     *
     * @throws ConfigLoaderException When file is corrupted
     */
   
public function load()
    {
       
$path = $this->path();

        try {
           
$xml = new SimpleXMLElement(file_get_contents($path));

            return
$this->buildFromXml($xml);
        } catch (
Exception $e) {
            throw new
CorruptedException($this->path());
        }
    }

    private function
buildFromXml(SimpleXMLElement $el)
    {
       
$result = array();

        foreach (
$el->attributes() as $attr => $value) {
           
$result[$attr] = trim($value);
        }

        foreach (
$el->children() as $name => $value) {
            if (!isset(
$result[$name])) {
               
$result[$name] = array();
            }

           
$childConfig = $this->buildFromXml($value);

           
$result[$name] = array_merge($result[$name], $childConfig);
           
$result[$name][] = $childConfig;
        }

       
$text = trim($el->__toString());
        if (!empty(
$text)) {
           
$result[''] = $text;
        }

        return
$result;
    }
}


For more information send a message to info at phpclasses dot org.