Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.96 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement