@@ -9,182 +9,130 @@ class ApiResponseFactory
9
9
// Success (200 - 299)
10
10
11
11
/**
12
- * @param string $content
13
- * @param int $status
14
- * @param array $headers
15
- *
12
+ * @param null $content
16
13
* @return ApiResponse
17
14
*/
18
- static public function success ($ content = 'Operation was successful ' , int $ status = ApiResponseCode::HTTP_SUCCESS , array $ headers = array ()) {
19
- $ response = ApiResponse::create ($ content , $ status , $ headers );
20
- return $ response ;
15
+ static public function success ($ content = null ) {
16
+ return ApiResponseTypes::success ($ content );
21
17
}
22
18
23
19
/**
24
- * @param string $content
25
- * @param int $status
26
- * @param array $headers
27
- *
20
+ * @param null $content
28
21
* @return ApiResponse
29
22
*/
30
- static public function created ($ content = 'Resource has been created ' , int $ status = ApiResponseCode::HTTP_SUCCESS_CREATED , array $ headers = array ()) {
31
- $ response = ApiResponse::create ($ content , $ status , $ headers );
32
- return $ response ;
23
+ static public function created ($ content = null ) {
24
+ return ApiResponseTypes::success ($ content , ApiResponseCode::HTTP_SUCCESS_CREATED );
33
25
}
34
26
35
27
/**
36
- * @param string $content
37
- * @param int $status
38
- * @param array $headers
39
- *
28
+ * @param null $content
40
29
* @return ApiResponse
41
30
*/
42
- static public function accepted ($ content = 'The request has been accepted ' , int $ status = ApiResponseCode::HTTP_SUCCESS_ACCEPTED , array $ headers = array ()) {
43
- $ response = ApiResponse::create ($ content , $ status , $ headers );
44
- return $ response ;
31
+ static public function accepted ($ content = null ) {
32
+ return ApiResponseTypes::success ($ content , ApiResponseCode::HTTP_SUCCESS_ACCEPTED );
45
33
}
46
34
47
35
// Redirection (300 - 399)
48
36
49
37
/**
50
- * @param string location
38
+ * @param string $ location
51
39
* @param string $content
52
- * @param int $status
53
- * @param array $headers
54
- *
55
40
* @return ApiResponse
56
41
*/
57
- static public function redirectPermanent (string $ location , $ content = 'The resource has permanently been moved ' , int $ status = ApiResponseCode::HTTP_REDIRECT_PERMANENT , array $ headers = array ()) {
58
- $ headers ['Location ' ] = $ location ;
59
-
60
- $ response = ApiResponse::create ($ content , $ status , $ headers );
61
- return $ response ;
42
+ static public function redirectPermanent (string $ location , $ content = "Resource has permanently been moved " ) {
43
+ return ApiResponseTypes::error ($ content , ApiResponseCode::HTTP_REDIRECT_PERMANENT )->header ('Location ' , $ location );
62
44
}
63
45
64
46
/**
65
- * @param string location
47
+ * @param string $ location
66
48
* @param string $content
67
- * @param int $status
68
- * @param array $headers
69
- *
70
49
* @return ApiResponse
71
50
*/
72
- static public function redirectTemporary (string $ location , $ content = 'The resource has temporary been moved ' , int $ status = ApiResponseCode::HTTP_REDIRECT_TEMPORARY , array $ headers = array ()) {
73
- $ headers ['Location ' ] = $ location ;
74
-
75
- $ response = ApiResponse::create ($ content , $ status , $ headers );
76
- return $ response ;
51
+ static public function redirectTemporary (string $ location , $ content = "Resource has temporarily been moved " ) {
52
+ return ApiResponseTypes::error ($ content , ApiResponseCode::HTTP_REDIRECT_TEMPORARY )->header ('Location ' , $ location );
77
53
}
78
54
79
55
// Client errors (400 - 499)
80
56
81
57
/**
82
58
* @param string $content
83
- * @param int $status
84
- * @param array $headers
85
- *
86
59
* @return ApiResponse
87
60
*/
88
- static public function accessDenied ($ content = 'You are not allowed to use this resource ' , int $ status = ApiResponseCode::HTTP_ERROR_CLIENT_ACCESS_DENIED , array $ headers = array ()){
89
- $ response = ApiResponse::create ($ content , $ status , $ headers );
90
- return $ response ;
61
+ static public function accessDenied ($ content = 'You are not allowed to use this resource ' ){
62
+ return ApiResponseTypes::fail ($ content , ApiResponseCode::HTTP_ERROR_CLIENT_ACCESS_DENIED );
91
63
}
92
64
93
65
/**
94
66
* @param string $content
95
- * @param int $status
96
- * @param array $headers
97
- *
98
67
* @return ApiResponse
99
68
*/
100
- static public function loginRequired ($ content = 'Login is required to use this resource ' , int $ status = ApiResponseCode::HTTP_ERROR_CLIENT_LOGIN_REQUIRED , array $ headers = array ()){
101
- $ response = ApiResponse::create ($ content , $ status , $ headers );
102
- return $ response ;
69
+ static public function loginRequired ($ content = 'Login is required to use this resource ' ){
70
+ return ApiResponseTypes::fail ($ content , ApiResponseCode::HTTP_ERROR_CLIENT_LOGIN_REQUIRED );
103
71
}
104
72
105
73
/**
106
74
* @param string $content
107
- * @param int $status
108
- * @param array $headers
109
- *
110
75
* @return ApiResponse
111
76
*/
112
- static public function notFound ($ content = 'Resource was not found ' , int $ status = ApiResponseCode::HTTP_ERROR_CLIENT_NOT_FOUND , array $ headers = array ()) {
113
- $ response = ApiResponse::create ($ content , $ status , $ headers );
114
- return $ response ;
77
+ static public function notFound ($ content = 'Resource was not found ' ) {
78
+ return ApiResponseTypes::fail ($ content , ApiResponseCode::HTTP_ERROR_CLIENT_NOT_FOUND );
115
79
}
116
80
117
81
/**
118
82
* @param string $content
119
- * @param int $status
120
- * @param array $headers
121
- *
122
83
* @return ApiResponse
123
84
*/
124
- static public function notAcceptable ($ content = 'The supplied parameters are not acceptable ' , int $ status = ApiResponseCode::HTTP_ERROR_CLIENT_NOT_FOUND , array $ headers = array ()) {
125
- $ response = ApiResponse::create ($ content , $ status , $ headers );
126
- return $ response ;
85
+ static public function notAcceptable ($ content = 'The supplied parameters are not acceptable ' ) {
86
+ return ApiResponseTypes::fail ($ content , ApiResponseCode::HTTP_ERROR_CLIENT_NOT_ACCEPTABLE );
127
87
}
128
88
129
89
130
90
/**
131
91
* @param string $content
132
- * @param int $status
133
- * @param array $headers
134
- *
135
92
* @return ApiResponse
136
93
*/
137
- static public function invalidCredentials ($ content = 'Credentials are incorrect ' , int $ status = ApiResponseCode::HTTP_ERROR_CLIENT_INVALID_CREDENTIALS , array $ headers = array ()){
138
- $ response = ApiResponse::create ($ content , $ status , $ headers );
139
- return $ response ;
94
+ static public function invalidCredentials ($ content = 'Credentials are incorrect ' ){
95
+ return ApiResponseTypes::fail ($ content , ApiResponseCode::HTTP_ERROR_CLIENT_INVALID_CREDENTIALS );
140
96
}
141
97
142
98
/**
143
99
* @param string $content
144
- * @param int $status
145
- * @param array $headers
146
- *
147
100
* @return ApiResponse
148
101
*/
149
- static public function paymentRequired ($ content = 'Payment required ' , int $ status = ApiResponseCode::HTTP_ERROR_CLIENT_PAYMENT_REQUIRED , array $ headers = array ()){
150
- $ response = ApiResponse::create ($ content , $ status , $ headers );
151
- return $ response ;
102
+ static public function paymentRequired ($ content = 'Payment required ' ){
103
+ return ApiResponseTypes::fail ($ content , ApiResponseCode::HTTP_ERROR_CLIENT_PAYMENT_REQUIRED );
152
104
}
153
105
154
106
/**
155
- * @param int $status
156
- * @param array $headers
157
- *
107
+ * @param array $validatorErrors
158
108
* @return ApiResponse
159
109
*/
160
- static public function validationError ($ validatorErrors , int $ status = ApiResponseCode::HTTP_ERROR_CLIENT_VALIDATION_ERROR , array $ headers = array ()) {
161
- $ response = ApiResponse::create ($ validatorErrors , $ status , $ headers );
162
- return $ response ;
110
+ static public function validationError (array $ validatorErrors ) {
111
+ return ApiResponseTypes::fail ($ validatorErrors , ApiResponseCode::HTTP_ERROR_CLIENT_VALIDATION_ERROR );
163
112
}
164
113
165
114
/**
166
115
* @param string $content
167
- * @param int $status
168
- * @param array $headers
169
- *
170
116
* @return ApiResponse
171
117
*/
172
- static public function tooManyRequests ($ content = 'You are sending too many requests ' , int $ status = ApiResponseCode::HTTP_ERROR_CLIENT_TOO_MANY_REQUESTS , array $ headers = array ()){
173
- $ response = ApiResponse::create ($ content , $ status , $ headers );
174
- return $ response ;
118
+ static public function tooManyRequests ($ content = 'You are sending too many requests ' ){
119
+ return ApiResponseTypes::fail ($ content , ApiResponseCode::HTTP_ERROR_CLIENT_TOO_MANY_REQUESTS );
175
120
}
176
121
177
122
// Server error (500 - 599)
178
123
179
124
/**
180
- * @param string $content
181
- * @param int $status
182
- * @param array $headers
183
- *
125
+ * @param string $error_message
126
+ * @param int|null $error_code
184
127
* @return ApiResponse
185
128
*/
186
- static public function internalServerError ($ content = 'An internal error has occured ' , int $ status = ApiResponseCode::HTTP_ERROR_SERVER_INTERNAL , array $ headers = array ()){
187
- $ response = ApiResponse::create ($ content , $ status , $ headers );
129
+ static public function internalServerError ($ error_message = 'An internal error has occured ' , $ error_code = null ){
130
+ $ response = ApiResponseTypes::error ($ error_message , ApiResponseCode::HTTP_ERROR_SERVER_INTERNAL );
131
+
132
+ if ($ error_code ){
133
+ $ response ->setErrorCode ($ error_code );
134
+ }
135
+
188
136
return $ response ;
189
137
}
190
138
}
0 commit comments