Options.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/options' configuration settings
  13. */
  14. class Options 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. $result[Converter::NAME_ATTRIBUTE_KEY] = $this->converterUtils->getComponentName($node);
  47. if ($this->hasChildElements($node)) {
  48. $result[Dom::TYPE_ATTRIBUTE] = 'array';
  49. /** @var \DOMNode $childNode */
  50. foreach ($node->childNodes as $childNode) {
  51. if ($childNode->nodeType === XML_ELEMENT_NODE) {
  52. $result['item'][$this->converterUtils->getComponentName($childNode)] = $this->toArray($childNode);
  53. }
  54. }
  55. } else {
  56. if ($node->nodeType == XML_ELEMENT_NODE) {
  57. $childResult = [];
  58. $attributes = [];
  59. $childResult[Converter::NAME_ATTRIBUTE_KEY] = $this->converterUtils->getComponentName($node);
  60. $childResult[Dom::TYPE_ATTRIBUTE] = $node->getAttribute(Dom::TYPE_ATTRIBUTE) ?: 'string';
  61. if ($node->hasAttributes()) {
  62. foreach ($node->attributes as $attribute) {
  63. $attributes[$attribute->nodeName] = $attribute->value;
  64. }
  65. }
  66. if (isset($attributes['class'])) {
  67. $childResult[Dom::TYPE_ATTRIBUTE] = 'object';
  68. $childResult['value'] = $attributes['class'];
  69. unset($attributes['class']);
  70. }
  71. $result = array_merge($attributes, ['value' => trim($node->nodeValue)], $childResult);
  72. }
  73. }
  74. return $result;
  75. }
  76. /**
  77. * Check is DOMNode has child DOMElements
  78. *
  79. * @param \DOMNode $node
  80. * @return bool
  81. */
  82. private function hasChildElements(\DOMNode $node)
  83. {
  84. if ($node->hasChildNodes()) {
  85. foreach ($node->childNodes as $childNode) {
  86. if ($childNode->nodeType == XML_ELEMENT_NODE) {
  87. return true;
  88. }
  89. }
  90. }
  91. return false;
  92. }
  93. }