14
14
15
15
trait ApiResponseHelpers
16
16
{
17
- private ?array $ _api_helpers_defaultSuccessData = ['success ' => true ];
18
-
19
- /**
20
- * @param string|\Exception $message
21
- * @param string|null $key
22
- *
23
- * @return \Illuminate\Http\JsonResponse
24
- */
17
+ private ?array $ _api_helpers_defaultSuccessData = [
18
+ 'success ' => true ,
19
+ ];
20
+
25
21
public function respondNotFound (
26
- $ message ,
22
+ string | Exception $ message ,
27
23
?string $ key = 'error '
28
24
): JsonResponse {
29
25
return $ this ->apiResponse (
30
- [$ key => $ this ->morphMessage ($ message )],
31
- Response::HTTP_NOT_FOUND
26
+ data: [$ key => $ this ->morphMessage ($ message )],
27
+ code: Response::HTTP_NOT_FOUND
32
28
);
33
29
}
34
30
35
- /**
36
- * @param array|Arrayable|JsonSerializable|null $contents
37
- */
38
- public function respondWithSuccess ($ contents = null ): JsonResponse
39
- {
40
- $ contents = $ this ->morphToArray ($ contents ) ?? [];
31
+ public function respondWithSuccess (
32
+ array |Arrayable |JsonSerializable |null $ contents = null
33
+ ): JsonResponse {
34
+ $ contents = $ this ->morphToArray (data: $ contents ) ?? [];
41
35
42
36
$ data = [] === $ contents
43
37
? $ this ->_api_helpers_defaultSuccessData
44
38
: $ contents ;
45
39
46
- return $ this ->apiResponse ($ data );
40
+ return $ this ->apiResponse (data: $ data );
47
41
}
48
42
49
43
public function setDefaultSuccessResponse (?array $ content = null ): self
@@ -54,90 +48,80 @@ public function setDefaultSuccessResponse(?array $content = null): self
54
48
55
49
public function respondOk (string $ message ): JsonResponse
56
50
{
57
- return $ this ->respondWithSuccess (['success ' => $ message ]);
51
+ return $ this ->respondWithSuccess (contents: ['success ' => $ message ]);
58
52
}
59
53
60
54
public function respondUnAuthenticated (?string $ message = null ): JsonResponse
61
55
{
62
56
return $ this ->apiResponse (
63
- ['error ' => $ message ?? 'Unauthenticated ' ],
64
- Response::HTTP_UNAUTHORIZED
57
+ data: ['error ' => $ message ?? 'Unauthenticated ' ],
58
+ code: Response::HTTP_UNAUTHORIZED
65
59
);
66
60
}
67
61
68
62
public function respondForbidden (?string $ message = null ): JsonResponse
69
63
{
70
64
return $ this ->apiResponse (
71
- ['error ' => $ message ?? 'Forbidden ' ],
72
- Response::HTTP_FORBIDDEN
65
+ data: ['error ' => $ message ?? 'Forbidden ' ],
66
+ code: Response::HTTP_FORBIDDEN
73
67
);
74
68
}
75
69
76
70
public function respondError (?string $ message = null ): JsonResponse
77
71
{
78
72
return $ this ->apiResponse (
79
- ['error ' => $ message ?? 'Error ' ],
80
- Response::HTTP_BAD_REQUEST
73
+ data: ['error ' => $ message ?? 'Error ' ],
74
+ code: Response::HTTP_BAD_REQUEST
81
75
);
82
76
}
83
77
84
- /**
85
- * @param array|Arrayable|JsonSerializable|null $data
86
- */
87
- public function respondCreated ($ data = null ): JsonResponse
88
- {
78
+ public function respondCreated (
79
+ array |Arrayable |JsonSerializable |null $ data = null
80
+ ): JsonResponse {
89
81
$ data ??= [];
82
+
90
83
return $ this ->apiResponse (
91
- $ this ->morphToArray ($ data ),
92
- Response::HTTP_CREATED
84
+ data: $ this ->morphToArray (data: $ data ),
85
+ code: Response::HTTP_CREATED
93
86
);
94
87
}
95
-
96
- /**
97
- * @param string|\Exception $message
98
- * @param string|null $key
99
- *
100
- * @return \Illuminate\Http\JsonResponse
101
- */
88
+
102
89
public function respondFailedValidation (
103
- $ message ,
90
+ string | Exception $ message ,
104
91
?string $ key = 'message '
105
92
): JsonResponse {
106
93
return $ this ->apiResponse (
107
- [$ key => $ this ->morphMessage ($ message )],
108
- Response::HTTP_UNPROCESSABLE_ENTITY
94
+ data: [$ key => $ this ->morphMessage ($ message )],
95
+ code: Response::HTTP_UNPROCESSABLE_ENTITY
109
96
);
110
97
}
111
98
112
99
public function respondTeapot (): JsonResponse
113
100
{
114
101
return $ this ->apiResponse (
115
- ['message ' => 'I \'m a teapot ' ],
116
- Response::HTTP_I_AM_A_TEAPOT
102
+ data: ['message ' => 'I \'m a teapot ' ],
103
+ code: Response::HTTP_I_AM_A_TEAPOT
117
104
);
118
105
}
119
106
120
- /**
121
- * @param array|Arrayable|JsonSerializable|null $data
122
- */
123
- public function respondNoContent ($ data = null ): JsonResponse
124
- {
107
+ public function respondNoContent (
108
+ array |Arrayable |JsonSerializable |null $ data = null
109
+ ): JsonResponse {
125
110
$ data ??= [];
126
- $ data = $ this ->morphToArray ($ data );
111
+ $ data = $ this ->morphToArray (data: $ data );
127
112
128
- return $ this ->apiResponse ($ data , Response::HTTP_NO_CONTENT );
113
+ return $ this ->apiResponse (
114
+ data: $ data ,
115
+ code: Response::HTTP_NO_CONTENT
116
+ );
129
117
}
130
118
131
119
private function apiResponse (array $ data , int $ code = 200 ): JsonResponse
132
120
{
133
- return response ()->json ($ data , $ code );
121
+ return response ()->json (data: $ data , status: $ code );
134
122
}
135
123
136
- /**
137
- * @param array|Arrayable|JsonSerializable|null $data
138
- * @return array|null
139
- */
140
- private function morphToArray ($ data )
124
+ private function morphToArray (array |Arrayable |JsonSerializable |null $ data ): ?array
141
125
{
142
126
if ($ data instanceof Arrayable) {
143
127
return $ data ->toArray ();
@@ -150,11 +134,7 @@ private function morphToArray($data)
150
134
return $ data ;
151
135
}
152
136
153
- /**
154
- * @param string|\Exception $message
155
- * @return string
156
- */
157
- private function morphMessage ($ message ): string
137
+ private function morphMessage (string |Exception $ message ): string
158
138
{
159
139
return $ message instanceof Exception
160
140
? $ message ->getMessage ()
0 commit comments