Buttons.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 'settings/buttons' configuration settings
  13. */
  14. class Buttons 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->hasChildNodes()) {
  39. return [
  40. Converter::NAME_ATTRIBUTE_KEY => $this->converterUtils->getComponentName($node),
  41. Dom::TYPE_ATTRIBUTE => 'array',
  42. 'item' => []
  43. ];
  44. }
  45. if ($node->nodeType !== XML_ELEMENT_NODE) {
  46. return [];
  47. }
  48. return $this->toArray($node);
  49. }
  50. /**
  51. * Convert nodes and child nodes to array
  52. *
  53. * @param \DOMNode $node
  54. * @return array
  55. */
  56. private function toArray(\DOMNode $node)
  57. {
  58. if ($node->localName == 'url') {
  59. $urlResult = $this->converter->convert($node, ['type' => 'url']);
  60. return $urlResult ?: [];
  61. }
  62. $result = [];
  63. if ($this->hasChildElements($node)) {
  64. $result = $this->processChildNodes($node);
  65. } else {
  66. if ($node->nodeType == XML_ELEMENT_NODE) {
  67. $childResult = [];
  68. $attributesResult = [];
  69. $childResult[Converter::NAME_ATTRIBUTE_KEY] = $this->converterUtils->getComponentName($node);
  70. $childResult[Dom::TYPE_ATTRIBUTE] = 'string';
  71. if ($node->hasAttributes()) {
  72. $attributesResult = $this->processAttributes($node);
  73. }
  74. $result = array_merge(['value' => trim($node->nodeValue)], $childResult, $attributesResult);
  75. }
  76. }
  77. return $result;
  78. }
  79. /**
  80. * Check is DOMNode has child DOMElements
  81. *
  82. * @param \DOMNode $node
  83. * @return bool
  84. */
  85. private function hasChildElements(\DOMNode $node)
  86. {
  87. if ($node->hasChildNodes()) {
  88. foreach ($node->childNodes as $childNode) {
  89. if ($childNode->nodeType == XML_ELEMENT_NODE) {
  90. return true;
  91. }
  92. }
  93. }
  94. return false;
  95. }
  96. /**
  97. * Collect node attributes
  98. *
  99. * @param \DOMNode $node
  100. * @return array
  101. */
  102. private function processAttributes(\DOMNode $node)
  103. {
  104. $attributes = [];
  105. $childResult = [];
  106. foreach ($node->attributes as $attribute) {
  107. $attributes[$attribute->nodeName] = $attribute->value;
  108. }
  109. if (isset($attributes['class'])) {
  110. $childResult['value'] = $attributes['class'];
  111. unset($attributes['class']);
  112. }
  113. return array_merge($attributes, $childResult);
  114. }
  115. /**
  116. * Convert child nodes to array
  117. *
  118. * @param \DOMNode $node
  119. * @return array
  120. */
  121. private function processChildNodes(\DOMNode $node)
  122. {
  123. $result[Converter::NAME_ATTRIBUTE_KEY] = $this->converterUtils->getComponentName($node);
  124. $result[Dom::TYPE_ATTRIBUTE] = 'array';
  125. /** @var \DOMNode $childNode */
  126. foreach ($node->childNodes as $childNode) {
  127. if ($childNode->nodeType === XML_ELEMENT_NODE) {
  128. $result['item'][$this->converterUtils->getComponentName($childNode)] = $this->toArray($childNode);
  129. }
  130. }
  131. if ($node->nodeType == XML_ELEMENT_NODE && $node->nodeName == 'button') {
  132. $result['item']['name'] = [
  133. Converter::NAME_ATTRIBUTE_KEY => 'name',
  134. 'value' => $node->getAttribute('name'),
  135. Dom::TYPE_ATTRIBUTE => 'string'
  136. ];
  137. }
  138. return $result;
  139. }
  140. }