rigsto/api-http-status

HTTP status codes and resources for API usages

v1.4 2024-01-29 14:26 UTC

This package is auto-updated.

Last update: 2024-10-29 16:13:08 UTC


README

run-tests Latest Version Packagist Version PHP Version Required MIT License

A simple enum class for HTTP status codes and their associated API response.

All codes are taken from Wikipedia.

Installation

Using composer:

$ composer require rigsto/api-http-status

Usage

Http Status Code

Enum

<?php

use Rigsto\ApiHttpStatus\HttpStatus;

HttpStatus::OK;
HttpStatus::NOT_FOUND;
HttpStatus::INTERNAL_SERVER_ERROR;
HttpStatus::BAD_REQUEST;
HttpStatus::UNAUTHORIZED;

Functions

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

Generate Http Status From Code

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

Api Response

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