Converter.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Config;
  7. use Magento\Framework\View\Xsd\Media\TypeDataExtractorPool;
  8. /**
  9. * Class Converter convert xml to appropriate array
  10. *
  11. * @package Magento\Framework\Config
  12. */
  13. class Converter implements \Magento\Framework\Config\ConverterInterface
  14. {
  15. /**
  16. * @var \Magento\Framework\View\Xsd\Media\TypeDataExtractorPool
  17. */
  18. protected $extractorPool;
  19. /**
  20. * @param TypeDataExtractorPool $extractorPool
  21. */
  22. public function __construct(TypeDataExtractorPool $extractorPool)
  23. {
  24. $this->extractorPool = $extractorPool;
  25. }
  26. /**
  27. * Convert dom node tree to array
  28. *
  29. * @param \DOMDocument $source
  30. * @return array
  31. * @throws \InvalidArgumentException
  32. */
  33. public function convert($source)
  34. {
  35. $xpath = new \DOMXPath($source);
  36. $output = [];
  37. foreach ($xpath->evaluate('/view') as $typeNode) {
  38. foreach ($typeNode->childNodes as $childNode) {
  39. if ($childNode->nodeType != XML_ELEMENT_NODE) {
  40. continue;
  41. }
  42. $result = $this->parseNodes($childNode);
  43. $output = array_merge_recursive($output, $result);
  44. }
  45. }
  46. return $output;
  47. }
  48. /**
  49. * Parse node values from xml nodes
  50. *
  51. * @param \DOMElement $childNode
  52. * @return array
  53. */
  54. protected function parseNodes($childNode)
  55. {
  56. $output = [];
  57. switch ($childNode->nodeName) {
  58. case 'vars':
  59. $moduleName = $childNode->getAttribute('module');
  60. $output[$childNode->tagName][$moduleName] = $this->parseVarElement($childNode);
  61. break;
  62. case 'exclude':
  63. /** @var $itemNode \DOMElement */
  64. foreach ($childNode->getElementsByTagName('item') as $itemNode) {
  65. $itemType = $itemNode->getAttribute('type');
  66. $output[$childNode->tagName][$itemType][] = $itemNode->nodeValue;
  67. }
  68. break;
  69. case 'media':
  70. foreach ($childNode->childNodes as $mediaNode) {
  71. if ($mediaNode instanceof \DOMElement) {
  72. $mediaNodesArray =
  73. $this->extractorPool->nodeProcessor($mediaNode->tagName)->process(
  74. $mediaNode,
  75. $childNode->tagName
  76. );
  77. $output = array_merge_recursive($output, $mediaNodesArray);
  78. }
  79. }
  80. break;
  81. }
  82. return $output;
  83. }
  84. /**
  85. * Recursive parser for <var> nodes
  86. *
  87. * @param \DOMElement $node
  88. * @return string|boolean|number|null|[]
  89. */
  90. protected function parseVarElement(\DOMElement $node)
  91. {
  92. $result = [];
  93. for ($varNode = $node->firstChild; $varNode !== null; $varNode = $varNode->nextSibling) {
  94. if ($varNode instanceof \DOMElement && $varNode->tagName == "var") {
  95. $varName = $varNode->getAttribute('name');
  96. $result[$varName] = $this->parseVarElement($varNode);
  97. }
  98. }
  99. if (!count($result)) {
  100. $result = (strtolower($node->nodeValue) !== 'true' && strtolower($node->nodeValue) !== 'false')
  101. ? $node->nodeValue
  102. : filter_var($node->nodeValue, FILTER_VALIDATE_BOOLEAN);
  103. }
  104. return $result;
  105. }
  106. }