Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement StatusCodeInterface #22

Merged
merged 1 commit into from
Nov 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
],
"require": {
"php": ">=7.1.0",
"psr/http-message": "^1.0",
"psr/http-factory": "^1.0"
"fig/http-message-util": "^1.1",
"psr/http-factory": "^1.0",
"psr/http-message": "^1.0"
},
"require-dev": {
"http-interop/http-factory-tests": "^0.5.0",
Expand Down
7 changes: 5 additions & 2 deletions src/Factory/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Slim\Psr7\Factory;

use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Slim\Psr7\Response;
Expand All @@ -27,8 +28,10 @@ class ResponseFactory implements ResponseFactoryInterface
*
* @return ResponseInterface
*/
public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
{
public function createResponse(
int $code = StatusCodeInterface::STATUS_OK,
string $reasonPhrase = ''
): ResponseInterface {
$res = new Response($code);

if ($reasonPhrase !== '') {
Expand Down
173 changes: 96 additions & 77 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
namespace Slim\Psr7;

use Fig\Http\Message\StatusCodeInterface;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
Expand All @@ -30,7 +31,7 @@ class Response extends Message implements ResponseInterface
*
* @var int
*/
protected $status = 200;
protected $status = StatusCodeInterface::STATUS_OK;

/**
* Reason phrase
Expand All @@ -46,73 +47,73 @@ class Response extends Message implements ResponseInterface
*/
protected static $messages = [
//Informational 1xx
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
StatusCodeInterface::STATUS_CONTINUE => 'Continue',
StatusCodeInterface::STATUS_SWITCHING_PROTOCOLS => 'Switching Protocols',
StatusCodeInterface::STATUS_PROCESSING => 'Processing',
//Successful 2xx
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
207 => 'Multi-Status',
208 => 'Already Reported',
226 => 'IM Used',
StatusCodeInterface::STATUS_OK => 'OK',
StatusCodeInterface::STATUS_CREATED => 'Created',
StatusCodeInterface::STATUS_ACCEPTED => 'Accepted',
StatusCodeInterface::STATUS_NON_AUTHORITATIVE_INFORMATION => 'Non-Authoritative Information',
StatusCodeInterface::STATUS_NO_CONTENT => 'No Content',
StatusCodeInterface::STATUS_RESET_CONTENT => 'Reset Content',
StatusCodeInterface::STATUS_PARTIAL_CONTENT => 'Partial Content',
StatusCodeInterface::STATUS_MULTI_STATUS => 'Multi-Status',
StatusCodeInterface::STATUS_ALREADY_REPORTED => 'Already Reported',
StatusCodeInterface::STATUS_IM_USED => 'IM Used',
//Redirection 3xx
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
306 => '(Unused)',
307 => 'Temporary Redirect',
308 => 'Permanent Redirect',
StatusCodeInterface::STATUS_MULTIPLE_CHOICES => 'Multiple Choices',
StatusCodeInterface::STATUS_MOVED_PERMANENTLY => 'Moved Permanently',
StatusCodeInterface::STATUS_FOUND => 'Found',
StatusCodeInterface::STATUS_SEE_OTHER => 'See Other',
StatusCodeInterface::STATUS_NOT_MODIFIED => 'Not Modified',
StatusCodeInterface::STATUS_USE_PROXY => 'Use Proxy',
StatusCodeInterface::STATUS_RESERVED => '(Unused)',
StatusCodeInterface::STATUS_TEMPORARY_REDIRECT => 'Temporary Redirect',
StatusCodeInterface::STATUS_PERMANENT_REDIRECT => 'Permanent Redirect',
//Client Error 4xx
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
418 => 'I\'m a teapot',
421 => 'Misdirected Request',
422 => 'Unprocessable Entity',
423 => 'Locked',
424 => 'Failed Dependency',
426 => 'Upgrade Required',
428 => 'Precondition Required',
429 => 'Too Many Requests',
431 => 'Request Header Fields Too Large',
StatusCodeInterface::STATUS_BAD_REQUEST => 'Bad Request',
StatusCodeInterface::STATUS_UNAUTHORIZED => 'Unauthorized',
StatusCodeInterface::STATUS_PAYMENT_REQUIRED => 'Payment Required',
StatusCodeInterface::STATUS_FORBIDDEN => 'Forbidden',
StatusCodeInterface::STATUS_NOT_FOUND => 'Not Found',
StatusCodeInterface::STATUS_METHOD_NOT_ALLOWED => 'Method Not Allowed',
StatusCodeInterface::STATUS_NOT_ACCEPTABLE => 'Not Acceptable',
StatusCodeInterface::STATUS_PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required',
StatusCodeInterface::STATUS_REQUEST_TIMEOUT => 'Request Timeout',
StatusCodeInterface::STATUS_CONFLICT => 'Conflict',
StatusCodeInterface::STATUS_GONE => 'Gone',
StatusCodeInterface::STATUS_LENGTH_REQUIRED => 'Length Required',
StatusCodeInterface::STATUS_PRECONDITION_FAILED => 'Precondition Failed',
StatusCodeInterface::STATUS_PAYLOAD_TOO_LARGE => 'Request Entity Too Large',
StatusCodeInterface::STATUS_URI_TOO_LONG => 'Request-URI Too Long',
StatusCodeInterface::STATUS_UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type',
StatusCodeInterface::STATUS_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable',
StatusCodeInterface::STATUS_EXPECTATION_FAILED => 'Expectation Failed',
StatusCodeInterface::STATUS_IM_A_TEAPOT => 'I\'m a teapot',
StatusCodeInterface::STATUS_MISDIRECTED_REQUEST => 'Misdirected Request',
StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY => 'Unprocessable Entity',
StatusCodeInterface::STATUS_LOCKED => 'Locked',
StatusCodeInterface::STATUS_FAILED_DEPENDENCY => 'Failed Dependency',
StatusCodeInterface::STATUS_UPGRADE_REQUIRED => 'Upgrade Required',
StatusCodeInterface::STATUS_PRECONDITION_REQUIRED => 'Precondition Required',
StatusCodeInterface::STATUS_TOO_MANY_REQUESTS => 'Too Many Requests',
StatusCodeInterface::STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE => 'Request Header Fields Too Large',
444 => 'Connection Closed Without Response',
451 => 'Unavailable For Legal Reasons',
StatusCodeInterface::STATUS_UNAVAILABLE_FOR_LEGAL_REASONS => 'Unavailable For Legal Reasons',
499 => 'Client Closed Request',
//Server Error 5xx
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
506 => 'Variant Also Negotiates',
507 => 'Insufficient Storage',
508 => 'Loop Detected',
510 => 'Not Extended',
511 => 'Network Authentication Required',
StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR => 'Internal Server Error',
StatusCodeInterface::STATUS_NOT_IMPLEMENTED => 'Not Implemented',
StatusCodeInterface::STATUS_BAD_GATEWAY => 'Bad Gateway',
StatusCodeInterface::STATUS_SERVICE_UNAVAILABLE => 'Service Unavailable',
StatusCodeInterface::STATUS_GATEWAY_TIMEOUT => 'Gateway Timeout',
StatusCodeInterface::STATUS_VERSION_NOT_SUPPORTED => 'HTTP Version Not Supported',
StatusCodeInterface::STATUS_VARIANT_ALSO_NEGOTIATES => 'Variant Also Negotiates',
StatusCodeInterface::STATUS_INSUFFICIENT_STORAGE => 'Insufficient Storage',
StatusCodeInterface::STATUS_LOOP_DETECTED => 'Loop Detected',
StatusCodeInterface::STATUS_NOT_EXTENDED => 'Not Extended',
StatusCodeInterface::STATUS_NETWORK_AUTHENTICATION_REQUIRED => 'Network Authentication Required',
599 => 'Network Connect Timeout Error',
];

Expand All @@ -130,8 +131,11 @@ class Response extends Message implements ResponseInterface
* @param HeadersInterface|null $headers The response headers.
* @param StreamInterface|null $body The response body.
*/
public function __construct($status = 200, HeadersInterface $headers = null, StreamInterface $body = null)
{
public function __construct(
$status = StatusCodeInterface::STATUS_OK,
HeadersInterface $headers = null,
StreamInterface $body = null
) {
$this->status = $this->filterStatus($status);
$this->headers = $headers ? $headers : new Headers();
$this->body = $body ? $body : new Body(fopen('php://temp', 'r+'));
Expand Down Expand Up @@ -217,7 +221,7 @@ public function withStatus($code, $reasonPhrase = '')
*/
protected function filterStatus($status)
{
if (!is_integer($status) || $status<100 || $status>599) {
if (!is_integer($status) || $status < StatusCodeInterface::STATUS_CONTINUE || $status > 599) {
throw new InvalidArgumentException('Invalid HTTP status code');
}

Expand Down Expand Up @@ -289,8 +293,8 @@ public function withRedirect($url, $status = null)
{
$responseWithRedirect = $this->withHeader('Location', (string)$url);

if (is_null($status) && $this->getStatusCode() === 200) {
$status = 302;
if (is_null($status) && $this->getStatusCode() === StatusCodeInterface::STATUS_OK) {
$status = StatusCodeInterface::STATUS_FOUND;
}

if (!is_null($status)) {
Expand Down Expand Up @@ -340,7 +344,11 @@ public function withJson($data, $status = null, $encodingOptions = 0)
*/
public function isEmpty()
{
return in_array($this->getStatusCode(), [204, 205, 304]);
return in_array($this->getStatusCode(), [
StatusCodeInterface::STATUS_NO_CONTENT,
StatusCodeInterface::STATUS_RESET_CONTENT,
StatusCodeInterface::STATUS_NOT_MODIFIED
]);
}

/**
Expand All @@ -352,7 +360,8 @@ public function isEmpty()
*/
public function isInformational()
{
return $this->getStatusCode() >= 100 && $this->getStatusCode() < 200;
return $this->getStatusCode() >= StatusCodeInterface::STATUS_CONTINUE
&& $this->getStatusCode() < StatusCodeInterface::STATUS_OK;
}

/**
Expand All @@ -364,7 +373,7 @@ public function isInformational()
*/
public function isOk()
{
return $this->getStatusCode() === 200;
return $this->getStatusCode() === StatusCodeInterface::STATUS_OK;
}

/**
Expand All @@ -376,7 +385,8 @@ public function isOk()
*/
public function isSuccessful()
{
return $this->getStatusCode() >= 200 && $this->getStatusCode() < 300;
return $this->getStatusCode() >= StatusCodeInterface::STATUS_OK
&& $this->getStatusCode() < StatusCodeInterface::STATUS_MULTIPLE_CHOICES;
}

/**
Expand All @@ -388,7 +398,13 @@ public function isSuccessful()
*/
public function isRedirect()
{
return in_array($this->getStatusCode(), [301, 302, 303, 307, 308]);
return in_array($this->getStatusCode(), [
StatusCodeInterface::STATUS_MOVED_PERMANENTLY,
StatusCodeInterface::STATUS_FOUND,
StatusCodeInterface::STATUS_SEE_OTHER,
StatusCodeInterface::STATUS_TEMPORARY_REDIRECT,
StatusCodeInterface::STATUS_PERMANENT_REDIRECT
]);
}

/**
Expand All @@ -400,7 +416,8 @@ public function isRedirect()
*/
public function isRedirection()
{
return $this->getStatusCode() >= 300 && $this->getStatusCode() < 400;
return $this->getStatusCode() >= StatusCodeInterface::STATUS_MULTIPLE_CHOICES
&& $this->getStatusCode() < StatusCodeInterface::STATUS_BAD_REQUEST;
}

/**
Expand All @@ -413,7 +430,7 @@ public function isRedirection()
*/
public function isForbidden()
{
return $this->getStatusCode() === 403;
return $this->getStatusCode() === StatusCodeInterface::STATUS_FORBIDDEN;
}

/**
Expand All @@ -425,7 +442,7 @@ public function isForbidden()
*/
public function isNotFound()
{
return $this->getStatusCode() === 404;
return $this->getStatusCode() === StatusCodeInterface::STATUS_NOT_FOUND;
}

/**
Expand All @@ -437,7 +454,8 @@ public function isNotFound()
*/
public function isClientError()
{
return $this->getStatusCode() >= 400 && $this->getStatusCode() < 500;
return $this->getStatusCode() >= StatusCodeInterface::STATUS_BAD_REQUEST
&& $this->getStatusCode() < StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR;
}

/**
Expand All @@ -449,7 +467,8 @@ public function isClientError()
*/
public function isServerError()
{
return $this->getStatusCode() >= 500 && $this->getStatusCode() < 600;
return $this->getStatusCode() >= StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR
&& $this->getStatusCode() < 600;
}

/**
Expand Down