bugloos /
responder-bundle
| 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 | protected function respondWithSuccess( |
||
| 62 | iterable|object $data, |
||
| 63 | array $groups= [], |
||
| 64 | array $headers = [] |
||
| 65 | ): JsonResponse { |
||
| 66 | return $this->setStatusCode(Response::HTTP_OK) |
||
| 67 | ->respond($data, $groups, $headers); |
||
| 68 | } |
||
| 69 | |||
| 70 | protected function respondCreated( |
||
| 71 | array|object $data, |
||
| 72 | array $groups= [], |
||
| 73 | array $headers = [] |
||
| 74 | ): JsonResponse { |
||
| 75 | return $this->setStatusCode(Response::HTTP_CREATED) |
||
| 76 | ->respond($data, $groups, $headers); |
||
| 77 | } |
||
| 78 | |||
| 79 | protected function respondCreatedMessage( |
||
| 80 | string $message, |
||
| 81 | array $headers = [] |
||
| 82 | ): JsonResponse { |
||
| 83 | return $this->setStatusCode(Response::HTTP_CREATED) |
||
| 84 | ->respondWithMessage($message, $headers); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @deprecated since Responder Bundle v1.0.0, use respondWithSuccess() instead |
||
| 89 | */ |
||
| 90 | protected function respondWithItem( |
||
| 91 | $item, |
||
| 92 | array $groups = [], |
||
| 93 | array $headers = [] |
||
| 94 | ): JsonResponse { |
||
| 95 | |||
| 96 | trigger_deprecation('bugloos/responder-bundle', '1.0.0', 'The "%s()" method is deprecated, use "respondWithSuccess()" instead.', __METHOD__); |
||
| 97 | |||
| 98 | return $this->setStatusCode(Response::HTTP_OK) |
||
| 99 | ->respond($item, $groups, $headers); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @deprecated since Responder Bundle v1.0.0, use respondWithSuccess() instead |
||
| 104 | */ |
||
| 105 | protected function respondWithCollection( |
||
| 106 | $collection, |
||
| 107 | array $groups = [], |
||
| 108 | array $headers = [] |
||
| 109 | ): JsonResponse { |
||
| 110 | |||
| 111 | trigger_deprecation('bugloos/responder-bundle', '1.0.0', 'The "%s()" method is deprecated, use "respondWithSuccess()" instead.', __METHOD__); |
||
| 112 | |||
| 113 | return $this->setStatusCode(Response::HTTP_OK) |
||
| 114 | ->respond($collection, $groups, $headers); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @deprecated since Responder Bundle v1.0.0, use respondWithSuccess() instead |
||
| 119 | */ |
||
| 120 | protected function respondWithPagination( |
||
| 121 | $pagination, |
||
| 122 | array $groups = [], |
||
| 123 | array $headers = [] |
||
| 124 | ): JsonResponse { |
||
| 125 | |||
| 126 | trigger_deprecation('bugloos/responder-bundle', '1.0.0', 'The "%s()" method is deprecated, use "respondWithSuccess()" instead.', __METHOD__); |
||
| 127 | |||
| 128 | return $this->setStatusCode(Response::HTTP_OK) |
||
| 129 | ->respond($pagination, $groups, $headers); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @deprecated since Responder Bundle v1.0.0, use respondWithSuccessMessage() instead |
||
| 134 | */ |
||
| 135 | protected function respondItemUpdated( |
||
| 136 | $message, |
||
| 137 | array $headers = [] |
||
| 138 | ): JsonResponse { |
||
| 139 | |||
| 140 | trigger_deprecation('bugloos/responder-bundle', '1.0.0', 'The "%s()" method is deprecated, use "respondWithSuccessMessage()" instead.', __METHOD__); |
||
| 141 | |||
| 142 | return $this->setStatusCode(Response::HTTP_OK) |
||
| 143 | ->respondWithMessage($message, $headers); |
||
| 144 | } |
||
| 145 | |||
| 146 | |||
| 147 | /** |
||
| 148 | * @deprecated since Responder Bundle v1.0.0, use respondCreated() OR respondCreatedMessage() instead |
||
| 149 | */ |
||
| 150 | protected function respondItemCreated( |
||
| 151 | string $message, |
||
| 152 | array $headers = [] |
||
| 153 | ): JsonResponse { |
||
| 154 | |||
| 155 | trigger_deprecation('bugloos/responder-bundle', '1.0.0', 'The "%s()" method is deprecated, use "respondCreated()" OR "respondCreatedMessage()" instead.', __METHOD__); |
||
| 156 | |||
| 157 | return $this->setStatusCode(Response::HTTP_CREATED) |
||
| 158 | ->respondWithMessage($message, $headers); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @deprecated since Responder Bundle v1.0.0, use respondDeleted() instead |
||
| 163 | */ |
||
| 164 | protected function respondItemDeleted( |
||
| 165 | string $message, |
||
| 166 | array $headers = [] |
||
| 167 | ): JsonResponse { |
||
| 168 | |||
| 169 | trigger_deprecation('bugloos/responder-bundle', '1.0.0', 'The "%s()" method is deprecated, use "respondDeleted()" instead.', __METHOD__); |
||
| 170 | |||
| 171 | return $this->setStatusCode(Response::HTTP_OK) |
||
| 172 | ->respondWithMessage($message, $headers); |
||
| 173 | } |
||
| 174 | |||
| 175 | private function setStatusCode(int $statusCode): self |
||
| 176 | { |
||
| 177 | $this->statusCode = $statusCode; |
||
| 178 | |||
| 179 | return $this; |
||
| 180 | } |
||
| 181 | |||
| 182 | private function getStatusCode(): string |
||
| 183 | { |
||
| 184 | return $this->statusCode; |
||
| 185 | } |
||
| 186 | |||
| 187 | private function respond($data, array $groups, array $headers): JsonResponse |
||
| 188 | { |
||
| 189 | return $this->json( |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 190 | $data, |
||
| 191 | $this->getStatusCode(), |
||
| 192 | $headers, |
||
| 193 | ['groups' => $groups] |
||
| 194 | ); |
||
| 195 | } |
||
| 196 | |||
| 197 | private function respondWithMessage(string $message, array $headers = []): JsonResponse |
||
| 198 | { |
||
| 199 | return $this->respond([ |
||
| 200 | 'message' => $message, |
||
| 201 | ], $headers, []); |
||
| 202 | } |
||
| 203 | |||
| 204 | } |
||
| 205 |