Skip to content

Commit 861f852

Browse files
committed
JSend standard implemented
1 parent ead6aa3 commit 861f852

File tree

4 files changed

+150
-100
lines changed

4 files changed

+150
-100
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"autoload": {
1717
"psr-4": {
18-
"Firebit\\laravel\\ApiResponse\\": "src/"
18+
"Firebit\\Laravel\\ApiResponse\\": "src/"
1919
}
2020
}
2121
}

src/ApiResponse.php

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
use Illuminate\Http\Response;
77

88
class ApiResponse extends Response {
9+
10+
// Holds the optional error message (in case of a 'error')
11+
protected $errorCode;
12+
13+
// Holds the optional error message (in case of a 'error')
14+
protected $errorMessage;
15+
16+
917
/**
1018
* Sends content for the current web response.
1119
*
@@ -16,7 +24,7 @@ public function sendContent() {
1624
http_response_code($this->statusCode);
1725

1826
if($this->getOriginalContent()){
19-
$content = $this->morphToJson($this->getOriginalContent());
27+
$content = $this->morphToJson($this->formatToJsend());
2028

2129
echo $content;
2230
}
@@ -25,12 +33,67 @@ public function sendContent() {
2533
}
2634

2735
/**
28-
* @param $headers
36+
* Formats the response according to the JSEND standard (https://github.com/omniti-labs/jsend)
37+
*
38+
* @return array|null
2939
*/
30-
public function setHeaders($headers)
31-
{
32-
foreach ($headers as $key => $value){
33-
$this->header($key, $value);
40+
public function formatToJsend(){
41+
// Create an empty response
42+
$response = null;
43+
44+
// If response is successful
45+
if($this->isSuccessful()){
46+
$response = [
47+
'status' => 'success',
48+
'data' => $this->getOriginalContent(),
49+
];
50+
51+
// If reponse is a client error
52+
}elseif ($this->isClientError()){
53+
$response = [
54+
'status' => 'fail',
55+
'data' => $this->getOriginalContent(),
56+
];
57+
58+
// If response is a server error
59+
}elseif ($this->isServerError()){
60+
$response = [
61+
'status' => 'error',
62+
'message' => $this->errorMessage,
63+
];
64+
65+
// Optional error code
66+
if($this->errorCode){
67+
$response['code'] = $this->errorCode;
68+
}
69+
70+
// Optional data
71+
if($this->getOriginalContent()){
72+
$response['data'] = $this->getOriginalContent();
73+
}
74+
3475
}
76+
77+
return $response;
78+
}
79+
80+
/**
81+
* @param $errorCode
82+
* @return $this
83+
*/
84+
public function setErrorCode($errorCode){
85+
$this->errorCode = $errorCode;
86+
87+
return $this;
88+
}
89+
90+
/**
91+
* @param $errorMessage
92+
* @return $this
93+
*/
94+
public function setErrorMessage($errorMessage){
95+
$this->errorMessage = $errorMessage;
96+
97+
return $this;
3598
}
3699
}

src/ApiResponseFactory.php

Lines changed: 41 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/ApiResponseTypes.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
4+
namespace Firebit\Laravel\ApiResponse;
5+
6+
7+
class ApiResponseTypes
8+
{
9+
/**
10+
* @param null $data
11+
* @param int $status_code
12+
* @return ApiResponse
13+
*/
14+
static public function success($data = null, int $status_code = ApiResponseCode::HTTP_SUCCESS){
15+
$response = ApiResponse::create($data, $status_code);
16+
return $response;
17+
}
18+
19+
/**
20+
* @param $data
21+
* @param int $status_code
22+
* @return ApiResponse
23+
*/
24+
static public function fail($data, int $status_code = ApiResponseCode::HTTP_ERROR_CLIENT_NOT_ACCEPTABLE){
25+
$response = ApiResponse::create($data, $status_code);
26+
return $response;
27+
}
28+
29+
/**
30+
* @param $message
31+
* @param int $status_code
32+
* @return ApiResponse
33+
*/
34+
static public function error($message, int $status_code = ApiResponseCode::HTTP_ERROR_SERVER_INTERNAL){
35+
$response = ApiResponse::create(null, $status_code);
36+
$response->setErrorMessage($message);
37+
return $response;
38+
}
39+
}

0 commit comments

Comments
 (0)