SHARE
TWEET

Untitled

a guest Nov 15th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. namespace CutCode\Common\Traits;
  3.  
  4. use Illuminate\Http\JsonResponse;
  5. use Illuminate\Http\Response;
  6.  
  7. /**
  8.  * Trait JsonResponseTrait
  9.  * @package CutCode\Common\Traits
  10.  */
  11. trait JsonResponseTrait
  12. {
  13.     /**
  14.      * The request has succeeded.
  15.      * @param array $data
  16.      * @return \Illuminate\Http\JsonResponse
  17.      */
  18.     protected function responseSuccess(array $data = []) : JsonResponse
  19.     {
  20.         return response()->json(array_merge(['status'=>'SUCCESS'], $data), Response::HTTP_OK);
  21.     }
  22.  
  23.     /**
  24.      * The request has error.
  25.      * @param array $data
  26.      * @param int|null $code
  27.      * @return \Illuminate\Http\JsonResponse
  28.      */
  29.     protected function responseError(array $data = [], int $code = null) : JsonResponse
  30.     {
  31.         return response()->json(array_merge(['status'=>'ERROR'], $data), $code ?? Response::HTTP_OK);
  32.     }
  33.  
  34.     /**
  35.      * The request has Forbidden.
  36.      * @param string|null $message
  37.      * @return \Illuminate\Http\JsonResponse
  38.      */
  39.     protected function responseForbidden(string $message = null) : JsonResponse
  40.     {
  41.         return response()->json(['message'=> $message ?? Response::$statusTexts[Response::HTTP_FORBIDDEN]], Response::HTTP_FORBIDDEN);
  42.     }
  43.  
  44.     /**
  45.      * The request has NotFound.
  46.      * @param string|null $message
  47.      * @return \Illuminate\Http\JsonResponse
  48.      */
  49.     protected function responseNotFound(string $message = null) : JsonResponse
  50.     {
  51.         return response()->json(['message'=> $message ?? Response::$statusTexts[Response::HTTP_NOT_FOUND]], Response::HTTP_NOT_FOUND);
  52.     }
  53.  
  54.     /**
  55.      * The request has responseUnauthorized.
  56.      * @param string|null $message
  57.      * @return \Illuminate\Http\JsonResponse
  58.      */
  59.     protected function responseUnauthorized(string $message = null) : JsonResponse
  60.     {
  61.         return response()->json(['message'=> $message ?? Response::$statusTexts[Response::HTTP_UNAUTHORIZED]], Response::HTTP_UNAUTHORIZED);
  62.     }
  63. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top