Xml.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // XML数据转换
  40. return $this->xmlEncode($data, $this->options['root_node'], $this->options['item_node'], $this->options['root_attr'], $this->options['item_key'], $this->options['encoding']);
  41. }
  42. /**
  43. * XML编码
  44. * @param mixed $data 数据
  45. * @param string $root 根节点名
  46. * @param string $item 数字索引的子节点名
  47. * @param string $attr 根节点属性
  48. * @param string $id 数字索引子节点key转换的属性名
  49. * @param string $encoding 数据编码
  50. * @return string
  51. */
  52. protected function xmlEncode($data, $root, $item, $attr, $id, $encoding)
  53. {
  54. if (is_array($attr)) {
  55. $array = [];
  56. foreach ($attr as $key => $value) {
  57. $array[] = "{$key}=\"{$value}\"";
  58. }
  59. $attr = implode(' ', $array);
  60. }
  61. $attr = trim($attr);
  62. $attr = empty($attr) ? '' : " {$attr}";
  63. $xml = "<?xml version=\"1.0\" encoding=\"{$encoding}\"?>";
  64. $xml .= "<{$root}{$attr}>";
  65. $xml .= $this->dataToXml($data, $item, $id);
  66. $xml .= "</{$root}>";
  67. return $xml;
  68. }
  69. /**
  70. * 数据XML编码
  71. * @param mixed $data 数据
  72. * @param string $item 数字索引时的节点名称
  73. * @param string $id 数字索引key转换为的属性名
  74. * @return string
  75. */
  76. protected function dataToXml($data, $item, $id)
  77. {
  78. $xml = $attr = '';
  79. if ($data instanceof Collection || $data instanceof Model) {
  80. $data = $data->toArray();
  81. }
  82. foreach ($data as $key => $val) {
  83. if (is_numeric($key)) {
  84. $id && $attr = " {$id}=\"{$key}\"";
  85. $key = $item;
  86. }
  87. $xml .= "<{$key}{$attr}>";
  88. $xml .= (is_array($val) || is_object($val)) ? $this->dataToXml($val, $item, $id) : $val;
  89. $xml .= "</{$key}>";
  90. }
  91. return $xml;
  92. }
  93. }