OXID eShop CE  4.8.10
 All Classes Files Functions Variables Pages
oxsimplexml.php
Go to the documentation of this file.
1 <?php
2 
7 {
16  protected function _addSimpleXmlElement($oXml, $oInput)
17  {
18  if (is_object( $oInput )) {
19  $aObjectVars = get_object_vars( $oInput );
20  } elseif (is_array($oInput) ) {
21  $aObjectVars = $oInput;
22  } else {
23  return $oXml;
24  }
25 
26  foreach ($aObjectVars as $sKey => $oVar) {
27  if (is_object( $oVar ) ) {
28  $oChildNode = $oXml->addChild($sKey);
29  $this->_addSimpleXmlElement($oChildNode, $oVar);
30  } elseif (is_array( $oVar) ) {
31  foreach ($oVar as $oSubValue) {
32  //this check for complex type is probably redundant, but I give up to solve it over single recursion call
33  //use existing Unit tests for refactoring
34  if (is_array($oSubValue) || is_object($oSubValue)) {
35  $oChildNode = $oXml->addChild($sKey);
36  $this->_addSimpleXmlElement($oChildNode, $oSubValue);
37  } else {
38  $oXml->addChild($sKey, $oSubValue);
39  }
40  }
41  } else {
42  //assume $oVar is string
43  $oXml->addChild($sKey, $oVar);
44  }
45  }
46 
47  return $oXml;
48  }
49 
76  public function objectToXml($oInput, $sDocument)
77  {
78  $oXml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\"?><$sDocument/>");
79  $this->_addSimpleXmlElement($oXml, $oInput);
80 
81  return $oXml->asXml();
82  }
83 
91  public function xmlToObject($sXml)
92  {
93  return simplexml_load_string($sXml);
94  }
95 }