Converter.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Element\UiComponent\Config;
  7. use Magento\Framework\Config\ConverterInterface;
  8. use Magento\Framework\View\Layout\Argument\Parser;
  9. /**
  10. * Class Converter
  11. */
  12. class Converter implements ConverterInterface
  13. {
  14. /**
  15. * The key attributes of a node
  16. */
  17. const DATA_ATTRIBUTES_KEY = '@attributes';
  18. /**
  19. * The key for the data arguments
  20. */
  21. const DATA_ARGUMENTS_KEY = '@arguments';
  22. /**
  23. * The key of the argument node
  24. */
  25. const ARGUMENT_KEY = 'argument';
  26. /**
  27. * Key name attribute value
  28. */
  29. const NAME_ATTRIBUTE_KEY = 'name';
  30. /**
  31. * @var Parser
  32. */
  33. protected $argumentParser;
  34. /**
  35. * Constructor
  36. *
  37. * @param Parser $argumentParser
  38. */
  39. public function __construct(Parser $argumentParser)
  40. {
  41. $this->argumentParser = $argumentParser;
  42. }
  43. /**
  44. * Transform Xml to array
  45. *
  46. * @param \DOMNode $node
  47. * @return array|string
  48. *
  49. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  50. * @SuppressWarnings(PHPMD.NPathComplexity)
  51. */
  52. protected function toArray(\DOMNode $node)
  53. {
  54. $result = [];
  55. $attributes = [];
  56. // Collect data from attributes
  57. if ($node->hasAttributes()) {
  58. foreach ($node->attributes as $attribute) {
  59. $attributes[$attribute->name] = $attribute->value;
  60. }
  61. }
  62. switch ($node->nodeType) {
  63. case XML_TEXT_NODE:
  64. case XML_COMMENT_NODE:
  65. case XML_CDATA_SECTION_NODE:
  66. break;
  67. default:
  68. if ($node->localName === static::ARGUMENT_KEY) {
  69. if (!isset($attributes[static::NAME_ATTRIBUTE_KEY])) {
  70. throw new \InvalidArgumentException(
  71. 'Attribute "' . static::NAME_ATTRIBUTE_KEY . '" is absent in the attributes node.'
  72. );
  73. }
  74. $result[ $attributes[static::NAME_ATTRIBUTE_KEY] ] = $this->argumentParser->parse($node);
  75. } else {
  76. $arguments = [];
  77. for ($i = 0, $iLength = $node->childNodes->length; $i < $iLength; ++$i) {
  78. $itemNode = $node->childNodes->item($i);
  79. if (empty($itemNode->localName)) {
  80. continue;
  81. }
  82. if ($itemNode->nodeName === static::ARGUMENT_KEY) {
  83. $arguments += $this->toArray($itemNode);
  84. } else {
  85. $result[$itemNode->localName][] = $this->toArray($itemNode);
  86. }
  87. }
  88. if (!empty($arguments)) {
  89. $result[static::DATA_ARGUMENTS_KEY] = $arguments;
  90. }
  91. if (!empty($attributes)) {
  92. $result[static::DATA_ATTRIBUTES_KEY] = $attributes;
  93. }
  94. }
  95. break;
  96. }
  97. return $result;
  98. }
  99. /**
  100. * Convert configuration
  101. *
  102. * @param \DOMDocument|null $source
  103. * @return array
  104. */
  105. public function convert($source)
  106. {
  107. if ($source === null) {
  108. return [];
  109. }
  110. return $this->toArray($source);
  111. }
  112. }