teapot / status
PHP HTTP Response Status library
Installs: 6 690
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 1
Requires
- php: >=5.3.3
Requires (Dev)
- pdepend/pdepend: *
- phploc/phploc: >=1.7.4
- phpmd/phpmd: >=1.4.0
- phpunit/phpunit: 3.7.*
- shrink/phpcpd: dev-master
- squizlabs/php_codesniffer: 1.*
- theseer/phpdox: *
- zerkalica/php-code-browser: dev-master
This package is not auto-updated.
Last update: 2016-10-05 15:56:54 UTC
README
This is a very simple library that aims to aid verbosity in any Web-based application by defining clearly the HTTP 1.1 response codes as constants. It includes two files: an interface, which contains the constants, and an exception specifically for HTTP.
Using the StatusCodes interface
Assuming for a moment a PHPUnit test on a cURL client response:
<?php
/**
* @test
* @dataProvider someUrlProvider
**/
public function testResponseIsOK($url)
{
$client = new Client($url);
$response = $client->get();
$this->assertEquals(200, $response->getStatusCode());
}
This becomes:
<?php
use \Teapot\HttpResponse\StatusCode;
...
$this->assertEquals(StatusCode:OK, $response->getStatusCode());
While this is a trivial example, the additional verbosity of the code is clearer with other HTTP status codes:
<?php
use \Teapot\HttpResponse\StatusCode;
$this->assertEquals(StatusCode:NOT_FOUND, $response->getStatusCode());
$this->assertEquals(StatusCode:FORBIDDEN, $response->getStatusCode());
$this->assertEquals(StatusCode:MOVED_PERMANENTLY, $response->getStatusCode());
$this->assertEquals(StatusCode:CREATED, $response->getStatusCode());
As StatusCode
is an interface without any methods, you can directly implement it if you prefer:
<?php
class FooController implements \Teapot\HttpResponse\StatusCode
{
public function badAction()
{
if ($this->request->getMethod() == 'POST') {
throw new \Exception('Bad!', self::METHOD_NOT_ALLOWED);
}
}
}
This may be beneficial in an abstract class, so that child classes don't need to explicitly use the interface.
All constants have doc blocks that use the official W3C descriptions of the status code, to aid IDEs and for reference.
Coding Standards
The entire library is intended to be PSR-2 compliant.
Get in touch
If you have any suggestions, feel free to email me at barney+teapot@shrikeh.net or ping me on Twitter with @shrikeh.