Skip to content

Commit

Permalink
JSend standard implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
JordyAlkema committed Oct 8, 2019
1 parent ead6aa3 commit 861f852
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 100 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"autoload": {
"psr-4": {
"Firebit\\laravel\\ApiResponse\\": "src/"
"Firebit\\Laravel\\ApiResponse\\": "src/"
}
}
}
75 changes: 69 additions & 6 deletions src/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
use Illuminate\Http\Response;

class ApiResponse extends Response {

// Holds the optional error message (in case of a 'error')
protected $errorCode;

// Holds the optional error message (in case of a 'error')
protected $errorMessage;


/**
* Sends content for the current web response.
*
Expand All @@ -16,7 +24,7 @@ public function sendContent() {
http_response_code($this->statusCode);

if($this->getOriginalContent()){
$content = $this->morphToJson($this->getOriginalContent());
$content = $this->morphToJson($this->formatToJsend());

echo $content;
}
Expand All @@ -25,12 +33,67 @@ public function sendContent() {
}

/**
* @param $headers
* Formats the response according to the JSEND standard (https://github.com/omniti-labs/jsend)
*
* @return array|null
*/
public function setHeaders($headers)
{
foreach ($headers as $key => $value){
$this->header($key, $value);
public function formatToJsend(){
// Create an empty response
$response = null;

// If response is successful
if($this->isSuccessful()){
$response = [
'status' => 'success',
'data' => $this->getOriginalContent(),
];

// If reponse is a client error
}elseif ($this->isClientError()){
$response = [
'status' => 'fail',
'data' => $this->getOriginalContent(),
];

// If response is a server error
}elseif ($this->isServerError()){
$response = [
'status' => 'error',
'message' => $this->errorMessage,
];

// Optional error code
if($this->errorCode){
$response['code'] = $this->errorCode;
}

// Optional data
if($this->getOriginalContent()){
$response['data'] = $this->getOriginalContent();
}

}

return $response;
}

/**
* @param $errorCode
* @return $this
*/
public function setErrorCode($errorCode){
$this->errorCode = $errorCode;

return $this;
}

/**
* @param $errorMessage
* @return $this
*/
public function setErrorMessage($errorMessage){
$this->errorMessage = $errorMessage;

return $this;
}
}
134 changes: 41 additions & 93 deletions src/ApiResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,182 +9,130 @@ class ApiResponseFactory
// Success (200 - 299)

/**
* @param string $content
* @param int $status
* @param array $headers
*
* @param null $content
* @return ApiResponse
*/
static public function success($content = 'Operation was successful', int $status = ApiResponseCode::HTTP_SUCCESS, array $headers = array()) {
$response = ApiResponse::create($content, $status, $headers);
return $response;
static public function success($content = null) {
return ApiResponseTypes::success($content);
}

/**
* @param string $content
* @param int $status
* @param array $headers
*
* @param null $content
* @return ApiResponse
*/
static public function created($content = 'Resource has been created', int $status = ApiResponseCode::HTTP_SUCCESS_CREATED, array $headers = array()) {
$response = ApiResponse::create($content, $status, $headers);
return $response;
static public function created($content = null) {
return ApiResponseTypes::success($content, ApiResponseCode::HTTP_SUCCESS_CREATED);
}

/**
* @param string $content
* @param int $status
* @param array $headers
*
* @param null $content
* @return ApiResponse
*/
static public function accepted($content = 'The request has been accepted', int $status = ApiResponseCode::HTTP_SUCCESS_ACCEPTED, array $headers = array()) {
$response = ApiResponse::create($content, $status, $headers);
return $response;
static public function accepted($content = null) {
return ApiResponseTypes::success($content, ApiResponseCode::HTTP_SUCCESS_ACCEPTED);
}

// Redirection (300 - 399)

/**
* @param string location
* @param string $location
* @param string $content
* @param int $status
* @param array $headers
*
* @return ApiResponse
*/
static public function redirectPermanent(string $location, $content = 'The resource has permanently been moved', int $status = ApiResponseCode::HTTP_REDIRECT_PERMANENT, array $headers = array()) {
$headers['Location'] = $location;

$response = ApiResponse::create($content, $status, $headers);
return $response;
static public function redirectPermanent(string $location, $content = "Resource has permanently been moved") {
return ApiResponseTypes::error($content, ApiResponseCode::HTTP_REDIRECT_PERMANENT)->header('Location', $location);
}

/**
* @param string location
* @param string $location
* @param string $content
* @param int $status
* @param array $headers
*
* @return ApiResponse
*/
static public function redirectTemporary(string $location, $content = 'The resource has temporary been moved', int $status = ApiResponseCode::HTTP_REDIRECT_TEMPORARY, array $headers = array()) {
$headers['Location'] = $location;

$response = ApiResponse::create($content, $status, $headers);
return $response;
static public function redirectTemporary(string $location, $content = "Resource has temporarily been moved") {
return ApiResponseTypes::error($content, ApiResponseCode::HTTP_REDIRECT_TEMPORARY)->header('Location', $location);
}

// Client errors (400 - 499)

/**
* @param string $content
* @param int $status
* @param array $headers
*
* @return ApiResponse
*/
static public function accessDenied($content = 'You are not allowed to use this resource', int $status = ApiResponseCode::HTTP_ERROR_CLIENT_ACCESS_DENIED, array $headers = array()){
$response = ApiResponse::create($content, $status, $headers);
return $response;
static public function accessDenied($content = 'You are not allowed to use this resource'){
return ApiResponseTypes::fail($content, ApiResponseCode::HTTP_ERROR_CLIENT_ACCESS_DENIED);
}

/**
* @param string $content
* @param int $status
* @param array $headers
*
* @return ApiResponse
*/
static public function loginRequired($content = 'Login is required to use this resource', int $status = ApiResponseCode::HTTP_ERROR_CLIENT_LOGIN_REQUIRED, array $headers = array()){
$response = ApiResponse::create($content, $status, $headers);
return $response;
static public function loginRequired($content = 'Login is required to use this resource'){
return ApiResponseTypes::fail($content, ApiResponseCode::HTTP_ERROR_CLIENT_LOGIN_REQUIRED);
}

/**
* @param string $content
* @param int $status
* @param array $headers
*
* @return ApiResponse
*/
static public function notFound($content = 'Resource was not found', int $status = ApiResponseCode::HTTP_ERROR_CLIENT_NOT_FOUND, array $headers = array()) {
$response = ApiResponse::create($content, $status, $headers);
return $response;
static public function notFound($content = 'Resource was not found') {
return ApiResponseTypes::fail($content, ApiResponseCode::HTTP_ERROR_CLIENT_NOT_FOUND);
}

/**
* @param string $content
* @param int $status
* @param array $headers
*
* @return ApiResponse
*/
static public function notAcceptable($content = 'The supplied parameters are not acceptable', int $status = ApiResponseCode::HTTP_ERROR_CLIENT_NOT_FOUND, array $headers = array()) {
$response = ApiResponse::create($content, $status, $headers);
return $response;
static public function notAcceptable($content = 'The supplied parameters are not acceptable') {
return ApiResponseTypes::fail($content, ApiResponseCode::HTTP_ERROR_CLIENT_NOT_ACCEPTABLE);
}


/**
* @param string $content
* @param int $status
* @param array $headers
*
* @return ApiResponse
*/
static public function invalidCredentials($content = 'Credentials are incorrect', int $status = ApiResponseCode::HTTP_ERROR_CLIENT_INVALID_CREDENTIALS, array $headers = array()){
$response = ApiResponse::create($content, $status, $headers);
return $response;
static public function invalidCredentials($content = 'Credentials are incorrect'){
return ApiResponseTypes::fail($content, ApiResponseCode::HTTP_ERROR_CLIENT_INVALID_CREDENTIALS);
}

/**
* @param string $content
* @param int $status
* @param array $headers
*
* @return ApiResponse
*/
static public function paymentRequired($content = 'Payment required', int $status = ApiResponseCode::HTTP_ERROR_CLIENT_PAYMENT_REQUIRED, array $headers = array()){
$response = ApiResponse::create($content, $status, $headers);
return $response;
static public function paymentRequired($content = 'Payment required'){
return ApiResponseTypes::fail($content, ApiResponseCode::HTTP_ERROR_CLIENT_PAYMENT_REQUIRED);
}

/**
* @param int $status
* @param array $headers
*
* @param array $validatorErrors
* @return ApiResponse
*/
static public function validationError($validatorErrors, int $status = ApiResponseCode::HTTP_ERROR_CLIENT_VALIDATION_ERROR, array $headers = array()) {
$response = ApiResponse::create($validatorErrors, $status, $headers);
return $response;
static public function validationError(array $validatorErrors) {
return ApiResponseTypes::fail($validatorErrors, ApiResponseCode::HTTP_ERROR_CLIENT_VALIDATION_ERROR);
}

/**
* @param string $content
* @param int $status
* @param array $headers
*
* @return ApiResponse
*/
static public function tooManyRequests($content = 'You are sending too many requests', int $status = ApiResponseCode::HTTP_ERROR_CLIENT_TOO_MANY_REQUESTS, array $headers = array()){
$response = ApiResponse::create($content, $status, $headers);
return $response;
static public function tooManyRequests($content = 'You are sending too many requests'){
return ApiResponseTypes::fail($content, ApiResponseCode::HTTP_ERROR_CLIENT_TOO_MANY_REQUESTS);
}

// Server error (500 - 599)

/**
* @param string $content
* @param int $status
* @param array $headers
*
* @param string $error_message
* @param int|null $error_code
* @return ApiResponse
*/
static public function internalServerError($content = 'An internal error has occured', int $status = ApiResponseCode::HTTP_ERROR_SERVER_INTERNAL, array $headers = array()){
$response = ApiResponse::create($content, $status, $headers);
static public function internalServerError($error_message = 'An internal error has occured', $error_code = null){
$response = ApiResponseTypes::error($error_message, ApiResponseCode::HTTP_ERROR_SERVER_INTERNAL);

if($error_code){
$response->setErrorCode($error_code);
}

return $response;
}
}
39 changes: 39 additions & 0 deletions src/ApiResponseTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php


namespace Firebit\Laravel\ApiResponse;


class ApiResponseTypes
{
/**
* @param null $data
* @param int $status_code
* @return ApiResponse
*/
static public function success($data = null, int $status_code = ApiResponseCode::HTTP_SUCCESS){
$response = ApiResponse::create($data, $status_code);
return $response;
}

/**
* @param $data
* @param int $status_code
* @return ApiResponse
*/
static public function fail($data, int $status_code = ApiResponseCode::HTTP_ERROR_CLIENT_NOT_ACCEPTABLE){
$response = ApiResponse::create($data, $status_code);
return $response;
}

/**
* @param $message
* @param int $status_code
* @return ApiResponse
*/
static public function error($message, int $status_code = ApiResponseCode::HTTP_ERROR_SERVER_INTERNAL){
$response = ApiResponse::create(null, $status_code);
$response->setErrorMessage($message);
return $response;
}
}

0 comments on commit 861f852

Please sign in to comment.