Deps.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 'settings/deps' configuration settings
  13. */
  14. class Deps 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. private function toArray(\DOMNode $node)
  44. {
  45. $result = [
  46. 'name' => $this->converterUtils->getComponentName($node),
  47. Dom::TYPE_ATTRIBUTE => 'array'
  48. ];
  49. if ($this->hasChildNodes($node)) {
  50. $i = 0;
  51. /** @var \DOMNode $childNode */
  52. foreach ($node->childNodes as $childNode) {
  53. if ($childNode->nodeType === XML_ELEMENT_NODE) {
  54. $result['item'][] = [
  55. 'name' => $i,
  56. Dom::TYPE_ATTRIBUTE => 'string',
  57. 'value' => trim($childNode->nodeValue)
  58. ];
  59. $i++;
  60. }
  61. }
  62. } else {
  63. $result['item'][] = [
  64. 'name' => 0,
  65. Dom::TYPE_ATTRIBUTE => 'string',
  66. 'value' => trim($node->nodeValue)
  67. ];
  68. }
  69. return $result;
  70. }
  71. /**
  72. * Check is DOMNode has child DOMElements
  73. *
  74. * @param \DOMNode $node
  75. * @return bool
  76. */
  77. private function hasChildNodes(\DOMNode $node)
  78. {
  79. if (!$node->hasChildNodes()) {
  80. return false;
  81. }
  82. foreach ($node->childNodes as $child) {
  83. if ($child->nodeType == XML_ELEMENT_NODE) {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. }