Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Generator.php
Go to the documentation of this file.
1 <?php
7 
8 class Generator
9 {
13  const DEFAULT_ENTITY_ITEM_NAME = 'item';
14 
18  protected $_dom = null;
19 
23  protected $_currentDom;
24 
29 
33  public function __construct()
34  {
35  $this->_dom = new \DOMDocument('1.0');
36  $this->_dom->formatOutput = true;
37  $this->_currentDom = $this->_dom;
38  return $this;
39  }
40 
44  public function getDom()
45  {
46  return $this->_dom;
47  }
48 
52  protected function _getCurrentDom()
53  {
54  return $this->_currentDom;
55  }
56 
61  protected function _setCurrentDom($node)
62  {
63  $this->_currentDom = $node;
64  return $this;
65  }
66 
73  public function arrayToXml($content)
74  {
75  $parentNode = $this->_getCurrentDom();
76  if (!$content || !count($content)) {
77  return $this;
78  }
79  foreach ($content as $_key => $_item) {
80  $node = $this->getDom()->createElement(preg_replace('/[^\w-]/i', '', $_key));
81  $parentNode->appendChild($node);
82  if (is_array($_item) && isset($_item['_attribute'])) {
83  if (is_array($_item['_value'])) {
84  if (isset($_item['_value'][0])) {
85  foreach ($_item['_value'] as $_v) {
86  $this->_setCurrentDom($node)->arrayToXml($_v);
87  }
88  } else {
89  $this->_setCurrentDom($node)->arrayToXml($_item['_value']);
90  }
91  } else {
92  $child = $this->getDom()->createTextNode($_item['_value']);
93  $node->appendChild($child);
94  }
95  foreach ($_item['_attribute'] as $_attributeKey => $_attributeValue) {
96  $node->setAttribute($_attributeKey, $_attributeValue);
97  }
98  } elseif (is_string($_item)) {
99  $text = $this->getDom()->createTextNode($_item);
100  $node->appendChild($text);
101  } elseif (is_array($_item) && !isset($_item[0])) {
102  $this->_setCurrentDom($node)->arrayToXml($_item);
103  } elseif (is_array($_item) && isset($_item[0])) {
104  foreach ($_item as $v) {
105  $this->_setCurrentDom($node)->arrayToXml([$this->_getIndexedArrayItemName() => $v]);
106  }
107  }
108  }
109  return $this;
110  }
111 
115  public function __toString()
116  {
117  return $this->getDom()->saveXML();
118  }
119 
124  public function save($file)
125  {
126  $this->getDom()->save($file);
127  return $this;
128  }
129 
137  {
138  $this->_defaultIndexedArrayItemName = $name;
139  return $this;
140  }
141 
147  protected function _getIndexedArrayItemName()
148  {
149  return $this->_defaultIndexedArrayItemName ?? self::DEFAULT_ENTITY_ITEM_NAME;
150  }
151 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
endifif( $block->getLastPageNum()>1)( 'Page') ?></strong >< ul class $text
Definition: pager.phtml:43
$_item
Definition: default.phtml:11
if(!isset($_GET['name'])) $name
Definition: log.php:14