josantonius / http-status-code
PHP library to get HTTP status code messages and definitions.
Fund package maintenance!
Josantonius
Requires
- php: ^8.0
Requires (Dev)
- phpmd/phpmd: ^2.6
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.7
README
Translations: Español
PHP library to get HTTP status code messages and definitions.
- Requirements
- Installation
- Available Classes
- Exceptions Used
- Usage
- List in JSON format
- Tests
- TODO
- Changelog
- Contribution
- Sponsor
- License
Requirements
-
Operating System: Linux | Windows.
-
PHP versions: 8.0 | 8.1 | 8.2.
Installation
The preferred way to install this extension is through Composer.
To install PHP HTTP Status Code library, simply:
composer require josantonius/http-status-code
The previous command will only install the necessary files, if you prefer to download the entire source code you can use:
composer require josantonius/http-status-code --prefer-source
You can also clone the complete repository with Git:
git clone https://github.com/josantonius/http-status-code.git
Available Classes
HttpStatusCode Class
Josantonius\HttpStatusCode\HttpStatusCode
Create a new instance defining the language:
/** * @param string $language The language in which the data will be retrieved. * * Available languages: en (English) * es (Spanish) * * @throws UnsupportedLanguageException if language is not supported. */ public function __construct(string $language = 'en');
Gets message of an HTTP status code:
public function getMessage(int $code): string|null;
Gets the messages of all HTTP status codes:
public function getMessages(): array;
Gets definition of an HTTP status code:
public function getDefinition(int $code): string|null;
Gets the definitions of all HTTP status codes:
public function getDefinitions(): array;
Gets messages and definitions of all HTTP status codes:
public function getAll(): array;
Exceptions Used
use Josantonius\HttpStatusCode\UnsupportedLanguageException;
Usage
Example of use for this library:
Create a new instance defining the language
use Josantonius\HttpStatusCode\HttpStatusCode; $httpStatusCode = new HttpStatusCode(); // Gets the messages in English.
use Josantonius\HttpStatusCode\HttpStatusCode; $httpStatusCode = new HttpStatusCode('es'); // Gets the messages in Spanish.
Gets message of an HTTP status code
use Josantonius\HttpStatusCode\HttpStatusCode; $httpStatusCode = new HttpStatusCode(); $httpStatusCode->getMessage(404);
Returns:
'Not Found'
Gets the messages of all HTTP status codes
use Josantonius\HttpStatusCode\HttpStatusCode; $httpStatusCode = new HttpStatusCode(); $httpStatusCode->getMessages();
Returns:
[ 100 => 'Continue', 101 => 'Switching Protocols', 102 => 'Processing', (...) ]
Gets definition of an HTTP status code
use Josantonius\HttpStatusCode\HttpStatusCode; $httpStatusCode = new HttpStatusCode(); $httpStatusCode->getDefinition(404);
Returns:
'The requested resource could not be found but (...)'
Gets the definitions of all HTTP status codes
use Josantonius\HttpStatusCode\HttpStatusCode; $httpStatusCode = new HttpStatusCode(); $httpStatusCode->getDefinitions();
Returns:
[ 100 => 'The server has received the request (...)', 101 => 'The requester has asked the server (...)', 102 => 'A WebDAV request may contain many (...)', (...) ]
Gets messages and definitions of all HTTP status codes
use Josantonius\HttpStatusCode\HttpStatusCode; $httpStatusCode = new HttpStatusCode(); $httpStatusCode->getAll();
[ 100 => [ 'message' => 'Continue', 'definition' => 'The server has received the request (...)', ], 101 => [ 'message' => 'Switching Protocols', 'definition' => 'The requester has asked the server (...)', ], 102 => [ 'message' => 'Processing', 'definition' => 'A WebDAV request may contain many (...)', ], (...) ]
List in JSON format
https://gist.github.com/Josantonius/0a889ab6f18db2fcefda15a039613293
Tests
To run tests you just need composer and to execute the following:
git clone https://github.com/josantonius/php-http-status-code.git
cd php-http-status-code
composer install
Run unit tests with PHPUnit:
composer phpunit
Run code standard tests with PHPCS:
composer phpcs
Run PHP Mess Detector tests to detect inconsistencies in code style:
composer phpmd
Run all previous tests:
composer tests
TODO
- Add new feature
- Improve tests
- Improve documentation
- Improve English translation in the README file
- Refactor code for disabled code style rules (see phpmd.xml and phpcs.xml)
Changelog
Detailed changes for each release are documented in the release notes.
Contribution
Please make sure to read the Contributing Guide, before making a pull request, start a discussion or report a issue.
Thanks to all contributors! ❤️
Sponsor
If this project helps you to reduce your development time, you can sponsor me to support my open source work 😊
License
This repository is licensed under the MIT License.
Copyright © 2016-present, Josantonius