snapshotpl / status-code
该包最新版本(1.0.0)没有可用的许可证信息。
PHP 中 HTTP 状态码值对象实现
1.0.0
2016-02-19 18:47 UTC
Requires
- php: ^5.5 || ^7.0
- psr/http-message: ^1.0
Requires (Dev)
- phpunit/phpunit: ^4.8
- zendframework/zend-diactoros: ^1.3
This package is auto-updated.
Last update: 2024-08-29 03:31:31 UTC
README
PHP 中 HTTP 状态码值对象实现。
功能
- 验证,
- 自动设置原因短语(如果已知),
- 不可变,
- 支持 PSR-7
Psr\Http\Message\ResponseInterface
。
支持的 RFC
- https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
- https://tools.ietf.org/html/rfc4918
- https://tools.ietf.org/html/rfc3229
- https://tools.ietf.org/html/rfc5842
- https://tools.ietf.org/html/rfc7538
- https://tools.ietf.org/html/rfc7540
- https://tools.ietf.org/html/rfc7231
- https://tools.ietf.org/html/rfc2518
- https://tools.ietf.org/html/rfc6585
- https://tools.ietf.org/html/rfc2295
- https://tools.ietf.org/html/rfc2774
支持的草稿
安装
添加到 composer
{ "require": { "snapshotpl/status-code": "^1.0" } }
用法
$statusCode = new StatusCode(404); $statusCode->isClientError(); // true $statusCode->isRfc2516() // true $statusCode->isServerError(); // false echo $statusCode; // 404 Not Found
您可以使用它与任何 PSR-7 实现。
$response = new Zend\Diactoros\Response(); $statusCode = StatusCode::createFromResponse($response); echo $statusCode; // 200 OK
$response = new Zend\Diactoros\Response(); $statusCode = new StatusCode(404, 'Not exists'); $newResponse = $statusCode->attachToResponse($response); echo $newResponse->getStatusCode(); // 404 echo $newResponse->getReasonPhrase(); // Not exists