[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/joomla/registry/src/Format/ -> Xml.php (source)

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


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer