|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bugloos\ResponderBundle\ResponseHelper; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @author Mojtaba Gheytasi <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
trait ApiResponseHelper |
|
12
|
|
|
{ |
|
13
|
|
|
private int $statusCode; |
|
14
|
|
|
|
|
15
|
|
|
protected function respondDeleted( |
|
16
|
|
|
string $message, |
|
17
|
|
|
array $headers = [] |
|
18
|
|
|
): JsonResponse { |
|
19
|
|
|
return $this->setStatusCode(Response::HTTP_OK) |
|
20
|
|
|
->respondWithMessage($message, $headers); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
protected function respondNoContent(array $headers = []): JsonResponse |
|
24
|
|
|
{ |
|
25
|
|
|
return $this->setStatusCode(Response::HTTP_NO_CONTENT) |
|
26
|
|
|
->respond([], $headers, []); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
protected function respondForbiddenError( |
|
30
|
|
|
string $message, |
|
31
|
|
|
array $headers = [] |
|
32
|
|
|
): JsonResponse { |
|
33
|
|
|
return $this->setStatusCode(Response::HTTP_FORBIDDEN) |
|
34
|
|
|
->respondWithMessage($message, $headers); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function respondNotFoundError( |
|
38
|
|
|
string $message, |
|
39
|
|
|
array $headers = [] |
|
40
|
|
|
): JsonResponse { |
|
41
|
|
|
return $this->setStatusCode(Response::HTTP_NOT_FOUND) |
|
42
|
|
|
->respondWithMessage($message, $headers); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function respondUnauthorizedError( |
|
46
|
|
|
string $message, |
|
47
|
|
|
array $headers = [] |
|
48
|
|
|
): JsonResponse { |
|
49
|
|
|
return $this->setStatusCode(Response::HTTP_UNAUTHORIZED) |
|
50
|
|
|
->respondWithMessage($message, $headers); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function respondWithSuccessMessage( |
|
54
|
|
|
string $message, |
|
55
|
|
|
array $headers = [] |
|
56
|
|
|
): JsonResponse { |
|
57
|
|
|
return $this->setStatusCode(Response::HTTP_OK) |
|
58
|
|
|
->respondWithMessage($message, $headers); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** new functions - start */ |
|
62
|
|
|
|
|
63
|
|
|
protected function respondWithSucess( |
|
64
|
|
|
array|object $data, |
|
65
|
|
|
array $groups= [], |
|
66
|
|
|
array $headers = [] |
|
67
|
|
|
): JsonResponse { |
|
68
|
|
|
return $this->setStatusCode(Response::HTTP_OK) |
|
69
|
|
|
->respond($data, $groups, $headers); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function respondCreated( |
|
73
|
|
|
array|object $data, |
|
74
|
|
|
array $groups= [], |
|
75
|
|
|
array $headers = [] |
|
76
|
|
|
): JsonResponse { |
|
77
|
|
|
return $this->setStatusCode(Response::HTTP_CREATED) |
|
78
|
|
|
->respond($data, $groups, $headers); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected function respondCreatedMessage( |
|
82
|
|
|
string $message, |
|
83
|
|
|
array $headers = [] |
|
84
|
|
|
): JsonResponse { |
|
85
|
|
|
return $this->setStatusCode(Response::HTTP_CREATED) |
|
86
|
|
|
->respondWithMessage($message, $headers); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** Legacy functionas - start */ |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @deprecated since Responder Bundle v1.0.0, use respondWithSucess() instead |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function respondWithItem( |
|
95
|
|
|
$item, |
|
96
|
|
|
array $groups = [], |
|
97
|
|
|
array $headers = [] |
|
98
|
|
|
): JsonResponse { |
|
99
|
|
|
|
|
100
|
|
|
trigger_deprecation('bugloos/responder-bundle', '1.0.0', 'The "%s()" method is deprecated, use "respondWithSucess()" instead.', __METHOD__); |
|
101
|
|
|
|
|
102
|
|
|
return $this->setStatusCode(Response::HTTP_OK) |
|
103
|
|
|
->respond($item, $groups, $headers); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @deprecated since Responder Bundle v1.0.0, use respondWithSucess() instead |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function respondWithCollection( |
|
110
|
|
|
$collection, |
|
111
|
|
|
array $groups = [], |
|
112
|
|
|
array $headers = [] |
|
113
|
|
|
): JsonResponse { |
|
114
|
|
|
|
|
115
|
|
|
trigger_deprecation('bugloos/responder-bundle', '1.0.0', 'The "%s()" method is deprecated, use "respondWithSucess()" instead.', __METHOD__); |
|
116
|
|
|
|
|
117
|
|
|
return $this->setStatusCode(Response::HTTP_OK) |
|
118
|
|
|
->respond($collection, $groups, $headers); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @deprecated since Responder Bundle v1.0.0, use respondWithSucess() instead |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function respondWithPagination( |
|
125
|
|
|
$pagination, |
|
126
|
|
|
array $groups = [], |
|
127
|
|
|
array $headers = [] |
|
128
|
|
|
): JsonResponse { |
|
129
|
|
|
|
|
130
|
|
|
trigger_deprecation('bugloos/responder-bundle', '1.0.0', 'The "%s()" method is deprecated, use "respondWithSucess()" instead.', __METHOD__); |
|
131
|
|
|
|
|
132
|
|
|
return $this->setStatusCode(Response::HTTP_OK) |
|
133
|
|
|
->respond($pagination, $groups, $headers); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @deprecated since Responder Bundle v1.0.0, use respondWithSucessMessage() instead |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function respondItemUpdated( |
|
140
|
|
|
$message, |
|
141
|
|
|
array $headers = [] |
|
142
|
|
|
): JsonResponse { |
|
143
|
|
|
|
|
144
|
|
|
trigger_deprecation('bugloos/responder-bundle', '1.0.0', 'The "%s()" method is deprecated, use "respondWithSucessMessage()" instead.', __METHOD__); |
|
145
|
|
|
|
|
146
|
|
|
return $this->setStatusCode(Response::HTTP_OK) |
|
147
|
|
|
->respondWithMessage($message, $headers); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @deprecated since Responder Bundle v1.0.0, use respondCreated() OR respondCreatedMessage() instead |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function respondItemCreated( |
|
155
|
|
|
string $message, |
|
156
|
|
|
array $headers = [] |
|
157
|
|
|
): JsonResponse { |
|
158
|
|
|
|
|
159
|
|
|
trigger_deprecation('bugloos/responder-bundle', '1.0.0', 'The "%s()" method is deprecated, use "respondCreated()" OR "respondCreatedMessage()" instead.', __METHOD__); |
|
160
|
|
|
|
|
161
|
|
|
return $this->setStatusCode(Response::HTTP_CREATED) |
|
162
|
|
|
->respondWithMessage($message, $headers); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @deprecated since Responder Bundle v1.0.0, use respondDeleted() instead |
|
167
|
|
|
*/ |
|
168
|
|
|
protected function respondItemDeleted( |
|
169
|
|
|
string $message, |
|
170
|
|
|
array $headers = [] |
|
171
|
|
|
): JsonResponse { |
|
172
|
|
|
|
|
173
|
|
|
trigger_deprecation('bugloos/responder-bundle', '1.0.0', 'The "%s()" method is deprecated, use "respondDeleted()" instead.', __METHOD__); |
|
174
|
|
|
|
|
175
|
|
|
return $this->setStatusCode(Response::HTTP_OK) |
|
176
|
|
|
->respondWithMessage($message, $headers); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** Legacy functionas - end */ |
|
180
|
|
|
|
|
181
|
|
|
private function setStatusCode(int $statusCode): self |
|
182
|
|
|
{ |
|
183
|
|
|
$this->statusCode = $statusCode; |
|
184
|
|
|
|
|
185
|
|
|
return $this; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
private function getStatusCode(): string |
|
189
|
|
|
{ |
|
190
|
|
|
return $this->statusCode; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
private function respond($data, array $groups, array $headers): JsonResponse |
|
194
|
|
|
{ |
|
195
|
|
|
return $this->json( |
|
|
|
|
|
|
196
|
|
|
$data, |
|
197
|
|
|
$this->getStatusCode(), |
|
198
|
|
|
$headers, |
|
199
|
|
|
['groups' => $groups] |
|
200
|
|
|
); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
private function respondWithMessage(string $message, array $headers = []): JsonResponse |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->respond([ |
|
206
|
|
|
'message' => $message, |
|
207
|
|
|
], $headers, []); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
} |
|
211
|
|
|
|