Changes to match updates to PSR-7 in progress

This commit is contained in:
PJ Dietz 2015-04-29 18:32:13 -04:00
parent 2e7783d19d
commit 086b09db4f
3 changed files with 72 additions and 69 deletions

View File

@ -62,17 +62,17 @@ class Response extends Message implements ResponseInterface
* @link http://tools.ietf.org/html/rfc7231#section-6 * @link http://tools.ietf.org/html/rfc7231#section-6
* @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
* @param integer $code The 3-digit integer result code to set. * @param integer $code The 3-digit integer result code to set.
* @param null|string $reasonPhrase The reason phrase to use with the * @param string $reasonPhrase The reason phrase to use with the
* provided status code; if none is provided, implementations MAY * provided status code; if none is provided, implementations MAY
* use the defaults as suggested in the HTTP specification. * use the defaults as suggested in the HTTP specification.
* @return self * @return self
* @throws \InvalidArgumentException For invalid status code arguments. * @throws \InvalidArgumentException For invalid status code arguments.
*/ */
public function withStatus($code, $reasonPhrase = null) public function withStatus($code, $reasonPhrase = "")
{ {
$response = clone $this; $response = clone $this;
$response->statusCode = $code; $response->statusCode = $code;
if ($reasonPhrase === null) { if (!$reasonPhrase) {
$reasonPhrase = $this->getDefaultReasonPhraseForStatusCode($code); $reasonPhrase = $this->getDefaultReasonPhraseForStatusCode($code);
} }
$response->reasonPhrase = $reasonPhrase; $response->reasonPhrase = $reasonPhrase;
@ -103,8 +103,6 @@ class Response extends Message implements ResponseInterface
*/ */
private function getDefaultReasonPhraseForStatusCode($statusCode) private function getDefaultReasonPhraseForStatusCode($statusCode)
{ {
static $reasonPhraseLookup = null;
if ($reasonPhraseLookup === null) {
$reasonPhraseLookup = [ $reasonPhraseLookup = [
100 => "Continue", 100 => "Continue",
101 => "Switching Protocols", 101 => "Switching Protocols",
@ -165,7 +163,6 @@ class Response extends Message implements ResponseInterface
510 => "Not Extended", 510 => "Not Extended",
511 => "Network Authentication Required" 511 => "Network Authentication Required"
]; ];
}
if (isset($reasonPhraseLookup[$statusCode])) { if (isset($reasonPhraseLookup[$statusCode])) {
return $reasonPhraseLookup[$statusCode]; return $reasonPhraseLookup[$statusCode];
} }

View File

@ -49,8 +49,14 @@ class Stream implements StreamInterface
*/ */
public function __toString() public function __toString()
{ {
$string = "";
try {
rewind($this->resource); rewind($this->resource);
return $this->getContents(); $string = $this->getContents();
} catch (Exception $e) {
// Silence exceptions in order to conform with PHP's string casting operations.
}
return $string;
} }
/** /**

View File

@ -76,7 +76,7 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
[503, null, "Service Unavailable"], [503, null, "Service Unavailable"],
[504, null, "Gateway Timeout"], [504, null, "Gateway Timeout"],
[505, null, "HTTP Version Not Supported"], [505, null, "HTTP Version Not Supported"],
[598, null, null], [598, null, ""],
[599, "Nonstandard", "Nonstandard"] [599, "Nonstandard", "Nonstandard"]
]; ];
} }