Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace CutCode\Common\Traits;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Http\Response;
- /**
- * Trait JsonResponseTrait
- * @package CutCode\Common\Traits
- */
- trait JsonResponseTrait
- {
- /**
- * The request has succeeded.
- * @param array $data
- * @return \Illuminate\Http\JsonResponse
- */
- protected function responseSuccess(array $data = []) : JsonResponse
- {
- return response()->json(array_merge(['status'=>'SUCCESS'], $data), Response::HTTP_OK);
- }
- /**
- * The request has error.
- * @param array $data
- * @param int|null $code
- * @return \Illuminate\Http\JsonResponse
- */
- protected function responseError(array $data = [], int $code = null) : JsonResponse
- {
- return response()->json(array_merge(['status'=>'ERROR'], $data), $code ?? Response::HTTP_OK);
- }
- /**
- * The request has Forbidden.
- * @param string|null $message
- * @return \Illuminate\Http\JsonResponse
- */
- protected function responseForbidden(string $message = null) : JsonResponse
- {
- return response()->json(['message'=> $message ?? Response::$statusTexts[Response::HTTP_FORBIDDEN]], Response::HTTP_FORBIDDEN);
- }
- /**
- * The request has NotFound.
- * @param string|null $message
- * @return \Illuminate\Http\JsonResponse
- */
- protected function responseNotFound(string $message = null) : JsonResponse
- {
- return response()->json(['message'=> $message ?? Response::$statusTexts[Response::HTTP_NOT_FOUND]], Response::HTTP_NOT_FOUND);
- }
- /**
- * The request has responseUnauthorized.
- * @param string|null $message
- * @return \Illuminate\Http\JsonResponse
- */
- protected function responseUnauthorized(string $message = null) : JsonResponse
- {
- return response()->json(['message'=> $message ?? Response::$statusTexts[Response::HTTP_UNAUTHORIZED]], Response::HTTP_UNAUTHORIZED);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement