gino-pane / nano-http-status
真正简约且自包含的包,用于处理HTTP状态和相关描述消息
v1.0.1
2017-12-12 12:01 UTC
Requires
- php: ^7.0
Requires (Dev)
- phpdocumentor/phpdocumentor: ^2.0
- phpmd/phpmd: ^2.6
- phpunit/phpunit: ^6.0
- squizlabs/php_codesniffer: ^3.0
This package is auto-updated.
Last update: 2024-08-26 05:39:46 UTC
README
真正简约且自包含的包,用于处理HTTP状态和相关描述消息。像 Teapot 或 Httpstatus 这样的包虽然很好且知名,但仍然要么过于笨重和复杂,要么根本不符合Gino Pane对高代码质量的标准。
要求
- PHP >= 7.0;
特性
- HTTP状态代码的完整列表,作为可读常量;
- 相关的状态消息;
- 自定义消息的能力;
- 轻松检测状态类的能力;
- 生产环境中无依赖;
- 集成代码质量、测试和构建文档的工具。
安装
composer require gino-pane/nano-http-status
基本用法
检查代码的存在(使用数字代码或易于阅读的常量)
(new NanoHttpStatus())->statusExists(200); //true
(new NanoHttpStatus())->statusExists(400); //true
(new NanoHttpStatus())->statusExists(451); //true
(new NanoHttpStatus())->statusExists(511); //true
(new NanoHttpStatus())->statusExists(522); //false
使用代码数字检测代码类别
(new NanoHttpStatus())->isInformational(NanoHttpStatus::HTTP_OK); //false
(new NanoHttpStatus())->isSuccess(202); //true
(new NanoHttpStatus())->isRedirection(301); //true
(new NanoHttpStatus())->isClientError(404); //true
(new NanoHttpStatus())->isServerError(NanoHttpStatus::HTTP_BAD_REQUEST); //false
通过状态代码获取状态消息
(new NanoHttpStatus())->getMessage(200); //OK
(new NanoHttpStatus())->getMessage(451); //Unavailable For Legal Reasons
(new NanoHttpStatus())->getMessage(452); //Undefined Status
设置本地化映射并获取自定义状态消息
$status = new NanoHttpStatus([
NanoHttpStatus::HTTP_BAD_REQUEST => 'Very bad request',
NanoHttpStatus::HTTP_BAD_GATEWAY => 'Not so bad gateway'
]);
$status->getMessage(400); //'Very bad request'
$status->getMessage(502); //'Not so bad gateway'
请注意,NanoHttpStatus
本身不会对无效状态抛出任何异常。
有用工具
运行测试
php vendor/bin/phpunit
或
composer test
代码检查工具
php vendor/bin/phpcs --standard=PSR2 src/
或
composer psr2check
代码自动修复工具
php vendor/bin/phpcbf --standard=PSR2 src/
或
composer psr2autofix
构建文档
php vendor/bin/phpdoc -d "src" -t "docs"
或
composer docs
变更日志
请参阅 CHANGELOG.md 以保持跟踪。
贡献
- 将它分支出来。
- 创建您的功能分支(git checkout -b my-new-feature)。
- 进行您的更改。
- 运行测试,如果需要为您的代码添加新测试(phpunit)。
- 提交您的更改(git commit -am '添加某些功能')。
- 将更改推送到分支(git push origin my-new-feature)。
- 创建新的拉取请求。
也请参阅 CONTRIBUTION.md。
许可
请参阅 LICENSE。