Converter.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Api\ExtensionAttribute\Config;
  7. class Converter implements \Magento\Framework\Config\ConverterInterface
  8. {
  9. const RESOURCE_PERMISSIONS = "resourceRefs";
  10. const DATA_TYPE = "type";
  11. const JOIN_DIRECTIVE = "join";
  12. const JOIN_REFERENCE_TABLE = "join_reference_table";
  13. const JOIN_REFERENCE_FIELD = "join_reference_field";
  14. const JOIN_ON_FIELD= "join_on_field";
  15. const JOIN_FIELDS = "fields";
  16. const JOIN_FIELD = "field";
  17. const JOIN_FIELD_COLUMN = "column";
  18. /**
  19. * Convert dom node tree to array
  20. *
  21. * @param \DOMDocument $source
  22. * @return array
  23. */
  24. public function convert($source)
  25. {
  26. $output = [];
  27. if (!$source instanceof \DOMDocument) {
  28. return $output;
  29. }
  30. /** @var \DOMNodeList $types */
  31. $types = $source->getElementsByTagName('extension_attributes');
  32. /** @var \DOMNode $type */
  33. foreach ($types as $type) {
  34. $typeConfig = [];
  35. $typeName = $type->getAttribute('for');
  36. $attributes = $type->getElementsByTagName('attribute');
  37. foreach ($attributes as $attribute) {
  38. $code = $attribute->getAttribute('code');
  39. $codeType = $attribute->getAttribute('type');
  40. $resourcesElement = $attribute->getElementsByTagName('resources')->item(0);
  41. $resourceRefs = [];
  42. if ($resourcesElement && $resourcesElement->nodeType === XML_ELEMENT_NODE) {
  43. $singleResourceElements = $resourcesElement->getElementsByTagName('resource');
  44. foreach ($singleResourceElements as $element) {
  45. if ($element->nodeType != XML_ELEMENT_NODE) {
  46. continue;
  47. }
  48. $resourceRefs[] = $element->attributes->getNamedItem('ref')->nodeValue;
  49. }
  50. }
  51. $joinElement = $attribute->getElementsByTagName('join')->item(0);
  52. $join = $this->processJoinElement($joinElement, $attribute);
  53. $typeConfig[$code] = [
  54. self::DATA_TYPE => $codeType,
  55. self::RESOURCE_PERMISSIONS => $resourceRefs,
  56. self::JOIN_DIRECTIVE => $join,
  57. ];
  58. }
  59. $output[$typeName] = $typeConfig;
  60. }
  61. return $output;
  62. }
  63. /**
  64. * Process the join element configuration
  65. *
  66. * @param \DOMElement $joinElement
  67. * @param \DOMElement $attribute
  68. * @return array
  69. */
  70. private function processJoinElement($joinElement, $attribute)
  71. {
  72. $join = null;
  73. if ($joinElement && $joinElement->nodeType === XML_ELEMENT_NODE) {
  74. $joinAttributes = $joinElement->attributes;
  75. $join = [
  76. self::JOIN_REFERENCE_TABLE => $joinAttributes->getNamedItem('reference_table')->nodeValue,
  77. self::JOIN_ON_FIELD => $joinAttributes->getNamedItem('join_on_field')->nodeValue,
  78. self::JOIN_REFERENCE_FIELD => $joinAttributes->getNamedItem('reference_field')->nodeValue,
  79. ];
  80. $fields = $attribute->getElementsByTagName('field');
  81. foreach ($fields as $field) {
  82. $column = $field->getAttribute('column');
  83. $join[self::JOIN_FIELDS][] = [
  84. self::JOIN_FIELD => $field->nodeValue,
  85. self::JOIN_FIELD_COLUMN => $column
  86. ];
  87. }
  88. }
  89. return $join;
  90. }
  91. }