Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Converter.php
Go to the documentation of this file.
1 <?php
7 
13 {
17  protected $_mapperFactory;
18 
24  protected $_mapperList = [
25  \Magento\Config\Model\Config\Structure\Mapper\Factory::MAPPER_EXTENDS,
26  \Magento\Config\Model\Config\Structure\Mapper\Factory::MAPPER_PATH,
27  \Magento\Config\Model\Config\Structure\Mapper\Factory::MAPPER_DEPENDENCIES,
28  \Magento\Config\Model\Config\Structure\Mapper\Factory::MAPPER_ATTRIBUTE_INHERITANCE,
29  \Magento\Config\Model\Config\Structure\Mapper\Factory::MAPPER_IGNORE,
30  \Magento\Config\Model\Config\Structure\Mapper\Factory::MAPPER_SORTING,
31  ];
32 
40  protected $_nameMap = [
41  'system' => ['tab' => 'tabs', 'section' => 'sections'],
42  'section' => ['group' => 'children'],
43  'group' => ['field' => 'children', 'group' => 'children'],
44  'depends' => ['field' => 'fields'],
45  ];
46 
50  public function __construct(\Magento\Config\Model\Config\Structure\Mapper\Factory $mapperFactory)
51  {
52  $this->_mapperFactory = $mapperFactory;
53  }
54 
61  public function convert($source)
62  {
63  $result = $this->_convertDOMDocument($source);
64 
65  foreach ($this->_mapperList as $type) {
67  $mapper = $this->_mapperFactory->create($type);
68  $result = $mapper->map($result);
69  }
70 
71  return $result;
72  }
73 
82  protected function _convertDOMDocument(\DOMNode $root)
83  {
84  $result = $this->_processAttributes($root);
85 
86  $children = $root->childNodes;
87 
88  $processedSubLists = [];
89  for ($i = 0; $i < $children->length; $i++) {
90  $child = $children->item($i);
91  $childName = $child->nodeName;
92  $convertedChild = [];
93 
94  switch ($child->nodeType) {
95  case XML_COMMENT_NODE:
96  continue 2;
97  break;
98 
99  case XML_TEXT_NODE:
100  if ($children->length && trim($child->nodeValue, "\n ") === '') {
101  continue 2;
102  }
103  $childName = 'value';
104  $convertedChild = $child->nodeValue;
105  break;
106 
107  case XML_CDATA_SECTION_NODE:
108  $childName = 'value';
109  $convertedChild = $child->nodeValue;
110  break;
111 
112  default:
114  if ($childName == 'attribute') {
115  $childName = $child->getAttribute('type');
116  }
117  $convertedChild = $this->_convertDOMDocument($child);
118  break;
119  }
120 
121  if (array_key_exists(
122  $root->nodeName,
123  $this->_nameMap
124  ) && array_key_exists(
125  $child->nodeName,
126  $this->_nameMap[$root->nodeName]
127  )
128  ) {
129  $childName = $this->_nameMap[$root->nodeName][$child->nodeName];
130  $processedSubLists[] = $childName;
131  $convertedChild['_elementType'] = $child->nodeName;
132  }
133 
134  if (in_array($childName, $processedSubLists)) {
135  $result = $this->_addProcessedNode($convertedChild, $result, $childName);
136  } elseif (array_key_exists($childName, $result)) {
137  $result[$childName] = [$result[$childName], $convertedChild];
138  $processedSubLists[] = $childName;
139  } else {
140  $result[$childName] = $convertedChild;
141  }
142  }
143 
144  if (count($result) == 1 && array_key_exists('value', $result)) {
145  $result = $result['value'];
146  }
147  if ($result == []) {
148  $result = null;
149  }
150 
151  return $result;
152  }
153 
162  protected function _addProcessedNode($convertedChild, $result, $childName)
163  {
164  if (is_array($convertedChild) && array_key_exists('id', $convertedChild)) {
165  $result[$childName][$convertedChild['id']] = $convertedChild;
166  } else {
167  $result[$childName][] = $convertedChild;
168  }
169  return $result;
170  }
171 
178  protected function _processAttributes(\DOMNode $root)
179  {
180  $result = [];
181 
182  if ($root->hasAttributes()) {
183  $attributes = $root->attributes;
184  foreach ($attributes as $attribute) {
185  if ($root->nodeName == 'attribute' && $attribute->name == 'type') {
186  continue;
187  }
188  $result[$attribute->name] = $attribute->value;
189  }
190  return $result;
191  }
192  return $result;
193  }
194 }
_addProcessedNode($convertedChild, $result, $childName)
Definition: Converter.php:162
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$source
Definition: source.php:23
$type
Definition: item.phtml:13
__construct(\Magento\Config\Model\Config\Structure\Mapper\Factory $mapperFactory)
Definition: Converter.php:50
$attributes
Definition: matrix.phtml:13
$children
Definition: actions.phtml:11
$i
Definition: gallery.phtml:31