Xml.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 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\Response;
  13. class Xml extends Response
  14. {
  15. // 输出参数
  16. protected $options = [
  17. // 根节点名
  18. 'root_node' => 'think',
  19. // 根节点属性
  20. 'root_attr' => '',
  21. //数字索引的子节点名
  22. 'item_node' => 'item',
  23. // 数字索引子节点key转换的属性名
  24. 'item_key' => 'id',
  25. // 数据编码
  26. 'encoding' => 'utf-8',
  27. ];
  28. protected $contentType = 'text/xml';
  29. /**
  30. * 处理数据
  31. * @access protected
  32. * @param mixed $data 要处理的数据
  33. * @return mixed
  34. */
  35. protected function output($data)
  36. {
  37. // XML数据转换
  38. return $this->xmlEncode($data, $this->options['root_node'], $this->options['item_node'], $this->options['root_attr'], $this->options['item_key'], $this->options['encoding']);
  39. }
  40. /**
  41. * XML编码
  42. * @param mixed $data 数据
  43. * @param string $root 根节点名
  44. * @param string $item 数字索引的子节点名
  45. * @param string $attr 根节点属性
  46. * @param string $id 数字索引子节点key转换的属性名
  47. * @param string $encoding 数据编码
  48. * @return string
  49. */
  50. protected function xmlEncode($data, $root, $item, $attr, $id, $encoding)
  51. {
  52. if (is_array($attr)) {
  53. $array = [];
  54. foreach ($attr as $key => $value) {
  55. $array[] = "{$key}=\"{$value}\"";
  56. }
  57. $attr = implode(' ', $array);
  58. }
  59. $attr = trim($attr);
  60. $attr = empty($attr) ? '' : " {$attr}";
  61. $xml = "<?xml version=\"1.0\" encoding=\"{$encoding}\"?>";
  62. $xml .= "<{$root}{$attr}>";
  63. $xml .= $this->dataToXml($data, $item, $id);
  64. $xml .= "</{$root}>";
  65. return $xml;
  66. }
  67. /**
  68. * 数据XML编码
  69. * @param mixed $data 数据
  70. * @param string $item 数字索引时的节点名称
  71. * @param string $id 数字索引key转换为的属性名
  72. * @return string
  73. */
  74. protected function dataToXml($data, $item, $id)
  75. {
  76. $xml = $attr = '';
  77. foreach ($data as $key => $val) {
  78. if (is_numeric($key)) {
  79. $id && $attr = " {$id}=\"{$key}\"";
  80. $key = $item;
  81. }
  82. $xml .= "<{$key}{$attr}>";
  83. $xml .= (is_array($val) || is_object($val)) ? $this->dataToXml($val, $item, $id) : $val;
  84. $xml .= "</{$key}>";
  85. }
  86. return $xml;
  87. }
  88. }