simplicer / http
Library to manage HTTP status codes and their descriptions.
Requires
- php: >=7.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- phpunit/phpunit: ^11.2
- saggre/phpdocumentor-markdown: ^0.1.4
README
This library provides a comprehensive list of HTTP status codes, their texts, and descriptions, as well as utility methods for working with HTTP status codes.
Installation
Use composer to install the library:
composer require simplicer/http
Usage
Accessing Status Codes
You can access status codes directly using the provided constants:
use Simplicer\Http\Status\Code;
echo Code::OK; // Outputs: 200
Accessing Status Texts
Use the getText
method to get the text representation of a status code:
echo Code::getText(Code::OK); // Outputs: "OK"
Accessing Status Descriptions
Use the getDescription
method to get a description of a status code:
echo Code::getDescription(Code::OK); // Outputs: "The request was successfully processed."
Accessing Status Headers
Use the getStatusHeader
method to get a formatted status header:
echo Code::getStatusHeader(Code::OK); // Outputs: "HTTP/1.1 200 OK"
Validating Status Codes
Use the isValidStatusCode
method to check if a status code is valid:
var_dump(Code::isValidStatusCode(200)); // Outputs: bool(true)
var_dump(Code::isValidStatusCode(999)); // Outputs: bool(false)
Grouping Status Codes by Category
You can get status codes grouped by their categories:
$statusCodesByCategory = Code::getStatusCodesByCategory();
print_r($statusCodesByCategory);
Calling Status Codes as Methods
You can call status codes as methods directly:
echo Code::OK(); // Outputs: 200
Handling Invalid Status Code Exceptions
This library throws an InvalidStatusCodeException
when an invalid HTTP status code is encountered. You can catch and handle this exception in your code as follows:
use Simplicer\Http\Status\Code;
use Simplicer\Http\Status\InvalidStatusCodeException;
try {
$statusCode = Code::INVALID_STATUS_CODE_CONSTANT();
} catch (InvalidStatusCodeException $e) {
echo "Caught exception: " . $e->getMessage() . "\n";
echo "Invalid status code: " . $e->getStatusCode() . "\n";
}
More information
You can check the PHPDoc autogenerated technical documentation in markdown on /doc
directory.
Author
Antonio VillamarĂn
License
This library is licensed under the GPLv3 License.