Item.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\Framework\ObjectManager\Config\Reader\Dom;
  8. use Magento\Ui\Config\Converter;
  9. use Magento\Ui\Config\ConverterInterface;
  10. use Magento\Ui\Config\ConverterUtils;
  11. /**
  12. * Converter for array inner items
  13. */
  14. class Item 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->nodeType == XML_ELEMENT_NODE && $node->getAttribute(Dom::TYPE_ATTRIBUTE) == 'url') {
  52. $urlResult = $this->converter->convert($node, ['type' => 'url']);
  53. return $urlResult ?: [];
  54. }
  55. $result[Converter::NAME_ATTRIBUTE_KEY] = $this->converterUtils->getComponentName($node);
  56. if ($this->hasChildNodes($node)) {
  57. $result = array_merge($result, $this->processChildNodes($node));
  58. } else {
  59. $result[Dom::TYPE_ATTRIBUTE] = 'string';
  60. if (trim($node->nodeValue) != '') {
  61. $result['value'] = trim($node->nodeValue);
  62. }
  63. }
  64. if ($node->hasAttributes() && $node->nodeType === XML_ELEMENT_NODE) {
  65. $result = array_merge($result, $this->processAttributes($node));
  66. }
  67. return $result;
  68. }
  69. /**
  70. * Check is DOMNode has child DOMElements
  71. *
  72. * @param \DOMNode $node
  73. * @return bool
  74. */
  75. private function hasChildNodes(\DOMNode $node)
  76. {
  77. if ($node->hasChildNodes()) {
  78. foreach ($node->childNodes as $childNode) {
  79. if ($childNode->nodeType == XML_ELEMENT_NODE) {
  80. return true;
  81. }
  82. }
  83. }
  84. return false;
  85. }
  86. /**
  87. * Collect node attributes
  88. *
  89. * @param \DOMNode $node
  90. * @return array
  91. */
  92. private function processAttributes(\DOMNode $node)
  93. {
  94. $attributes = [];
  95. foreach ($node->attributes as $attribute) {
  96. if ($attribute->name == Converter::NAME_ATTRIBUTE_KEY) {
  97. continue;
  98. }
  99. $attributes[$attribute->nodeName] = $attribute->value;
  100. }
  101. return $attributes;
  102. }
  103. /**
  104. * Convert child nodes to array
  105. *
  106. * @param \DOMNode $node
  107. * @return array
  108. */
  109. private function processChildNodes(\DOMNode $node)
  110. {
  111. $result[Dom::TYPE_ATTRIBUTE] = 'array';
  112. /** @var \DOMNode $childNode */
  113. foreach ($node->childNodes as $childNode) {
  114. if ($childNode->nodeType === XML_ELEMENT_NODE) {
  115. $result['item'][$this->converterUtils->getComponentName($childNode)] = $this->toArray($childNode);
  116. }
  117. }
  118. return $result;
  119. }
  120. }