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 
9 {
15  public function convert($source)
16  {
17  $widgets = [];
18  $xpath = new \DOMXPath($source);
20  foreach ($xpath->query('/widgets/widget') as $widget) {
21  $widgetAttributes = $widget->attributes;
22  $widgetArray = ['@' => []];
23  $widgetArray['@']['type'] = $widgetAttributes->getNamedItem('class')->nodeValue;
24 
25  $isEmailCompatible = $widgetAttributes->getNamedItem('is_email_compatible');
26  if ($isEmailCompatible !== null) {
27  $widgetArray['is_email_compatible'] = $isEmailCompatible->nodeValue == 'true' ? '1' : '0';
28  }
29  $placeholderImage = $widgetAttributes->getNamedItem('placeholder_image');
30  if ($placeholderImage !== null) {
31  $widgetArray['placeholder_image'] = $placeholderImage->nodeValue;
32  }
33 
34  $widgetId = $widgetAttributes->getNamedItem('id');
36  foreach ($widget->childNodes as $widgetSubNode) {
37  switch ($widgetSubNode->nodeName) {
38  case 'label':
39  $widgetArray['name'] = $widgetSubNode->nodeValue;
40  break;
41  case 'description':
42  $widgetArray['description'] = $widgetSubNode->nodeValue;
43  break;
44  case 'parameters':
46  foreach ($widgetSubNode->childNodes as $parameter) {
47  if ($parameter->nodeName === '#text' || $parameter->nodeName === '#comment') {
48  continue;
49  }
50  $subNodeAttributes = $parameter->attributes;
51  $parameterName = $subNodeAttributes->getNamedItem('name')->nodeValue;
52  $widgetArray['parameters'][$parameterName] = $this->_convertParameter($parameter);
53  }
54  break;
55  case 'containers':
56  if (!isset($widgetArray['supported_containers'])) {
57  $widgetArray['supported_containers'] = [];
58  }
59  foreach ($widgetSubNode->childNodes as $container) {
60  if ($container->nodeName === '#text' || $container->nodeName === '#comment') {
61  continue;
62  }
63  $widgetArray['supported_containers'] = array_merge(
64  $widgetArray['supported_containers'],
65  $this->_convertContainer($container)
66  );
67  }
68  break;
69  case "#text":
70  break;
71  case '#comment':
72  break;
73  default:
74  throw new \LogicException(
75  sprintf(
76  "Unsupported child xml node '%s' found in the 'widget' node",
77  $widgetSubNode->nodeName
78  )
79  );
80  }
81  }
82  $widgets[$widgetId->nodeValue] = $widgetArray;
83  }
84  return $widgets;
85  }
86 
94  protected function _convertContainer($source)
95  {
96  $supportedContainers = [];
97  $containerAttributes = $source->attributes;
98  $template = [];
99  foreach ($source->childNodes as $containerTemplate) {
100  if (!$containerTemplate instanceof \DOMElement) {
101  continue;
102  }
103  if ($containerTemplate->nodeName !== 'template') {
104  throw new \LogicException("Only 'template' node can be child of 'container' node");
105  }
106  $templateAttributes = $containerTemplate->attributes;
107  $template[$templateAttributes->getNamedItem(
108  'name'
109  )->nodeValue] = $templateAttributes->getNamedItem(
110  'value'
111  )->nodeValue;
112  }
113  $supportedContainers[] = [
114  'container_name' => $containerAttributes->getNamedItem('name')->nodeValue,
115  'template' => $template,
116  ];
117  return $supportedContainers;
118  }
119 
129  protected function _convertParameter($source)
130  {
131  $parameter = [];
132  $sourceAttributes = $source->attributes;
133  $xsiType = $sourceAttributes->getNamedItem('type')->nodeValue;
134  if ($xsiType == 'block') {
135  $parameter['type'] = 'label';
136  $parameter['@'] = [];
137  $parameter['@']['type'] = 'complex';
138  foreach ($source->childNodes as $blockSubNode) {
139  if ($blockSubNode->nodeName == 'block') {
140  $parameter['helper_block'] = $this->_convertBlock($blockSubNode);
141  break;
142  }
143  }
144  } elseif ($xsiType == 'select' || $xsiType == 'multiselect') {
145  $sourceModel = $sourceAttributes->getNamedItem('source_model');
146  if ($sourceModel !== null) {
147  $parameter['source_model'] = $sourceModel->nodeValue;
148  }
149  $parameter['type'] = $xsiType;
150 
152  foreach ($source->childNodes as $paramSubNode) {
153  if ($paramSubNode->nodeName == 'options') {
155  foreach ($paramSubNode->childNodes as $option) {
156  if ($option->nodeName === '#text') {
157  continue;
158  }
159  $optionAttributes = $option->attributes;
160  $optionName = $optionAttributes->getNamedItem('name')->nodeValue;
161  $selected = $optionAttributes->getNamedItem('selected');
162  if ($selected !== null) {
163  $parameter['value'] = $optionAttributes->getNamedItem('value')->nodeValue;
164  }
165  if (!isset($parameter['values'])) {
166  $parameter['values'] = [];
167  }
168  $parameter['values'][$optionName] = $this->_convertOption($option);
169  }
170  }
171  }
172  } elseif ($xsiType == 'text') {
173  $parameter['type'] = $xsiType;
174  foreach ($source->childNodes as $textSubNode) {
175  if ($textSubNode->nodeName == 'value') {
176  $parameter['value'] = $textSubNode->nodeValue;
177  }
178  }
179  } elseif ($xsiType == 'conditions') {
180  $parameter['type'] = $sourceAttributes->getNamedItem('class')->nodeValue;
181  } else {
182  $parameter['type'] = $xsiType;
183  }
184  $visible = $sourceAttributes->getNamedItem('visible');
185  if ($visible) {
186  $parameter['visible'] = $visible->nodeValue == 'true' ? '1' : '0';
187  } else {
188  $parameter['visible'] = true;
189  }
190  $required = $sourceAttributes->getNamedItem('required');
191  if ($required) {
192  $parameter['required'] = $required->nodeValue == 'false' ? '0' : '1';
193  }
194  $sortOrder = $sourceAttributes->getNamedItem('sort_order');
195  if ($sortOrder) {
196  $parameter['sort_order'] = $sortOrder->nodeValue;
197  }
198  foreach ($source->childNodes as $paramSubNode) {
199  switch ($paramSubNode->nodeName) {
200  case 'label':
201  $parameter['label'] = $paramSubNode->nodeValue;
202  break;
203  case 'description':
204  $parameter['description'] = $paramSubNode->nodeValue;
205  break;
206  case 'depends':
207  $parameter['depends'] = $this->_convertDepends($paramSubNode);
208  break;
209  }
210  }
211  return $parameter;
212  }
213 
221  protected function _convertDepends($source)
222  {
223  $depends = [];
224  foreach ($source->childNodes as $childNode) {
225  if ($childNode->nodeName === '#text') {
226  continue;
227  }
228  if ($childNode->nodeName !== 'parameter') {
229  throw new \LogicException(
230  sprintf("Only 'parameter' node can be child of 'depends' node, %s found", $childNode->nodeName)
231  );
232  }
233  $parameterAttributes = $childNode->attributes;
234  $dependencyName = $parameterAttributes->getNamedItem('name')->nodeValue;
235  $dependencyValue = $parameterAttributes->getNamedItem('value')->nodeValue;
236 
237  if (!isset($depends[$dependencyName])) {
238  $depends[$dependencyName] = [
239  'value' => $dependencyValue,
240  ];
241 
242  continue;
243  } else if (!isset($depends[$dependencyName]['values'])) {
244  $depends[$dependencyName]['values'] = [$depends[$dependencyName]['value']];
245  unset($depends[$dependencyName]['value']);
246  }
247 
248  $depends[$dependencyName]['values'][] = $dependencyValue;
249  }
250 
251  return $depends;
252  }
253 
261  protected function _convertBlock($source)
262  {
263  $helperBlock = [];
264  $helperBlock['type'] = $source->attributes->getNamedItem('class')->nodeValue;
265  foreach ($source->childNodes as $blockSubNode) {
266  if ($blockSubNode->nodeName == '#text') {
267  continue;
268  }
269  if ($blockSubNode->nodeName !== 'data') {
270  throw new \LogicException(
271  sprintf("Only 'data' node can be child of 'block' node, %s found", $blockSubNode->nodeName)
272  );
273  }
274  $helperBlock['data'] = $this->_convertData($blockSubNode);
275  }
276  return $helperBlock;
277  }
278 
285  protected function _convertData($source)
286  {
287  $data = [];
288  if (!$source->hasChildNodes()) {
289  return $data;
290  }
291  foreach ($source->childNodes as $dataChild) {
292  if ($dataChild instanceof \DOMElement) {
293  $data[$dataChild->attributes->getNamedItem('name')->nodeValue] = $this->_convertData($dataChild);
294  } else {
295  if (strlen(trim($dataChild->nodeValue))) {
296  $data = $dataChild->nodeValue;
297  }
298  }
299  }
300  return $data;
301  }
302 
310  protected function _convertOption($source)
311  {
312  $option = [];
313  $optionAttributes = $source->attributes;
314  $option['value'] = $optionAttributes->getNamedItem('value')->nodeValue;
315  foreach ($source->childNodes as $childNode) {
316  if ($childNode->nodeName == '#text') {
317  continue;
318  }
319  if ($childNode->nodeName !== 'label') {
320  throw new \LogicException("Only 'label' node can be child of 'option' node");
321  }
322  $option['label'] = $childNode->nodeValue;
323  }
324  return $option;
325  }
326 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$source
Definition: source.php:23
$template
Definition: export.php:12
$required
Definition: wrapper.phtml:8