Actions.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/actions' configuration settings
  13. */
  14. class Actions 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 == '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[Dom::TYPE_ATTRIBUTE] = 'array';
  59. /** @var \DOMNode $childNode */
  60. foreach ($node->childNodes as $childNode) {
  61. if ($childNode->nodeType === XML_ELEMENT_NODE) {
  62. $result['item'][$this->converterUtils->getComponentName($childNode)] = $this->toArray($childNode);
  63. }
  64. }
  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[Dom::TYPE_ATTRIBUTE] = 'object';
  111. $childResult['value'] = $attributes['class'];
  112. unset($attributes['class']);
  113. }
  114. return array_merge($attributes, $childResult);
  115. }
  116. }