XML.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace crmeb\services\upload;
  3. class XML
  4. {
  5. /**
  6. * XML to array.
  7. *
  8. * @param string $xml XML string
  9. *
  10. * @return array
  11. */
  12. public static function parse($xml)
  13. {
  14. $backup = PHP_MAJOR_VERSION < 8 ? libxml_disable_entity_loader(true) : null;
  15. $result = self::normalize(simplexml_load_string(self::sanitize($xml), 'SimpleXMLElement', LIBXML_COMPACT | LIBXML_NOCDATA | LIBXML_NOBLANKS));
  16. PHP_MAJOR_VERSION < 8 && libxml_disable_entity_loader($backup);
  17. return $result;
  18. }
  19. /**
  20. * XML encode.
  21. *
  22. * @param mixed $data
  23. * @param string $root
  24. * @param string $item
  25. * @param string $attr
  26. * @param string $id
  27. *
  28. * @return string
  29. */
  30. public static function build(
  31. $data,
  32. $root = 'xml',
  33. $item = 'item',
  34. $attr = '',
  35. $id = 'id'
  36. )
  37. {
  38. if (is_array($attr)) {
  39. $_attr = [];
  40. foreach ($attr as $key => $value) {
  41. $_attr[] = "{$key}=\"{$value}\"";
  42. }
  43. $attr = implode(' ', $_attr);
  44. }
  45. $attr = trim($attr);
  46. $attr = empty($attr) ? '' : " {$attr}";
  47. $xml = "<{$root}{$attr}>";
  48. $xml .= self::data2Xml($data, $item, $id);
  49. $xml .= "</{$root}>";
  50. return $xml;
  51. }
  52. /**
  53. * Build CDATA.
  54. *
  55. * @param string $string
  56. *
  57. * @return string
  58. */
  59. public static function cdata($string)
  60. {
  61. return sprintf('<![CDATA[%s]]>', $string);
  62. }
  63. /**
  64. * Object to array.
  65. *
  66. *
  67. * @param SimpleXMLElement $obj
  68. *
  69. * @return array
  70. */
  71. protected static function normalize($obj)
  72. {
  73. $result = null;
  74. if (is_object($obj)) {
  75. $obj = (array)$obj;
  76. }
  77. if (is_array($obj)) {
  78. foreach ($obj as $key => $value) {
  79. $res = self::normalize($value);
  80. if (('@attributes' === $key) && ($key)) {
  81. $result = $res; // @codeCoverageIgnore
  82. } else {
  83. $result[$key] = $res;
  84. }
  85. }
  86. } else {
  87. $result = $obj;
  88. }
  89. return $result;
  90. }
  91. /**
  92. * Array to XML.
  93. *
  94. * @param array $data
  95. * @param string $item
  96. * @param string $id
  97. *
  98. * @return string
  99. */
  100. protected static function data2Xml($data, $item = 'item', $id = 'id')
  101. {
  102. $xml = $attr = '';
  103. foreach ($data as $key => $val) {
  104. if (is_numeric($key)) {
  105. $id && $attr = " {$id}=\"{$key}\"";
  106. $key = $item;
  107. }
  108. $xml .= "<{$key}{$attr}>";
  109. if ((is_array($val) || is_object($val))) {
  110. $xml .= self::data2Xml((array)$val, $item, $id);
  111. } else {
  112. $xml .= is_numeric($val) ? $val : self::cdata($val);
  113. }
  114. $xml .= "</{$key}>";
  115. }
  116. return $xml;
  117. }
  118. /**
  119. * Delete invalid characters in XML.
  120. *
  121. * @see https://www.w3.org/TR/2008/REC-xml-20081126/#charsets - XML charset range
  122. * @see http://php.net/manual/en/regexp.reference.escape.php - escape in UTF-8 mode
  123. *
  124. * @param string $xml
  125. *
  126. * @return string
  127. */
  128. public static function sanitize($xml)
  129. {
  130. return preg_replace('/[^\x{9}\x{A}\x{D}\x{20}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]+/u', '', $xml);
  131. }
  132. }