Dom.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Config\Converter;
  7. class Dom implements \Magento\Framework\Config\ConverterInterface
  8. {
  9. const ATTRIBUTES = '__attributes__';
  10. const CONTENT = '__content__';
  11. /**
  12. * Convert dom node tree to array
  13. *
  14. * @param mixed $source
  15. * @return array
  16. */
  17. public function convert($source)
  18. {
  19. $nodeListData = [];
  20. /** @var $node \DOMNode */
  21. foreach ($source->childNodes as $node) {
  22. if ($node->nodeType == XML_ELEMENT_NODE) {
  23. $nodeData = [];
  24. /** @var $attribute \DOMNode */
  25. foreach ($node->attributes as $attribute) {
  26. if ($attribute->nodeType == XML_ATTRIBUTE_NODE) {
  27. $nodeData[self::ATTRIBUTES][$attribute->nodeName] = $attribute->nodeValue;
  28. }
  29. }
  30. $childrenData = $this->convert($node);
  31. if (is_array($childrenData)) {
  32. $nodeData = array_merge($nodeData, $childrenData);
  33. } else {
  34. $nodeData[self::CONTENT] = $childrenData;
  35. }
  36. $nodeListData[$node->nodeName][] = $nodeData;
  37. } elseif ($node->nodeType == XML_CDATA_SECTION_NODE || $node->nodeType == XML_TEXT_NODE && trim(
  38. $node->nodeValue
  39. ) != ''
  40. ) {
  41. return $node->nodeValue;
  42. }
  43. }
  44. return $nodeListData;
  45. }
  46. }