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

Json::__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\Cookie;
16
use think\Response;
17
18
/**
19
 * Json Response
20
 */
21
class Json extends Response
22
{
23
    // 输出参数
24
    protected $options = [
25
        'json_encode_param' => JSON_UNESCAPED_UNICODE,
26
    ];
27
28
    protected $contentType = 'application/json';
29
30
    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...
31
    {
32
        parent::__construct($cookie, $data, $code);
33
    }
34
35
    /**
36
     * 处理数据
37
     * @access protected
38
     * @param  mixed $data 要处理的数据
39
     * @return string
40
     * @throws \Exception
41
     */
42
    protected function output($data): string
43
    {
44
        try {
45
            // 返回JSON数据格式到客户端 包含状态信息
46
            $data = json_encode($data, $this->options['json_encode_param']);
47
48
            if (false === $data) {
49
                throw new \InvalidArgumentException(json_last_error_msg());
50
            }
51
52
            return $data;
53
        } catch (\Exception $e) {
54
            if ($e->getPrevious()) {
55
                throw $e->getPrevious();
56
            }
57
            throw $e;
58
        }
59
    }
60
61
}
62