Source for file Xml.class.php

Documentation is available at Xml.class.php

  1. <?php
  2. /**
  3.  * Gumbo Library Framework
  4.  *
  5.  * LICENSE
  6.  * This library is being released under the terms of the New BSD License.  A
  7.  * copy of the license is packaged with the software (LICENSE.txt).  If no
  8.  * copy is found, a copy of the license template can be found at:
  9.  * http://www.opensource.org/licenses/bsd-license.php
  10.  * 
  11.  * @category Gumbo
  12.  * @package Converter
  13.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  14.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  15.  * @author Michael Luster <mluster79@yahoo.com>
  16.  * @link http://sourceforge.net/projects/phpgumbo
  17.  * @version 0.0.1
  18.  */
  19.  
  20. /**
  21.  * XML Converter Class
  22.  *
  23.  * @category Gumbo
  24.  * @package Converter
  25.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  26.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  27.  * @author Michael Luster <mluster79@yahoo.com>
  28.  * @link http://sourceforge.net/projects/phpgumbo
  29.  * @desc XML Converter Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Converter_Abstract");
  34.  
  35.     
  36.     /** @var string $_class SimpleXMLElement Class Name */
  37.     private $_class;
  38.     /** @var int $_options LibXML Options */
  39.     private $_options;
  40.     /** @var string $_ns Namespace */
  41.     private $_ns;
  42.     /** @var bool $_prefix is Prefix option */
  43.     private $_prefix;
  44.     
  45.     
  46.     
  47.     /**
  48.      * Constructor
  49.      * @param string $class_name SimpleXMLElement Class Name
  50.      * @param int $options LibXML Parameters
  51.      * @param string $ns XML Namespace
  52.      * @param bool $is_prefix 
  53.      */
  54.     public function __construct ($class_name=null$options=null$ns=null$is_prefix=null{
  55.         if (!is_null ($class_name)) $this->setClassName ($class_name)}
  56.         if (!is_null ($options)) $this->setOptions ($options)}
  57.         if (!is_null ($ns)) $this->setNamespace ($ns)}
  58.         if (!is_null ($is_prefix)) $this->setPrefix ($is_prefix)}
  59.     }
  60.     
  61.     
  62.     
  63.     /** ACTION METHODS **/
  64.     /**
  65.      * Returns a Composite
  66.      * @param string|SimpleXMLElement$data array data
  67.      * @return Gumbo_Interface_Composite 
  68.      * @throws Gumbo_Exception
  69.      * @uses Gumbo_Branch
  70.      */
  71.     public function toComposite ($data{
  72.         try {
  73.             // verify precondition
  74.             if (!is_string ($data&& !is_object ($data)) {
  75.                 throw new Gumbo_Exception ("Invalid Argument 'data:str|SimpleXMLElement' => {$data}:gettype ($data));
  76.             }
  77.             if (is_object ($data&& !($data instanceof SimpleXMLElement)) {
  78.                 throw new Gumbo_Exception ("Invalid Argument 'data:SimpleXMLElement' => {$data}:gettype ($data));
  79.             }
  80.             
  81.             // converts into a SimpleXMLElement object
  82.             if (is_string ($data)) {
  83.                 $func "simplexml_load_string";
  84.                 if (file_exists ($data)) {
  85.                     $func "simplexml_load_file";
  86.                 }
  87.                 $data call_user_func ($func$data$this->getClassName ()$this->getOptions ()$this->getNamespace ()$this->isPrefix ());
  88.             }
  89.             
  90.             return $this->_getComposite ($data);
  91.         catch (Gumbo_Exception $e{
  92.             $e->setFunction (__METHOD__);
  93.             gumbo_trigger ($e);
  94.         }
  95.         return new Gumbo_Branch ();
  96.     }
  97.     
  98.     
  99.     
  100.     /** MUTATOR METHODS **/
  101.     /**
  102.      * Sets the Class Name (must be subclass of SimpleXMLElement)
  103.      * @param string $name 
  104.      * @throws Gumbo_Exception
  105.      */
  106.     public function setClassName ($name{
  107.         try {
  108.             // verify precondition
  109.             if (!is_string ($name)) {
  110.                 throw new Gumbo_Exception ("Invalid Argument 'name:str' => {$name}:gettype ($name));
  111.             }
  112.             if (!is_subclass_of ("SimpleXMLElement"$name)) {
  113.                 throw new Gumbo_Exception ("Invalid SubClass to SimpleXMLElement: {$name}");
  114.             }
  115.             
  116.             $this->_class = $name;
  117.         catch (Gumbo_Exception $e{
  118.             $e->setFunction (__METHOD__);
  119.             gumbo_trigger ($e);
  120.         }
  121.     }
  122.     
  123.     /**
  124.      * Sets the Options from LibXML
  125.      * @param int $options 
  126.      * @throws Gumbo_Exception
  127.      */
  128.     public function setOptions ($options{
  129.         try {
  130.             // verify precondition
  131.             if (!is_int ($options)) {
  132.                 throw new Gumbo_Exception ("Invalid Argument 'options:int' => {$options}:gettype ($options));
  133.             }
  134.             
  135.             $this->_options = $options;
  136.         catch (Gumbo_Exception $e{
  137.             $e->setFunction (__METHOD__);
  138.             gumbo_trigger ($e);
  139.         }
  140.     }
  141.     
  142.     /**
  143.      * Sets the Namespace
  144.      * @param string $ns 
  145.      * @throws Gumbo_Exception
  146.      */
  147.     public function setNamespace ($ns{
  148.         try {
  149.             // verify precondition
  150.             if (!is_string ($ns)) {
  151.                 throw new Gumbo_Exception ("Invalid Argument 'ns:str' => {$ns}:gettype ($ns));
  152.             }
  153.             
  154.             $this->_ns = $ns;
  155.         catch (Gumbo_Exception $e{
  156.             $e->setFunction (__METHOD__);
  157.             gumbo_trigger ($e);
  158.         }
  159.     }
  160.     
  161.     /**
  162.      * Sets the Prefix option
  163.      * @param bool $val 
  164.      * @throws Gumbo_Exception
  165.      */
  166.     public function setPrefix ($val{
  167.         try {
  168.             // verify precondition
  169.             if (!is_bool ($val)) {
  170.                 throw new Gumbo_Exception ("Invalid Argument 'val:bool' => {$val}:gettype ($val));
  171.             }
  172.             
  173.             $this->_prefix = $val;
  174.         catch (Gumbo_Exception $e{
  175.             $e->setFunction (__METHOD__);
  176.             gumbo_trigger ($e);
  177.         }
  178.     }
  179.     
  180.     
  181.     
  182.     /** ACCESSOR METHODS **/
  183.     /**
  184.      * Returns the Class Name
  185.      * @return string 
  186.      */
  187.     public function getClassName ({
  188.         return $this->_class;
  189.     }
  190.     
  191.     /**
  192.      * Returns the Options
  193.      * @return int 
  194.      */
  195.     public function getOptions ({
  196.         return $this->_options;
  197.     }
  198.     
  199.     /**
  200.      * Returns the Namespace
  201.      * @return string 
  202.      */
  203.     public function getNamespace ({
  204.         return $this->_ns;
  205.     }
  206.     
  207.     /**
  208.      * Returns the Prefix
  209.      * @return bool 
  210.      */
  211.     public function isPrefix ({
  212.         return $this->_prefix;
  213.     }
  214.     
  215.     
  216.     
  217.     /**
  218.      * Returns a single Composite object from an array
  219.      * This is a recusive method
  220.      * @param SimpleXMLElement $data 
  221.      * @return Gumbo_Interface_Composite 
  222.      * @throws Gumbo_Exception
  223.      * @uses Gumbo_Interface_Composite
  224.      */
  225.     private function _getComposite ($data{
  226.         try {
  227.             // verify precondition
  228.             if (!($data instanceof SimpleXMLElement)) {
  229.                 throw new Gumbo_Exception ("Invalid Argument 'data:SimpleXMLElement' => {$data}:gettype ($data));
  230.             }
  231.             $tree new Gumbo_Branch ($data->getName ());
  232.             
  233.             // set the attributes
  234.             foreach ($data->attributes (as $key=>$val{
  235.                 $tree->addAttribute ($key(string) $val);
  236.             }
  237.             
  238.             // set the child elements
  239.             if ($data->children ()) {
  240.                 foreach ($data->children (as $child{
  241.                     $tmp $this->_getTree ($child);
  242.                     if ($tmp instanceof Gumbo_Interface_Composite{
  243.                         $tree->add ($tmp);
  244.                     }
  245.                 }
  246.             else {
  247.                 $name $data->getName ();
  248.                 $tree->setValue ((string) $data->$name);
  249.             }
  250.             
  251.             return $tree;
  252.         catch (Gumbo_Exception $e{
  253.             $e->setFunction (__METHOD__);
  254.             gumbo_trigger ($e);
  255.         }
  256.         return null;
  257.     }
  258.     
  259. }
  260.  
  261. ?>