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 {
10  const RESOURCE_PERMISSIONS = "resourceRefs";
11  const DATA_TYPE = "type";
12 
13  const JOIN_DIRECTIVE = "join";
14  const JOIN_REFERENCE_TABLE = "join_reference_table";
15  const JOIN_REFERENCE_FIELD = "join_reference_field";
16  const JOIN_ON_FIELD= "join_on_field";
17 
18  const JOIN_FIELDS = "fields";
19  const JOIN_FIELD = "field";
20  const JOIN_FIELD_COLUMN = "column";
21 
28  public function convert($source)
29  {
30  $output = [];
31  if (!$source instanceof \DOMDocument) {
32  return $output;
33  }
34 
36  $types = $source->getElementsByTagName('extension_attributes');
38  foreach ($types as $type) {
39  $typeConfig = [];
40  $typeName = $type->getAttribute('for');
41 
42  $attributes = $type->getElementsByTagName('attribute');
43  foreach ($attributes as $attribute) {
44  $code = $attribute->getAttribute('code');
45  $codeType = $attribute->getAttribute('type');
46 
47  $resourcesElement = $attribute->getElementsByTagName('resources')->item(0);
48  $resourceRefs = [];
49  if ($resourcesElement && $resourcesElement->nodeType === XML_ELEMENT_NODE) {
50  $singleResourceElements = $resourcesElement->getElementsByTagName('resource');
51  foreach ($singleResourceElements as $element) {
52  if ($element->nodeType != XML_ELEMENT_NODE) {
53  continue;
54  }
55  $resourceRefs[] = $element->attributes->getNamedItem('ref')->nodeValue;
56  }
57  }
58 
59  $joinElement = $attribute->getElementsByTagName('join')->item(0);
60  $join = $this->processJoinElement($joinElement, $attribute);
61 
62  $typeConfig[$code] = [
63  self::DATA_TYPE => $codeType,
64  self::RESOURCE_PERMISSIONS => $resourceRefs,
65  self::JOIN_DIRECTIVE => $join,
66  ];
67  }
68 
69  $output[$typeName] = $typeConfig;
70  }
71  return $output;
72  }
73 
81  private function processJoinElement($joinElement, $attribute)
82  {
83  $join = null;
84  if ($joinElement && $joinElement->nodeType === XML_ELEMENT_NODE) {
85  $joinAttributes = $joinElement->attributes;
86  $join = [
87  self::JOIN_REFERENCE_TABLE => $joinAttributes->getNamedItem('reference_table')->nodeValue,
88  self::JOIN_ON_FIELD => $joinAttributes->getNamedItem('join_on_field')->nodeValue,
89  self::JOIN_REFERENCE_FIELD => $joinAttributes->getNamedItem('reference_field')->nodeValue,
90  ];
91  $fields = $attribute->getElementsByTagName('field');
92  foreach ($fields as $field) {
93  $column = $field->getAttribute('column');
94  $join[self::JOIN_FIELDS][] = [
95  self::JOIN_FIELD => $field->nodeValue,
96  self::JOIN_FIELD_COLUMN => $column
97  ];
98  }
99  }
100 
101  return $join;
102  }
103 }
$source
Definition: source.php:23
$fields
Definition: details.phtml:14
$type
Definition: item.phtml:13
$attributes
Definition: matrix.phtml:13
$code
Definition: info.phtml:12
$element
Definition: element.phtml:12