A simple enum class for HTTP status codes and their associated API response.
All codes are taken from Wikipedia.
Using composer:
$ composer require rigsto/api-http-status
<?php
use Rigsto\ApiHttpStatus\HttpStatus;
HttpStatus::OK;
HttpStatus::NOT_FOUND;
HttpStatus::INTERNAL_SERVER_ERROR;
HttpStatus::BAD_REQUEST;
HttpStatus::UNAUTHORIZED;
getStatusCode()
will return the status code.
getName()
will return the http name.
getCategory()
will return the http category.
isSuccess()
will return true if the status code is a success code.
<?php
use Rigsto\ApiHttpStatus\HttpStatus;
$http = HttpStatus::OK;
$http->getStatusCode(); // 200
$http->getName(); // Ok
$http->getCategory(); // Success
$http->isSuccess(); // true
$http = HttpStatus::UNAUTHORIZED;
$http->getStatusCode(); // 401
$http->getName(); // Unauthorized
$http->getCategory(); // Client Error
$http->isSuccess(); // false
$http = HttpStatus::INTERNAL_SERVER_ERROR;
$http->getStatusCode(); // 500
$http->getName(); // Internal Server Error
$http->getCategory(); // Server Error
$http->isSuccess(); // false
isValidCode()
will return true
if the code is valid and false
if it is not.
fromCode()
will return an HttpStatus
object if the code is valid and null
if it is not.
<?php
use Rigsto\ApiHttpStatus\HttpStatus;
$code = 200;
$codeValidity = HttpStatus::isValidCode($code); // true
$http = HttpStatus::fromCode($code); // HttpStatus::OK
$code = 999
$codeValidity = HttpStatus::isValidCode($code); // false
$http = HttpStatus::fromCode($code); // null
generateResponse(HttpStatus, ?message, ?data)
will return json string of the http status, message, and data.
HttpStatus
is required, but message
and data
are optional. If message
is null, then message will be generated from http status name.
<?php
use Rigsto\ApiHttpStatus\HttpStatus;
use Rigsto\ApiHttpStatus\ApiResponse;
$data = [...];
$response = ApiResponse::generateResponse(HttpStatus::OK, null, $data);
// {"success": true, "code": 200, "message": "Ok", "data": [...]}
$response = ApiResponse::generateResponse(HttpStatus::BAD_REQUEST, 'Custom Message', $data);
// {"success": false, "code": 400, "message": "Custom Message", "data": [...]}
$response = ApiResponse::generateResponse(HttpStatus::INTERNAL_SERVER_ERROR, null, null);
// {"success": false, "code": 500, "message": "Internal Server Error", "data": null}
generateSuccessResponse(?message, ?data)
will return json string of the http status, message, and data. This method is same with function above, but it generates response with HttpStatus::OK
as http status.
<?php
use Rigsto\ApiHttpStatus\ApiResources;
$data = [...];
$response = ApiResources::generateSuccessResponse();
// {"success": true, "code": 200, "message": "Ok", "data": null}
$response = ApiResources::generateSuccessResponse(message: 'Custom Message');
// {"success": true, "code": 200, "message": "Custom Message", "data": null}
$response = ApiResources::generateSuccessResponse(data: $data);
// {"success": true, "code": 200, "message": "Ok", "data": [...]}
generateUnauthorizedResponse()
will return json string with unauthorized http status and message.
<?php
use Rigsto\ApiHttpStatus\ApiResources;
$response = ApiResources::generateUnauthorizedResponse();
// {"success": false, "code": 401, "message": "Unauthorized", "data": null}
generatePaginationResponse(HttpStatus, ?message, ?data
will return json string of http status, message, and paginate data.
Function concept same as generateResponse()
but with pagination data.
<?php
use Rigsto\ApiHttpStatus\HttpStatus;
use Rigsto\ApiHttpStatus\ApiResponse;
$data = ["your data here" => "..."];
$response = ApiResponse::generatePaginationResponse(HttpStatus::OK, null, $data);
{
"success": true,
"code": 200,
"message": "Ok",
"data": {
"data": {
"your data here":"..."
},
"pagination": {
"total": 50,
"per_page": 10,
"current_page": 1,
"last_page": 5,
"from": 1,
"to": 10
}
}
}