Source for file xml.php

Documentation is available at xml.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Registry
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * XML format handler for JRegistry.
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Registry
  17.  * @since       11.1
  18.  */
  19. {
  20.     /**
  21.      * Converts an object into an XML formatted string.
  22.      * -    If more than two levels of nested groups are necessary, since INI is not
  23.      * useful, XML or another format should be used.
  24.      *
  25.      * @param   object  $object   Data source object.
  26.      * @param   array   $options  Options used by the formatter.
  27.      *
  28.      * @return  string  XML formatted string.
  29.      *
  30.      * @since   11.1
  31.      */
  32.     public function objectToString($object$options array())
  33.     {
  34.         $rootName (isset($options['name'])) $options['name''registry';
  35.         $nodeName (isset($options['nodeName'])) $options['nodeName''node';
  36.  
  37.         // Create the root node.
  38.         $root simplexml_load_string('<' $rootName ' />');
  39.  
  40.         // Iterate over the object members.
  41.         $this->getXmlChildren($root$object$nodeName);
  42.  
  43.         return $root->asXML();
  44.     }
  45.  
  46.     /**
  47.      * Parse a XML formatted string and convert it into an object.
  48.      *
  49.      * @param   string  $data     XML formatted string to convert.
  50.      * @param   array   $options  Options used by the formatter.
  51.      *
  52.      * @return  object   Data object.
  53.      *
  54.      * @since   11.1
  55.      */
  56.     public function stringToObject($dataarray $options array())
  57.     {
  58.         $obj new stdClass;
  59.  
  60.         // Parse the XML string.
  61.         $xml simplexml_load_string($data);
  62.  
  63.         foreach ($xml->children(as $node)
  64.         {
  65.             $obj->$node['name'$this->getValueFromNode($node);
  66.         }
  67.  
  68.         return $obj;
  69.     }
  70.  
  71.     /**
  72.      * Method to get a PHP native value for a SimpleXMLElement object. -- called recursively
  73.      *
  74.      * @param   object  $node  SimpleXMLElement object for which to get the native value.
  75.      *
  76.      * @return  mixed  Native value of the SimpleXMLElement object.
  77.      *
  78.      * @since   11.1
  79.      */
  80.     protected function getValueFromNode($node)
  81.     {
  82.         switch ($node['type'])
  83.         {
  84.             case 'integer':
  85.                 $value = (string) $node;
  86.  
  87.                 return (int) $value;
  88.                 break;
  89.             case 'string':
  90.                 return (string) $node;
  91.                 break;
  92.             case 'boolean':
  93.                 $value = (string) $node;
  94.  
  95.                 return (bool) $value;
  96.                 break;
  97.             case 'double':
  98.                 $value = (string) $node;
  99.  
  100.                 return (float) $value;
  101.                 break;
  102.             case 'array':
  103.                 $value array();
  104.  
  105.                 foreach ($node->children(as $child)
  106.                 {
  107.                     $value[(string) $child['name']] $this->getValueFromNode($child);
  108.                 }
  109.                 break;
  110.             default:
  111.                 $value new stdClass;
  112.  
  113.                 foreach ($node->children(as $child)
  114.                 {
  115.                     $value->$child['name'$this->getValueFromNode($child);
  116.                 }
  117.                 break;
  118.         }
  119.  
  120.         return $value;
  121.     }
  122.  
  123.     /**
  124.      * Method to build a level of the XML string -- called recursively
  125.      *
  126.      * @param   SimpleXMLElement  $node      SimpleXMLElement object to attach children.
  127.      * @param   object            $var       Object that represents a node of the XML document.
  128.      * @param   string            $nodeName  The name to use for node elements.
  129.      *
  130.      * @return  void 
  131.      *
  132.      * @since   11.1
  133.      */
  134.     protected function getXmlChildren(SimpleXMLElement $node$var$nodeName)
  135.     {
  136.         // Iterate over the object members.
  137.         foreach ((array) $var as $k => $v)
  138.         {
  139.             if (is_scalar($v))
  140.             {
  141.                 $n $node->addChild($nodeName$v);
  142.                 $n->addAttribute('name'$k);
  143.                 $n->addAttribute('type'gettype($v));
  144.             }
  145.             else
  146.             {
  147.                 $n $node->addChild($nodeName);
  148.                 $n->addAttribute('name'$k);
  149.                 $n->addAttribute('type'gettype($v));
  150.  
  151.                 $this->getXmlChildren($n$v$nodeName);
  152.             }
  153.         }
  154.     }
  155. }

Documentation generated on Tue, 19 Nov 2013 15:18:36 +0100 by phpDocumentor 1.4.3