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
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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 getStatusText method to get the text representation of a status code:
echo Code::getStatusText(Code::OK); // Outputs: "OK"
echo Code::getStatusText(999); // Outputs: "Unknown Status"
Accessing Status Descriptions
Use the getStatusDescription method to get a description of a status code:
echo Code::getStatusDescription(Code::OK); // Outputs: "The request was successful."
Accessing Status Headers
Use the getHeader method to get a formatted status line, and getHeaderText for the code and
text alone:
echo Code::getHeader(Code::OK); // Outputs: "HTTP/1.1 200 OK"
echo Code::getHeader(Code::NOT_FOUND, 2); // Outputs: "HTTP/2 404 NOT FOUND"
echo Code::getHeaderText(Code::NOT_FOUND); // Outputs: "404 NOT FOUND"
Checking the Class of a Status Code
isInformation, isSuccessful, isRedirection, isClientError and isServerError check the
class of any code (1xx to 5xx), even one without a constant. getStatusRange names it:
var_dump(Code::isServerError(599)); // Outputs: bool(true)
echo Code::getStatusRange(Code::NOT_FOUND); // Outputs: "Client Error"
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";
}
Tests
composer install
composer test
The tests fail on any PHP deprecation, notice or warning, and they also run the examples of this README.
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.