-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathResponse.php
129 lines (118 loc) · 2.64 KB
/
Response.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/********************************************
* Easy PHP *
* *
* A lightweight PHP framework for studying *
* *
* TIERGB *
* <https://github.com/TIGERB> *
* *
********************************************/
namespace Framework;
use Framework\App;
/**
* 响应
*
* @author TIERGB <https://github.com/TIGERB>
*/
class Response
{
/**
* app instance
*
* @var Framework\App
*/
private $app = null;
/**
* 构造函数
*/
public function __construct(App $app)
{
$this->app = $app;
}
/**
* 响应
*
* @param mixed $response 响应内容
* @return json
*/
public function response($response)
{
header('Content-Type:Application/json; Charset=utf-8');
die(json_encode(
$response,
JSON_UNESCAPED_UNICODE)
);
}
/**
* REST风格 成功响应
*
* @param mixed $response 响应内容
* @return json
*/
public function restSuccess($response)
{
header('Content-Type:Application/json; Charset=utf-8');
die(json_encode([
'code' => 200,
'message' => 'OK',
'result' => $response
],JSON_UNESCAPED_UNICODE)
);
}
/**
* cli模式成功响应
*
* @param mixed $response 响应内容
* @return array
*/
public function cliModeSuccess($response)
{
var_dump([
'code' => 200,
'message' => 'OK',
'result' => $response
]);
}
/**
* REST风格 失败响应
*
* @param mixed $response 响应内容
* @return json
*/
public function restFail(
$code = 500,
$message = 'Internet Server Error',
$response)
{
header('Content-Type:Application/json; Charset=utf-8');
die(json_encode([
'code' => $code,
'message' => $message,
'result' => $response
],JSON_UNESCAPED_UNICODE));
}
/**
* 魔法函数__get.
*
* @param string $name 属性名称
*
* @return mixed
*/
public function __get($name = '')
{
return $this->$name;
}
/**
* 魔法函数__set.
*
* @param string $name 属性名称
* @param mixed $value 属性值
*
* @return mixed
*/
public function __set($name = '', $value = '')
{
$this->$name = $value;
}
}