forked from dees040/laravel-api-responses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
responses.php
195 lines (181 loc) · 4.5 KB
/
responses.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
if (! function_exists('json_response')) {
/**
* Return a new JSON response.
*
* @param string|array $data
* @param int $status
* @param array $headers
* @param int $options
* @return \Illuminate\Http\JsonResponse
*/
function json_response($data = null, $status = 200, array $headers = [], $options = 0)
{
return new \Illuminate\Http\JsonResponse($data, $status, $headers, $options);
}
}
if (! function_exists('error_json_response')) {
/**
* Return a new error (4xx) response.
*
* @param string $message
* @param array $errors
* @param int $code
* @return \Illuminate\Http\JsonResponse
*/
function error_json_response($message = '', $errors = [], $code = 400)
{
return json_response(compact('message', 'errors'), $code);
}
}
if (! function_exists('ok')) {
/**
* Return an all OK 200 response.
*
* @param mixed $data
* @return \Illuminate\Http\JsonResponse
*/
function ok($data = null)
{
return json_response($data, 200);
}
}
if (! function_exists('created')) {
/**
* Return created response.
*
* @param mixed $data
* @return \Illuminate\Http\JsonResponse
*/
function created($data = null)
{
return json_response($data, 201);
}
}
if (! function_exists('accepted')) {
/**
* Return accepted response.
*
* @param mixed $data
* @return \Illuminate\Http\JsonResponse
*/
function accepted($data = null)
{
return json_response($data, 202);
}
}
if (! function_exists('no_content')) {
/**
* Return no content response.
*
* @return \Illuminate\Http\JsonResponse
*/
function no_content()
{
return json_response(null, 204);
}
}
if (! function_exists('bad_request')) {
/**
* Return a bad request response.
*
* @param string $message
* @param array $errors
* @return \Illuminate\Http\JsonResponse
*/
function bad_request($message = '', $errors = [])
{
return error_json_response($message, $errors, 400);
}
}
if (! function_exists('unauthenticated')) {
/**
* Return a unauthenticated response.
*
* @param string $message
* @param array $errors
* @return \Illuminate\Http\JsonResponse
*/
function unauthenticated($message = '', $errors = [])
{
return error_json_response($message, $errors, 401);
}
}
if (! function_exists('forbidden')) {
/**
* Return a forbidden response.
*
* @param string $message
* @param array $errors
* @return \Illuminate\Http\JsonResponse
*/
function forbidden($message = '', $errors = [])
{
return error_json_response($message, $errors, 403);
}
}
if (! function_exists('not_found')) {
/**
* Return a not found response.
*
* @param string $message
* @param array $errors
* @return \Illuminate\Http\JsonResponse
*/
function not_found($message = '', $errors = [])
{
return error_json_response($message, $errors, 404);
}
}
if (! function_exists('method_not_allowed')) {
/**
* Return a method not allowed response.
*
* @param string $message
* @param array $errors
* @return \Illuminate\Http\JsonResponse
*/
function method_not_allowed($message = '', $errors = [])
{
return error_json_response($message, $errors, 405);
}
}
if (! function_exists('not_acceptable')) {
/**
* Return a not acceptable response.
*
* @param string $message
* @param array $errors
* @return \Illuminate\Http\JsonResponse
*/
function not_acceptable($message = '', $errors = [])
{
return error_json_response($message, $errors, 406);
}
}
if (! function_exists('teapot')) {
/**
* All hail the magical teapot response.
*
* @param string $message
* @param array $errors
* @return \Illuminate\Http\JsonResponse
*/
function teapot($message = '', $errors = [])
{
return error_json_response($message, $errors, 418);
}
}
if (! function_exists('unprocessable_entity')) {
/**
* Return a unprocessable entity response.
*
* @param string $message
* @param array $errors
* @return \Illuminate\Http\JsonResponse
*/
function unprocessable_entity($message = '', $errors = [])
{
return error_json_response($message, $errors, 422);
}
}