Url.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 URL type settings
  13. */
  14. class Url implements ConverterInterface
  15. {
  16. /**
  17. * @var ConverterUtils
  18. */
  19. private $converterUtils;
  20. /**
  21. * @param ConverterUtils $converterUtils
  22. */
  23. public function __construct(ConverterUtils $converterUtils)
  24. {
  25. $this->converterUtils = $converterUtils;
  26. }
  27. /**
  28. * @inheritdoc
  29. */
  30. public function convert(\DOMNode $node, array $data = [])
  31. {
  32. if ($node->nodeType !== XML_ELEMENT_NODE) {
  33. return [];
  34. }
  35. return $this->toArray($node);
  36. }
  37. /**
  38. * Convert nodes and child nodes to array
  39. *
  40. * @param \DOMNode $node
  41. * @return array
  42. */
  43. public function toArray(\DOMNode $node)
  44. {
  45. $result[Converter::NAME_ATTRIBUTE_KEY] = $this->converterUtils->getComponentName($node);
  46. if ($node->localName != 'param') {
  47. $result[Dom::TYPE_ATTRIBUTE] = 'url';
  48. }
  49. if ($this->hasChildNodes($node)) {
  50. $result = array_merge($result, $this->processChildNodes($node));
  51. } else {
  52. $nodeValue = trim($node->nodeValue);
  53. if ($nodeValue !== '') {
  54. $result['value'] = $nodeValue;
  55. }
  56. }
  57. if ($node->hasAttributes() && $node->nodeType === XML_ELEMENT_NODE) {
  58. $result = array_merge($result, $this->processAttributes($node));
  59. }
  60. return $result;
  61. }
  62. /**
  63. * Check Check is DOMNode has child DOMElements
  64. *
  65. * @param \DOMNode $node
  66. * @return bool
  67. */
  68. private function hasChildNodes(\DOMNode $node)
  69. {
  70. if ($node->hasChildNodes()) {
  71. foreach ($node->childNodes as $childNode) {
  72. if ($childNode->nodeType == XML_ELEMENT_NODE) {
  73. return true;
  74. }
  75. }
  76. }
  77. return false;
  78. }
  79. /**
  80. * Collect node attributes
  81. *
  82. * @param \DOMNode $node
  83. * @return array
  84. */
  85. private function processAttributes(\DOMNode $node)
  86. {
  87. $attributes = [];
  88. foreach ($node->attributes as $attribute) {
  89. if ($attribute->name == Converter::NAME_ATTRIBUTE_KEY) {
  90. continue;
  91. }
  92. $attributes[$attribute->nodeName] = $attribute->value;
  93. }
  94. return $attributes;
  95. }
  96. /**
  97. * Convert child nodes to array
  98. *
  99. * @param \DOMNode $node
  100. * @return array
  101. */
  102. private function processChildNodes(\DOMNode $node)
  103. {
  104. $result = [];
  105. /** @var \DOMNode $childNode */
  106. foreach ($node->childNodes as $childNode) {
  107. if ($childNode->nodeType === XML_ELEMENT_NODE) {
  108. $result['param'][$this->converterUtils->getComponentName($childNode)] = $this->toArray($childNode);
  109. }
  110. }
  111. return $result;
  112. }
  113. }