Communication.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\ConverterInterface;
  9. use Magento\Ui\Config\ConverterUtils;
  10. /**
  11. * Converter for "communication" types of configuration settings ('imports', 'exports', 'links', etc)
  12. */
  13. class Communication implements ConverterInterface
  14. {
  15. /**
  16. * @var ConverterUtils
  17. */
  18. private $converterUtils;
  19. /**
  20. * @param ConverterUtils $converterUtils
  21. */
  22. public function __construct(ConverterUtils $converterUtils)
  23. {
  24. $this->converterUtils = $converterUtils;
  25. }
  26. /**
  27. * @inheritdoc
  28. */
  29. public function convert(\DOMNode $node, array $data = [])
  30. {
  31. if ($node->nodeType !== XML_ELEMENT_NODE) {
  32. return [];
  33. }
  34. return $this->toArray($node);
  35. }
  36. /**
  37. * Convert nodes and child nodes to array
  38. *
  39. * @param \DOMNode $node
  40. * @return array
  41. */
  42. private function toArray(\DOMNode $node)
  43. {
  44. $result = [
  45. 'name' => $this->converterUtils->getComponentName($node),
  46. Dom::TYPE_ATTRIBUTE => 'array'
  47. ];
  48. if ($this->hasChildNodes($node)) {
  49. /** @var \DOMNode $childNode */
  50. foreach ($node->childNodes as $childNode) {
  51. if ($childNode->nodeType === XML_ELEMENT_NODE) {
  52. $childNodeName = $this->converterUtils->getComponentName($childNode);
  53. $result['item'][$childNodeName] = [
  54. 'name' => $childNodeName,
  55. Dom::TYPE_ATTRIBUTE => 'string',
  56. 'value' => trim($childNode->nodeValue)
  57. ];
  58. }
  59. }
  60. }
  61. return $result;
  62. }
  63. /**
  64. * Check is DOMNode has child DOMElements
  65. *
  66. * @param \DOMNode $node
  67. * @return bool
  68. */
  69. private function hasChildNodes(\DOMNode $node)
  70. {
  71. if (!$node->hasChildNodes()) {
  72. return false;
  73. }
  74. foreach ($node->childNodes as $child) {
  75. if ($child->nodeType == XML_ELEMENT_NODE) {
  76. return true;
  77. }
  78. }
  79. return false;
  80. }
  81. }