-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrester_response.class.php
162 lines (144 loc) · 4.84 KB
/
rester_response.class.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
* Class rester_response
*/
class rester_response
{
/**
* @var int 응답코드
*/
protected static $response_code = 200;
const code_success = '00';
const code_require_login = '01';
const code_system_error = '02';
const code_parameter = '11';
const code_config = '12';
const code_request_method = '13';
const code_uri = '14';
const code_access_ip = '15';
const code_not_found = '16';
const code_access_level = '17';
const code_param_filter = '18';
const code_param_data = '19';
const code_cache_server = '20';
const code_response_fail = '21';
const code_login_fail = '22';
const code_etc = '99';
/**
* @var array 응답코드 목록
*/
protected static $res_code_list = [
self::code_success =>'성공',
self::code_require_login =>'로그인 필요',
self::code_system_error =>'시스템 오류',
self::code_parameter =>'호출인자 오류',
self::code_config =>'환경설정 오류',
self::code_request_method =>'호출 메서드 오류',
self::code_uri =>'호출 URI 오류',
self::code_access_ip =>'접근이 허용되지 않습니다.',
self::code_not_found =>'모듈이나 프로시저를 찾을수 없습니다.',
self::code_access_level =>'접근권한 오류',
self::code_param_filter =>'파라미터 필터링 오류',
self::code_param_data =>'파라미터 검증 오류',
self::code_cache_server =>'캐쉬서버 접속오류',
self::code_response_fail =>'외부호출 결과 실패',
self::code_login_fail =>'로그인 실패',
self::code_etc =>'기타 오류'
];
protected static $success = true;
protected static $res_code = '00';
protected static $session = false;
protected static $msg = false;
protected static $warning = false;
protected static $error = false;
protected static $error_trace = false;
protected static $data = false;
protected static $custom = false;
/**
* @param string $code
* @param string $msg
*/
public static function failed($code, $msg='')
{
self::$res_code = $code;
self::msg(self::$res_code_list[$code]);
if($msg) self::error($msg);
}
/**
* render result
*/
public static function run()
{
http_response_code(self::$response_code);
header("Content-type: application/json; charset=UTF-8");
$body = [];
$body['success'] = self::$success;
$body['retCode'] = self::$res_code;
if(self::$session) $body['session'] = self::$session;
if(self::$msg) $body['msg'] = self::$msg;
$body['data'] = self::$data;
if(cfg::debug_mode())
{
if(self::$warning) $body['warning'] = self::$warning;
if(self::$error) $body['error'] = self::$error;
if(self::$error_trace) $body['errorTrace'] = self::$error_trace;
}
if(self::$custom && is_array(self::$custom)) $body = array_merge($body, self::$custom);
echo json_encode($body);
}
/**
* reset data
*/
public static function reset()
{
self::$success = true;
self::$res_code = '00';
self::$session = false;
self::$msg = false;
self::$warning = false;
self::$error = false;
self::$error_trace = false;
self::$data = false;
}
/**
* @param array $data
*/
public static function body($data) { self::$data = $data; }
/**
* @param array $data
*/
public static function session($data) { self::$session = $data; }
/**
* Add message
*
* @param string $msg
*/
public static function msg($msg) { if(!self::$msg) self::$msg=[]; self::$msg[] = $msg; }
/**
* Add warning message
*
* @param string $msg
*/
public static function warning($msg) { if(!self::$warning) self::$warning=[]; self::$warning[] = $msg; }
/**
* Add Custom data
*
* @param string $msg
*/
public static function custom($key, $value) { if(!self::$custom) self::$custom=[]; self::$custom[$key] = $value; }
/**
* Add error
*
* @param string $msg
*/
public static function error($msg) { if(!self::$error) self::$error=[]; self::$error[] = $msg; self::failure(); }
/**
* Set error trace
* @param array $data
*/
public static function error_trace($data) { if(!self::$error_trace) self::$error_trace=[]; self::$error_trace = $data; self::failure(); }
/**
* set failure
*/
public static function failure() { self::$success = false; }
}