Completed
Push — 6.0 ( 28c43f...8e1d5f )
by liu
09:21
created

Xml::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think\response;
14
15
use think\Collection;
16
use think\Cookie;
17
use think\Model;
18
use think\Response;
19
20
/**
21
 * XML Response
22
 */
23
class Xml extends Response
24
{
25
    // 输出参数
26
    protected $options = [
27
        // 根节点名
28
        'root_node' => 'think',
29
        // 根节点属性
30
        'root_attr' => '',
31
        //数字索引的子节点名
32
        'item_node' => 'item',
33
        // 数字索引子节点key转换的属性名
34
        'item_key'  => 'id',
35
        // 数据编码
36
        'encoding'  => 'utf-8',
37
    ];
38
39
    protected $contentType = 'text/xml';
40
41
    public function __construct(Cookie $cookie, $data = '', int $code = 200)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
42
    {
43
        parent::__construct($cookie, $data, $code);
44
    }
45
46
    /**
47
     * 处理数据
48
     * @access protected
49
     * @param  mixed $data 要处理的数据
50
     * @return mixed
51
     */
52
    protected function output($data): string
53
    {
54
        if (is_string($data)) {
55
            if (0 !== strpos($data, '<?xml')) {
56
                $encoding = $this->options['encoding'];
57
                $xml      = "<?xml version=\"1.0\" encoding=\"{$encoding}\"?>";
58
                $data     = $xml . $data;
59
            }
60
            return $data;
61
        }
62
63
        // XML数据转换
64
        return $this->xmlEncode($data, $this->options['root_node'], $this->options['item_node'], $this->options['root_attr'], $this->options['item_key'], $this->options['encoding']);
65
    }
66
67
    /**
68
     * XML编码
69
     * @access protected
70
     * @param  mixed $data 数据
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
71
     * @param  string $root 根节点名
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
72
     * @param  string $item 数字索引的子节点名
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
73
     * @param  mixed  $attr 根节点属性
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
74
     * @param  string $id   数字索引子节点key转换的属性名
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter name; 3 found
Loading history...
75
     * @param  string $encoding 数据编码
76
     * @return string
77
     */
78
    protected function xmlEncode($data, string $root, string $item, $attr, string $id, string $encoding): string
79
    {
80
        if (is_array($attr)) {
81
            $array = [];
82
            foreach ($attr as $key => $value) {
83
                $array[] = "{$key}=\"{$value}\"";
84
            }
85
            $attr = implode(' ', $array);
86
        }
87
88
        $attr = trim($attr);
89
        $attr = empty($attr) ? '' : " {$attr}";
90
        $xml  = "<?xml version=\"1.0\" encoding=\"{$encoding}\"?>";
91
        $xml .= "<{$root}{$attr}>";
92
        $xml .= $this->dataToXml($data, $item, $id);
93
        $xml .= "</{$root}>";
94
95
        return $xml;
96
    }
97
98
    /**
99
     * 数据XML编码
100
     * @access protected
101
     * @param  mixed  $data 数据
102
     * @param  string $item 数字索引时的节点名称
103
     * @param  string $id   数字索引key转换为的属性名
104
     * @return string
105
     */
106
    protected function dataToXml($data, string $item, string $id): string
107
    {
108
        $xml = $attr = '';
109
110
        if ($data instanceof Collection || $data instanceof Model) {
111
            $data = $data->toArray();
112
        }
113
114
        foreach ($data as $key => $val) {
115
            if (is_numeric($key)) {
116
                $id && $attr = " {$id}=\"{$key}\"";
117
                $key         = $item;
118
            }
119
            $xml .= "<{$key}{$attr}>";
120
            $xml .= (is_array($val) || is_object($val)) ? $this->dataToXml($val, $item, $id) : $val;
121
            $xml .= "</{$key}>";
122
        }
123
124
        return $xml;
125
    }
126
}
127