Add parameters to Response::__construct
This commit is contained in:
parent
1b0fccfe0e
commit
4d5430e589
|
|
@ -3,6 +3,7 @@
|
||||||
namespace WellRESTed\Message;
|
namespace WellRESTed\Message;
|
||||||
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Http\Message\StreamInterface;
|
||||||
|
|
||||||
class Response extends Message implements ResponseInterface
|
class Response extends Message implements ResponseInterface
|
||||||
{
|
{
|
||||||
|
|
@ -18,6 +19,30 @@ class Response extends Message implements ResponseInterface
|
||||||
private $statusCode;
|
private $statusCode;
|
||||||
/** @var string HTTP protocol and version */
|
/** @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
|
// Psr\Http\Message\ResponseInterface
|
||||||
|
|
||||||
|
|
@ -60,6 +85,36 @@ class Response extends Message implements ResponseInterface
|
||||||
$response = clone $this;
|
$response = clone $this;
|
||||||
$response->statusCode = $code;
|
$response->statusCode = $code;
|
||||||
if ($reasonPhrase === null) {
|
if ($reasonPhrase === null) {
|
||||||
|
$reasonPhrase = $this->getDefaultReasonPhraseForStatusCode($code);
|
||||||
|
}
|
||||||
|
$response->reasonPhrase = $reasonPhrase;
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the response Reason-Phrase, a short textual description of the Status-Code.
|
||||||
|
*
|
||||||
|
* Because a Reason-Phrase is not a required element in a response
|
||||||
|
* Status-Line, the Reason-Phrase value MAY be null. Implementations MAY
|
||||||
|
* choose to return the default RFC 7231 recommended reason phrase (or those
|
||||||
|
* listed in the IANA HTTP Status Code Registry) for the response's
|
||||||
|
* Status-Code.
|
||||||
|
*
|
||||||
|
* @link http://tools.ietf.org/html/rfc7231#section-6
|
||||||
|
* @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
|
||||||
|
* @return string|null Reason phrase, or null if unknown.
|
||||||
|
*/
|
||||||
|
public function getReasonPhrase()
|
||||||
|
{
|
||||||
|
return $this->reasonPhrase;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $statusCode
|
||||||
|
* @return string|null
|
||||||
|
*/
|
||||||
|
private function getDefaultReasonPhraseForStatusCode($statusCode)
|
||||||
|
{
|
||||||
static $reasonPhraseLookup = null;
|
static $reasonPhraseLookup = null;
|
||||||
if ($reasonPhraseLookup === null) {
|
if ($reasonPhraseLookup === null) {
|
||||||
$reasonPhraseLookup = [
|
$reasonPhraseLookup = [
|
||||||
|
|
@ -123,29 +178,9 @@ class Response extends Message implements ResponseInterface
|
||||||
511 => "Network Authentication Required"
|
511 => "Network Authentication Required"
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
if (isset($reasonPhraseLookup[$code])) {
|
if (isset($reasonPhraseLookup[$statusCode])) {
|
||||||
$reasonPhrase = $reasonPhraseLookup[$code];
|
return $reasonPhraseLookup[$statusCode];
|
||||||
}
|
}
|
||||||
}
|
return null;
|
||||||
$response->reasonPhrase = $reasonPhrase;
|
|
||||||
return $response;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the response Reason-Phrase, a short textual description of the Status-Code.
|
|
||||||
*
|
|
||||||
* Because a Reason-Phrase is not a required element in a response
|
|
||||||
* Status-Line, the Reason-Phrase value MAY be null. Implementations MAY
|
|
||||||
* choose to return the default RFC 7231 recommended reason phrase (or those
|
|
||||||
* listed in the IANA HTTP Status Code Registry) for the response's
|
|
||||||
* Status-Code.
|
|
||||||
*
|
|
||||||
* @link http://tools.ietf.org/html/rfc7231#section-6
|
|
||||||
* @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
|
|
||||||
* @return string|null Reason phrase, or null if unknown.
|
|
||||||
*/
|
|
||||||
public function getReasonPhrase()
|
|
||||||
{
|
|
||||||
return $this->reasonPhrase;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
|
||||||
/**
|
/**
|
||||||
* @covers WellRESTed\Message\Response::withStatus
|
* @covers WellRESTed\Message\Response::withStatus
|
||||||
* @covers WellRESTed\Message\Response::getStatusCode
|
* @covers WellRESTed\Message\Response::getStatusCode
|
||||||
|
* @covers WellRESTed\Message\Response::getDefaultReasonPhraseForStatusCode
|
||||||
*/
|
*/
|
||||||
public function testCreatesNewInstanceWithStatusCode()
|
public function testCreatesNewInstanceWithStatusCode()
|
||||||
{
|
{
|
||||||
|
|
@ -25,6 +26,7 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
|
||||||
/**
|
/**
|
||||||
* @covers WellRESTed\Message\Response::withStatus
|
* @covers WellRESTed\Message\Response::withStatus
|
||||||
* @covers WellRESTed\Message\Response::getReasonPhrase
|
* @covers WellRESTed\Message\Response::getReasonPhrase
|
||||||
|
* @covers WellRESTed\Message\Response::getDefaultReasonPhraseForStatusCode
|
||||||
* @dataProvider statusProvider
|
* @dataProvider statusProvider
|
||||||
*/
|
*/
|
||||||
public function testCreatesNewInstanceWithReasonPhrase($code, $reasonPhrase, $expected)
|
public function testCreatesNewInstanceWithReasonPhrase($code, $reasonPhrase, $expected)
|
||||||
|
|
@ -98,4 +100,37 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->assertEquals(404, $response2->getStatusCode());
|
$this->assertEquals(404, $response2->getStatusCode());
|
||||||
$this->assertEquals(["text/plain"], $response2->getHeader("Content-type"));
|
$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