Xml.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\response;
  12. use think\Collection;
  13. use think\Model;
  14. use think\Response;
  15. class Xml extends Response
  16. {
  17. // 输出参数
  18. protected $options = [
  19. // 根节点名
  20. 'root_node' => 'think',
  21. // 根节点属性
  22. 'root_attr' => '',
  23. //数字索引的子节点名
  24. 'item_node' => 'item',
  25. // 数字索引子节点key转换的属性名
  26. 'item_key' => 'id',
  27. // 数据编码
  28. 'encoding' => 'utf-8',
  29. ];
  30. protected $contentType = 'text/xml';
  31. /**
  32. * 处理数据
  33. * @access protected
  34. * @param mixed $data 要处理的数据
  35. * @return mixed
  36. */
  37. protected function output($data)
  38. {
  39. if (is_string($data)) {
  40. if (0 !== strpos($data, '<?xml')) {
  41. $encoding = $this->options['encoding'];
  42. $xml = "<?xml version=\"1.0\" encoding=\"{$encoding}\"?>";
  43. $data = $xml . $data;
  44. }
  45. return $data;
  46. }
  47. // XML数据转换
  48. return $this->xmlEncode($data, $this->options['root_node'], $this->options['item_node'], $this->options['root_attr'], $this->options['item_key'], $this->options['encoding']);
  49. }
  50. /**
  51. * XML编码
  52. * @access protected
  53. * @param mixed $data 数据
  54. * @param string $root 根节点名
  55. * @param string $item 数字索引的子节点名
  56. * @param string $attr 根节点属性
  57. * @param string $id 数字索引子节点key转换的属性名
  58. * @param string $encoding 数据编码
  59. * @return string
  60. */
  61. protected function xmlEncode($data, $root, $item, $attr, $id, $encoding)
  62. {
  63. if (is_array($attr)) {
  64. $array = [];
  65. foreach ($attr as $key => $value) {
  66. $array[] = "{$key}=\"{$value}\"";
  67. }
  68. $attr = implode(' ', $array);
  69. }
  70. $attr = trim($attr);
  71. $attr = empty($attr) ? '' : " {$attr}";
  72. $xml = "<?xml version=\"1.0\" encoding=\"{$encoding}\"?>";
  73. $xml .= "<{$root}{$attr}>";
  74. $xml .= $this->dataToXml($data, $item, $id);
  75. $xml .= "</{$root}>";
  76. return $xml;
  77. }
  78. /**
  79. * 数据XML编码
  80. * @access protected
  81. * @param mixed $data 数据
  82. * @param string $item 数字索引时的节点名称
  83. * @param string $id 数字索引key转换为的属性名
  84. * @return string
  85. */
  86. protected function dataToXml($data, $item, $id)
  87. {
  88. $xml = $attr = '';
  89. if ($data instanceof Collection || $data instanceof Model) {
  90. $data = $data->toArray();
  91. }
  92. foreach ($data as $key => $val) {
  93. if (is_numeric($key)) {
  94. $id && $attr = " {$id}=\"{$key}\"";
  95. $key = $item;
  96. }
  97. $xml .= "<{$key}{$attr}>";
  98. $xml .= (is_array($val) || is_object($val)) ? $this->dataToXml($val, $item, $id) : $val;
  99. $xml .= "</{$key}>";
  100. }
  101. return $xml;
  102. }
  103. }