Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
xml.php
Aller à la documentation de ce fichier.
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  */
20 {
21  /**
22  * Converts an object into an XML formatted string.
23  * - If more than two levels of nested groups are necessary, since INI is not
24  * useful, XML or another format should be used.
25  *
26  * @param object $object Data source object.
27  * @param array $options Options used by the formatter.
28  *
29  * @return string XML formatted string.
30  *
31  * @since 11.1
32  */
33  public function objectToString($object, $options = array())
34  {
35  $rootName = (isset($options['name'])) ? $options['name'] : 'registry';
36  $nodeName = (isset($options['nodeName'])) ? $options['nodeName'] : 'node';
37 
38  // Create the root node.
39  $root = simplexml_load_string('<' . $rootName . ' />');
40 
41  // Iterate over the object members.
42  $this->getXmlChildren($root, $object, $nodeName);
43 
44  return $root->asXML();
45  }
46 
47  /**
48  * Parse a XML formatted string and convert it into an object.
49  *
50  * @param string $data XML formatted string to convert.
51  * @param array $options Options used by the formatter.
52  *
53  * @return object Data object.
54  *
55  * @since 11.1
56  */
57  public function stringToObject($data, array $options = array())
58  {
59  $obj = new stdClass;
60 
61  // Parse the XML string.
62  $xml = simplexml_load_string($data);
63 
64  foreach ($xml->children() as $node)
65  {
66  $obj->$node['name'] = $this->getValueFromNode($node);
67  }
68 
69  return $obj;
70  }
71 
72  /**
73  * Method to get a PHP native value for a SimpleXMLElement object. -- called recursively
74  *
75  * @param object $node SimpleXMLElement object for which to get the native value.
76  *
77  * @return mixed Native value of the SimpleXMLElement object.
78  *
79  * @since 11.1
80  */
81  protected function getValueFromNode($node)
82  {
83  switch ($node['type'])
84  {
85  case 'integer':
86  $value = (string) $node;
87 
88  return (int) $value;
89  break;
90  case 'string':
91  return (string) $node;
92  break;
93  case 'boolean':
94  $value = (string) $node;
95 
96  return (bool) $value;
97  break;
98  case 'double':
99  $value = (string) $node;
100 
101  return (float) $value;
102  break;
103  case 'array':
104  $value = array();
105 
106  foreach ($node->children() as $child)
107  {
108  $value[(string) $child['name']] = $this->getValueFromNode($child);
109  }
110  break;
111  default:
112  $value = new stdClass;
113 
114  foreach ($node->children() as $child)
115  {
116  $value->$child['name'] = $this->getValueFromNode($child);
117  }
118  break;
119  }
120 
121  return $value;
122  }
123 
124  /**
125  * Method to build a level of the XML string -- called recursively
126  *
127  * @param SimpleXMLElement $node SimpleXMLElement object to attach children.
128  * @param object $var Object that represents a node of the XML document.
129  * @param string $nodeName The name to use for node elements.
130  *
131  * @return void
132  *
133  * @since 11.1
134  */
135  protected function getXmlChildren(SimpleXMLElement $node, $var, $nodeName)
136  {
137  // Iterate over the object members.
138  foreach ((array) $var as $k => $v)
139  {
140  if (is_scalar($v))
141  {
142  $n = $node->addChild($nodeName, $v);
143  $n->addAttribute('name', $k);
144  $n->addAttribute('type', gettype($v));
145  }
146  else
147  {
148  $n = $node->addChild($nodeName);
149  $n->addAttribute('name', $k);
150  $n->addAttribute('type', gettype($v));
151 
152  $this->getXmlChildren($n, $v, $nodeName);
153  }
154  }
155  }
156 }