Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Xml.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Config/Writer/FileAbstract.php';
26 
30 #require_once 'Zend/Config/Xml.php';
31 
39 {
46  public function render()
47  {
48  $xml = new SimpleXMLElement('<zend-config xmlns:zf="' . Zend_Config_Xml::XML_NAMESPACE . '"/>');
49  $extends = $this->_config->getExtends();
50  $sectionName = $this->_config->getSectionName();
51 
52  if (is_string($sectionName)) {
53  $child = $xml->addChild($sectionName);
54 
55  $this->_addBranch($this->_config, $child, $xml);
56  } else {
57  foreach ($this->_config as $sectionName => $data) {
58  if (!($data instanceof Zend_Config)) {
59  $xml->addChild($sectionName, (string) $data);
60  } else {
61  $child = $xml->addChild($sectionName);
62 
63  if (isset($extends[$sectionName])) {
64  $child->addAttribute('zf:extends', $extends[$sectionName], Zend_Config_Xml::XML_NAMESPACE);
65  }
66 
67  $this->_addBranch($data, $child, $xml);
68  }
69  }
70  }
71 
72  $dom = dom_import_simplexml($xml)->ownerDocument;
73  $dom->formatOutput = true;
74 
75  $xmlString = $dom->saveXML();
76 
77  return $xmlString;
78  }
79 
88  protected function _addBranch(Zend_Config $config, SimpleXMLElement $xml, SimpleXMLElement $parent)
89  {
90  $branchType = null;
91 
92  foreach ($config as $key => $value) {
93  if ($branchType === null) {
94  if (is_numeric($key)) {
95  $branchType = 'numeric';
96  $branchName = $xml->getName();
97  $xml = $parent;
98 
99  unset($parent->{$branchName});
100  } else {
101  $branchType = 'string';
102  }
103  } else if ($branchType !== (is_numeric($key) ? 'numeric' : 'string')) {
104  #require_once 'Zend/Config/Exception.php';
105  throw new Zend_Config_Exception('Mixing of string and numeric keys is not allowed');
106  }
107 
108  if ($branchType === 'numeric') {
109  if ($value instanceof Zend_Config) {
110  $child = $parent->addChild($branchName);
111 
112  $this->_addBranch($value, $child, $parent);
113  } else {
114  $parent->addChild($branchName, (string) $value);
115  }
116  } else {
117  if ($value instanceof Zend_Config) {
118  $child = $xml->addChild($key);
119 
120  $this->_addBranch($value, $child, $xml);
121  } else {
122  $xml->addChild($key, (string) $value);
123  }
124  }
125  }
126  }
127 }
$config
Definition: fraud_order.php:17
const XML_NAMESPACE
Definition: Xml.php:46
$value
Definition: gender.phtml:16
_addBranch(Zend_Config $config, SimpleXMLElement $xml, SimpleXMLElement $parent)
Definition: Xml.php:88