Skip to content

Commit

Permalink
Added more reponse factories
Browse files Browse the repository at this point in the history
  • Loading branch information
JordyAlkema committed Oct 8, 2019
1 parent d878287 commit ead6aa3
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 5 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
],
"require": {
"php": ">=7",
"illuminate/http": "^5.5",
"phpunit/phpunit": "^8.4"
},
"autoload": {
"psr-4": {
"Firebit\\laravel\\ApiResponse\\": "src/"
}
}
}
}
32 changes: 29 additions & 3 deletions src/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,34 @@

namespace Firebit\Laravel\ApiResponse;

use Illuminate\Http\Response;

class ApiResponse
{
class ApiResponse extends Response {
/**
* Sends content for the current web response.
*
* @return $this
*/
public function sendContent() {
$this->header('Content-Type', 'application/json');
http_response_code($this->statusCode);

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

echo $content;
}

return $this;
}

/**
* @param $headers
*/
public function setHeaders($headers)
{
foreach ($headers as $key => $value){
$this->header($key, $value);
}
}
}
33 changes: 33 additions & 0 deletions src/ApiResponseCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php


namespace Firebit\laravel\ApiResponse;


class ApiResponseCode
{
// Informational (100 - 199)

// Success (200 - 299)
const HTTP_SUCCESS = 200;
const HTTP_SUCCESS_CREATED = 201;
const HTTP_SUCCESS_ACCEPTED = 202;
const HTTP_SUCCESS_NO_CONTENT = 204;

// Redirection (300 - 399)
const HTTP_REDIRECT_PERMANENT = 301;
const HTTP_REDIRECT_TEMPORARY = 302;

// Client errors (400 - 499)
const HTTP_ERROR_CLIENT_INVALID_CREDENTIALS = 401;
const HTTP_ERROR_CLIENT_PAYMENT_REQUIRED = 402;
const HTTP_ERROR_CLIENT_LOGIN_REQUIRED = 401;
const HTTP_ERROR_CLIENT_ACCESS_DENIED = 403;
const HTTP_ERROR_CLIENT_NOT_FOUND = 404;
const HTTP_ERROR_CLIENT_NOT_ACCEPTABLE = 406;
const HTTP_ERROR_CLIENT_VALIDATION_ERROR = 422;
const HTTP_ERROR_CLIENT_TOO_MANY_REQUESTS = 429;

// Server error (500 - 599)
const HTTP_ERROR_SERVER_INTERNAL = 500;
}
182 changes: 181 additions & 1 deletion src/ApiResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,185 @@

class ApiResponseFactory
{
// Success (200 - 299)

}
/**
* @param string $content
* @param int $status
* @param array $headers
*
* @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;
}

/**
* @param string $content
* @param int $status
* @param array $headers
*
* @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;
}

/**
* @param string $content
* @param int $status
* @param array $headers
*
* @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;
}

// Redirection (300 - 399)

/**
* @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;
}

/**
* @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;
}

// 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;
}

/**
* @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;
}

/**
* @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;
}

/**
* @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;
}


/**
* @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;
}

/**
* @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;
}

/**
* @param int $status
* @param array $headers
*
* @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;
}

/**
* @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;
}

// Server error (500 - 599)

/**
* @param string $content
* @param int $status
* @param array $headers
*
* @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);
return $response;
}
}

0 comments on commit ead6aa3

Please sign in to comment.