|
@@ -0,0 +1,115 @@
|
|
|
|
+<?php
|
|
|
|
+
|
|
|
|
+namespace App\Http\Controllers;
|
|
|
|
+
|
|
|
|
+use Illuminate\Http\JsonResponse;
|
|
|
|
+use Illuminate\Http\Response;
|
|
|
|
+
|
|
|
|
+trait JsonResponseHelper
|
|
|
|
+{
|
|
|
|
+ /**
|
|
|
|
+ * 201
|
|
|
|
+ *
|
|
|
|
+ * @param array|string|null $content
|
|
|
|
+ * @return string
|
|
|
|
+ */
|
|
|
|
+ protected function created(array|string $content = null): string
|
|
|
|
+ {
|
|
|
|
+ return new Response($content, Response::HTTP_CREATED);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 202
|
|
|
|
+ *
|
|
|
|
+ * @return Response
|
|
|
|
+ */
|
|
|
|
+ protected function accepted(): Response
|
|
|
|
+ {
|
|
|
|
+ return new Response('', Response::HTTP_ACCEPTED);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 204
|
|
|
|
+ *
|
|
|
|
+ * @return Response
|
|
|
|
+ */
|
|
|
|
+ protected function noContent(): Response
|
|
|
|
+ {
|
|
|
|
+ return new Response('', Response::HTTP_NO_CONTENT);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 400
|
|
|
|
+ *
|
|
|
|
+ * @param string $message
|
|
|
|
+ * @param array $headers
|
|
|
|
+ * @param int $options
|
|
|
|
+ * @return JsonResponse
|
|
|
|
+ */
|
|
|
|
+ protected function badRequest(string $message, array $headers = [], int $options = 0): JsonResponse
|
|
|
|
+ {
|
|
|
|
+ return response()->json([
|
|
|
|
+ 'message' => $message
|
|
|
|
+ ], Response::HTTP_BAD_REQUEST, $headers, $options);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 401
|
|
|
|
+ *
|
|
|
|
+ * @param string $message
|
|
|
|
+ * @param array $headers
|
|
|
|
+ * @param int $options
|
|
|
|
+ * @return JsonResponse
|
|
|
|
+ */
|
|
|
|
+ protected function unauthorized(string $message = '', array $headers = [], int $options = 0): JsonResponse
|
|
|
|
+ {
|
|
|
|
+ return response()->json([
|
|
|
|
+ 'message' => $message ? $message : 'Token Signature could not be verified.'
|
|
|
|
+ ], Response::HTTP_UNAUTHORIZED, $headers, $options);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 403
|
|
|
|
+ *
|
|
|
|
+ * @param string $message
|
|
|
|
+ * @param array $headers
|
|
|
|
+ * @param int $options
|
|
|
|
+ * @return JsonResponse
|
|
|
|
+ */
|
|
|
|
+ protected function forbidden(string $message = '', array $headers = [], int $options = 0): JsonResponse
|
|
|
|
+ {
|
|
|
|
+ return response()->json([
|
|
|
|
+ 'message' => $message ? $message : 'Insufficient permissions.'
|
|
|
|
+ ], Response::HTTP_FORBIDDEN, $headers, $options);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 422
|
|
|
|
+ *
|
|
|
|
+ * @param array $errors
|
|
|
|
+ * @param array $headers
|
|
|
|
+ * @param string $message
|
|
|
|
+ * @param $options
|
|
|
|
+ * @return JsonResponse
|
|
|
|
+ */
|
|
|
|
+ protected function unprocesableEtity(array $errors = [], array $headers = [], string $message = '', $options = 0)
|
|
|
|
+ {
|
|
|
|
+ return response()->json([
|
|
|
|
+ 'message' => $message ? $message : '422 Unprocessable Entity',
|
|
|
|
+ 'errors' => $errors
|
|
|
|
+ ], Response::HTTP_UNPROCESSABLE_ENTITY, $headers, $options);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 200
|
|
|
|
+ *
|
|
|
|
+ * @param array $data
|
|
|
|
+ * @param array $headers
|
|
|
|
+ * @param $options
|
|
|
|
+ * @return JsonResponse
|
|
|
|
+ */
|
|
|
|
+ protected function success(array $data, array $headers = [], $options = 0): JsonResponse
|
|
|
|
+ {
|
|
|
|
+ return response()->json($data, Response::HTTP_OK, $headers, $options);
|
|
|
|
+ }
|
|
|
|
+}
|