StorageConfig.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Config\Converter;
  7. use Magento\Ui\Config\Converter;
  8. use Magento\Ui\Config\ConverterInterface;
  9. use Magento\Framework\ObjectManager\Config\Reader\Dom;
  10. use Magento\Ui\Config\ConverterUtils;
  11. /**
  12. * Converter for data provider storage configuration settings
  13. */
  14. class StorageConfig implements ConverterInterface
  15. {
  16. /**
  17. * @var ConverterInterface
  18. */
  19. private $converter;
  20. /**
  21. * @var ConverterUtils
  22. */
  23. private $converterUtils;
  24. /**
  25. * @param ConverterInterface $converter
  26. * @param ConverterUtils $converterUtils
  27. */
  28. public function __construct(ConverterInterface $converter, ConverterUtils $converterUtils)
  29. {
  30. $this->converter = $converter;
  31. $this->converterUtils = $converterUtils;
  32. }
  33. /**
  34. * @inheritdoc
  35. */
  36. public function convert(\DOMNode $node, array $data = [])
  37. {
  38. if ($node->nodeType !== XML_ELEMENT_NODE) {
  39. return [];
  40. }
  41. return $this->toArray($node);
  42. }
  43. /**
  44. * Convert nodes and child nodes to array
  45. *
  46. * @param \DOMNode $node
  47. * @return array
  48. */
  49. private function toArray(\DOMNode $node)
  50. {
  51. if ($node->localName == 'path' || $node->getAttribute(Dom::TYPE_ATTRIBUTE) == 'url') {
  52. $urlResult = $this->converter->convert($node, ['type' => 'url']);
  53. return $urlResult ?: [];
  54. }
  55. $result = [];
  56. $result[Converter::NAME_ATTRIBUTE_KEY] = $this->converterUtils->getComponentName($node);
  57. if ($this->hasChildElements($node)) {
  58. $result = array_merge($result, $this->processChildNodes($node));
  59. } else {
  60. if ($node->nodeType == XML_ELEMENT_NODE) {
  61. $childResult = [];
  62. $attributes = [];
  63. $childResult[Converter::NAME_ATTRIBUTE_KEY] = $this->converterUtils->getComponentName($node);
  64. $childResult[Dom::TYPE_ATTRIBUTE] = 'string';
  65. if ($node->hasAttributes()) {
  66. foreach ($node->attributes as $attribute) {
  67. $attributes[$attribute->nodeName] = $attribute->value;
  68. }
  69. }
  70. $result = array_merge($childResult, ['value' => trim($node->nodeValue)], $attributes);
  71. }
  72. }
  73. return $result;
  74. }
  75. /**
  76. * Check is DOMNode has child DOMElements
  77. *
  78. * @param \DOMNode $node
  79. * @return bool
  80. */
  81. private function hasChildElements(\DOMNode $node)
  82. {
  83. if ($node->hasChildNodes()) {
  84. foreach ($node->childNodes as $childNode) {
  85. if ($childNode->nodeType == XML_ELEMENT_NODE) {
  86. return true;
  87. }
  88. }
  89. }
  90. return false;
  91. }
  92. /**
  93. * Convert child nodes to array
  94. *
  95. * @param \DOMNode $node
  96. * @return array
  97. */
  98. private function processChildNodes(\DOMNode $node)
  99. {
  100. $result[Dom::TYPE_ATTRIBUTE] = 'array';
  101. /** @var \DOMNode $childNode */
  102. foreach ($node->childNodes as $childNode) {
  103. if ($childNode->nodeType === XML_ELEMENT_NODE) {
  104. $result['item'][$this->converterUtils->getComponentName($childNode)] = $this->toArray($childNode);
  105. }
  106. }
  107. return $result;
  108. }
  109. }