Xml.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\ReportXml\Config\Converter;
  7. use Magento\Framework\Config\ConverterInterface;
  8. /**
  9. * A converter of reports configuration.
  10. *
  11. * Converts configuration data stored in XML format into corresponding PHP array.
  12. */
  13. class Xml implements ConverterInterface
  14. {
  15. /**
  16. * Converts XML node into corresponding array.
  17. *
  18. * @param \DOMNode $source
  19. * @return array|string
  20. */
  21. private function convertNode(\DOMNode $source)
  22. {
  23. $result = [];
  24. if ($source->hasAttributes()) {
  25. $attrs = $source->attributes;
  26. foreach ($attrs as $attr) {
  27. $result[$attr->name] = $attr->value;
  28. }
  29. }
  30. if ($source->hasChildNodes()) {
  31. $children = $source->childNodes;
  32. if ($children->length == 1) {
  33. $child = $children->item(0);
  34. if ($child->nodeType == XML_TEXT_NODE) {
  35. $result['_value'] = $child->nodeValue;
  36. return count($result) == 1 ? $result['_value'] : $result;
  37. }
  38. }
  39. foreach ($children as $child) {
  40. if ($child instanceof \DOMCharacterData) {
  41. continue;
  42. }
  43. $result[$child->nodeName][] = $this->convertNode($child);
  44. }
  45. }
  46. return $result;
  47. }
  48. /**
  49. * Converts XML document into corresponding array.
  50. *
  51. * @param \DOMDocument $source
  52. * @return array
  53. */
  54. public function convert($source)
  55. {
  56. return $this->convertNode($source);
  57. }
  58. }