@@ -9,182 +9,130 @@ class ApiResponseFactory
99 // Success (200 - 299)
1010
1111 /**
12- * @param string $content
13- * @param int $status
14- * @param array $headers
15- *
12+ * @param null $content
1613 * @return ApiResponse
1714 */
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 );
2117 }
2218
2319 /**
24- * @param string $content
25- * @param int $status
26- * @param array $headers
27- *
20+ * @param null $content
2821 * @return ApiResponse
2922 */
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 );
3325 }
3426
3527 /**
36- * @param string $content
37- * @param int $status
38- * @param array $headers
39- *
28+ * @param null $content
4029 * @return ApiResponse
4130 */
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 );
4533 }
4634
4735 // Redirection (300 - 399)
4836
4937 /**
50- * @param string location
38+ * @param string $ location
5139 * @param string $content
52- * @param int $status
53- * @param array $headers
54- *
5540 * @return ApiResponse
5641 */
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 );
6244 }
6345
6446 /**
65- * @param string location
47+ * @param string $ location
6648 * @param string $content
67- * @param int $status
68- * @param array $headers
69- *
7049 * @return ApiResponse
7150 */
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 );
7753 }
7854
7955 // Client errors (400 - 499)
8056
8157 /**
8258 * @param string $content
83- * @param int $status
84- * @param array $headers
85- *
8659 * @return ApiResponse
8760 */
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 );
9163 }
9264
9365 /**
9466 * @param string $content
95- * @param int $status
96- * @param array $headers
97- *
9867 * @return ApiResponse
9968 */
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 );
10371 }
10472
10573 /**
10674 * @param string $content
107- * @param int $status
108- * @param array $headers
109- *
11075 * @return ApiResponse
11176 */
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 );
11579 }
11680
11781 /**
11882 * @param string $content
119- * @param int $status
120- * @param array $headers
121- *
12283 * @return ApiResponse
12384 */
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 );
12787 }
12888
12989
13090 /**
13191 * @param string $content
132- * @param int $status
133- * @param array $headers
134- *
13592 * @return ApiResponse
13693 */
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 );
14096 }
14197
14298 /**
14399 * @param string $content
144- * @param int $status
145- * @param array $headers
146- *
147100 * @return ApiResponse
148101 */
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 );
152104 }
153105
154106 /**
155- * @param int $status
156- * @param array $headers
157- *
107+ * @param array $validatorErrors
158108 * @return ApiResponse
159109 */
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 );
163112 }
164113
165114 /**
166115 * @param string $content
167- * @param int $status
168- * @param array $headers
169- *
170116 * @return ApiResponse
171117 */
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 );
175120 }
176121
177122 // Server error (500 - 599)
178123
179124 /**
180- * @param string $content
181- * @param int $status
182- * @param array $headers
183- *
125+ * @param string $error_message
126+ * @param int|null $error_code
184127 * @return ApiResponse
185128 */
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+
188136 return $ response ;
189137 }
190138}
0 commit comments