55use Illuminate \Support \Facades \Validator ;
66use Illuminate \Http \Response as IlluminateResponse ;
77
8+ /**
9+ * Class LaravelResponder
10+ * @package gaurav93d\LaravelResponder
11+ */
812class LaravelResponder
913{
14+ /**
15+ * @var array
16+ */
1017 protected $ headers = [];
18+ /**
19+ * @var int
20+ */
1121 protected $ status = 200 ;
22+ /**
23+ * @var array
24+ */
1225 protected $ data = [];
26+ /**
27+ * @var bool
28+ */
1329 protected $ success = true ;
30+ /**
31+ * @var array
32+ */
1433 protected $ errors = [];
1534
35+
36+ /**
37+ * @param array $headers
38+ * @return LaravelResponder
39+ */
1640 public function headers ($ headers = [])
1741 {
1842 $ this ->headers = $ headers ;
1943 return $ this ;
2044 }
2145
46+ /**
47+ * @param array $data
48+ * @param int $status
49+ * @return mixed
50+ */
2251 public function success ($ data = [], $ status = 200 )
2352 {
2453 $ this ->data = $ data ;
2554 $ this ->status = $ status ;
2655 return $ this ->respond ();
2756 }
2857
58+ /**
59+ * @param array $errors
60+ * @param int $status
61+ * @return mixed
62+ */
2963 public function errors ($ errors = [], $ status = 200 )
3064 {
3165 $ this ->errors = $ errors ;
@@ -34,6 +68,9 @@ public function errors($errors = [], $status = 200)
3468 return $ this ->respond ();
3569 }
3670
71+ /**
72+ * @return mixed
73+ */
3774 public function respond ()
3875 {
3976 $ responseData = [
@@ -45,31 +82,56 @@ public function respond()
4582 return response ()->json ($ responseData , $ this ->status , $ this ->headers );
4683 }
4784
85+ /**
86+ * @param string $message
87+ * @param int $status
88+ * @return mixed
89+ */
4890 public function error ($ message = 'Error! ' , $ status = 200 )
4991 {
5092 return $ this ->errors ([$ message ], $ status );
5193 }
5294
95+ /**
96+ * @param Validator $validator
97+ * @return mixed
98+ */
5399 public function respondValidationErrors (Validator $ validator )
54100 {
55101 return $ this ->errors ($ validator ->errors (), IlluminateResponse::HTTP_BAD_REQUEST );
56102 }
57103
104+ /**
105+ * @param string $message
106+ * @return mixed
107+ */
58108 public function respondInternalError ($ message = 'Internal Error! ' )
59109 {
60110 return $ this ->errors ([$ message ], IlluminateResponse::HTTP_INTERNAL_SERVER_ERROR );
61111 }
62112
113+ /**
114+ * @param string $message
115+ * @return mixed
116+ */
63117 public function respondUnauthorizedError ($ message = 'Unauthorized! ' )
64118 {
65119 return $ this ->errors ([$ message ], IlluminateResponse::HTTP_UNAUTHORIZED );
66120 }
67121
122+ /**
123+ * @param string $message
124+ * @return mixed
125+ */
68126 public function respondBadRequestError ($ message = 'Bad Request! ' )
69127 {
70128 return $ this ->errors ([$ message ], IlluminateResponse::HTTP_BAD_REQUEST );
71129 }
72130
131+ /**
132+ * @param string $message
133+ * @return mixed
134+ */
73135 public function respondNotFoundError ($ message = 'Not found! ' )
74136 {
75137 return $ this ->errors ([$ message ], IlluminateResponse::HTTP_NOT_FOUND );
0 commit comments