Add parameters to Response::__construct
This commit is contained in:
parent
1b0fccfe0e
commit
4d5430e589
|
|
@ -3,6 +3,7 @@
|
|||
namespace WellRESTed\Message;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
|
||||
class Response extends Message implements ResponseInterface
|
||||
{
|
||||
|
|
@ -18,6 +19,30 @@ class Response extends Message implements ResponseInterface
|
|||
private $statusCode;
|
||||
/** @var string HTTP protocol and version */
|
||||
|
||||
/**
|
||||
* @param int $statusCode
|
||||
* @param array $headers
|
||||
* @param StreamInterface $body
|
||||
*/
|
||||
public function __construct($statusCode = 500, array $headers = null, StreamInterface $body = null)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->statusCode = $statusCode;
|
||||
$this->reasonPhrase = $this->getDefaultReasonPhraseForStatusCode($statusCode);
|
||||
|
||||
if ($headers) {
|
||||
foreach ($headers as $name => $values) {
|
||||
foreach ($values as $value) {
|
||||
$this->headers[$name] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($body !== null) {
|
||||
$this->body = $body;
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// Psr\Http\Message\ResponseInterface
|
||||
|
||||
|
|
@ -60,72 +85,7 @@ class Response extends Message implements ResponseInterface
|
|||
$response = clone $this;
|
||||
$response->statusCode = $code;
|
||||
if ($reasonPhrase === null) {
|
||||
static $reasonPhraseLookup = null;
|
||||
if ($reasonPhraseLookup === null) {
|
||||
$reasonPhraseLookup = [
|
||||
100 => "Continue",
|
||||
101 => "Switching Protocols",
|
||||
102 => "Processing",
|
||||
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",
|
||||
300 => "Multiple Choices",
|
||||
301 => "Moved Permanently",
|
||||
302 => "Found",
|
||||
303 => "See Other",
|
||||
304 => "Not Modified",
|
||||
305 => "Use Proxy",
|
||||
307 => "Temporary Redirect",
|
||||
308 => "Permanent Redirect",
|
||||
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 => "Payload Too Large",
|
||||
414 => "URI Too Long",
|
||||
415 => "Unsupported Media Type",
|
||||
416 => "Range Not Satisfiable",
|
||||
417 => "Expectation Failed",
|
||||
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",
|
||||
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"
|
||||
];
|
||||
}
|
||||
if (isset($reasonPhraseLookup[$code])) {
|
||||
$reasonPhrase = $reasonPhraseLookup[$code];
|
||||
}
|
||||
$reasonPhrase = $this->getDefaultReasonPhraseForStatusCode($code);
|
||||
}
|
||||
$response->reasonPhrase = $reasonPhrase;
|
||||
return $response;
|
||||
|
|
@ -148,4 +108,79 @@ class Response extends Message implements ResponseInterface
|
|||
{
|
||||
return $this->reasonPhrase;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $statusCode
|
||||
* @return string|null
|
||||
*/
|
||||
private function getDefaultReasonPhraseForStatusCode($statusCode)
|
||||
{
|
||||
static $reasonPhraseLookup = null;
|
||||
if ($reasonPhraseLookup === null) {
|
||||
$reasonPhraseLookup = [
|
||||
100 => "Continue",
|
||||
101 => "Switching Protocols",
|
||||
102 => "Processing",
|
||||
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",
|
||||
300 => "Multiple Choices",
|
||||
301 => "Moved Permanently",
|
||||
302 => "Found",
|
||||
303 => "See Other",
|
||||
304 => "Not Modified",
|
||||
305 => "Use Proxy",
|
||||
307 => "Temporary Redirect",
|
||||
308 => "Permanent Redirect",
|
||||
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 => "Payload Too Large",
|
||||
414 => "URI Too Long",
|
||||
415 => "Unsupported Media Type",
|
||||
416 => "Range Not Satisfiable",
|
||||
417 => "Expectation Failed",
|
||||
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",
|
||||
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"
|
||||
];
|
||||
}
|
||||
if (isset($reasonPhraseLookup[$statusCode])) {
|
||||
return $reasonPhraseLookup[$statusCode];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
|
|||
/**
|
||||
* @covers WellRESTed\Message\Response::withStatus
|
||||
* @covers WellRESTed\Message\Response::getStatusCode
|
||||
* @covers WellRESTed\Message\Response::getDefaultReasonPhraseForStatusCode
|
||||
*/
|
||||
public function testCreatesNewInstanceWithStatusCode()
|
||||
{
|
||||
|
|
@ -25,6 +26,7 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
|
|||
/**
|
||||
* @covers WellRESTed\Message\Response::withStatus
|
||||
* @covers WellRESTed\Message\Response::getReasonPhrase
|
||||
* @covers WellRESTed\Message\Response::getDefaultReasonPhraseForStatusCode
|
||||
* @dataProvider statusProvider
|
||||
*/
|
||||
public function testCreatesNewInstanceWithReasonPhrase($code, $reasonPhrase, $expected)
|
||||
|
|
@ -98,4 +100,37 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertEquals(404, $response2->getStatusCode());
|
||||
$this->assertEquals(["text/plain"], $response2->getHeader("Content-type"));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// Construction
|
||||
|
||||
/**
|
||||
* @covers WellRESTed\Message\Response::__construct
|
||||
*/
|
||||
public function testSetsStatusCodeOnConstruction()
|
||||
{
|
||||
$response = new Response(200);
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers WellRESTed\Message\Response::__construct
|
||||
*/
|
||||
public function testSetsHeadersOnConstruction()
|
||||
{
|
||||
$response = new Response(200, [
|
||||
"X-foo" => ["bar","baz"]
|
||||
]);
|
||||
$this->assertEquals(["bar","baz"], $response->getHeader("X-foo"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers WellRESTed\Message\Response::__construct
|
||||
*/
|
||||
public function testSetsBodyOnConstruction()
|
||||
{
|
||||
$body = $this->prophesize('\Psr\Http\Message\StreamInterface');
|
||||
$response = new Response(200, [], $body->reveal());
|
||||
$this->assertSame($body->reveal(), $response->getBody());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue