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,69 +103,66 @@ class Response extends Message implements ResponseInterface
*/ */
private function getDefaultReasonPhraseForStatusCode($statusCode) private function getDefaultReasonPhraseForStatusCode($statusCode)
{ {
static $reasonPhraseLookup = null; $reasonPhraseLookup = [
if ($reasonPhraseLookup === null) { 100 => "Continue",
$reasonPhraseLookup = [ 101 => "Switching Protocols",
100 => "Continue", 102 => "Processing",
101 => "Switching Protocols", 200 => "OK",
102 => "Processing", 201 => "Created",
200 => "OK", 202 => "Accepted",
201 => "Created", 203 => "Non-Authoritative Information",
202 => "Accepted", 204 => "No Content",
203 => "Non-Authoritative Information", 205 => "Reset Content",
204 => "No Content", 206 => "Partial Content",
205 => "Reset Content", 207 => "Multi-Status",
206 => "Partial Content", 208 => "Already Reported",
207 => "Multi-Status", 226 => "IM Used",
208 => "Already Reported", 300 => "Multiple Choices",
226 => "IM Used", 301 => "Moved Permanently",
300 => "Multiple Choices", 302 => "Found",
301 => "Moved Permanently", 303 => "See Other",
302 => "Found", 304 => "Not Modified",
303 => "See Other", 305 => "Use Proxy",
304 => "Not Modified", 307 => "Temporary Redirect",
305 => "Use Proxy", 308 => "Permanent Redirect",
307 => "Temporary Redirect", 400 => "Bad Request",
308 => "Permanent Redirect", 401 => "Unauthorized",
400 => "Bad Request", 402 => "Payment Required",
401 => "Unauthorized", 403 => "Forbidden",
402 => "Payment Required", 404 => "Not Found",
403 => "Forbidden", 405 => "Method Not Allowed",
404 => "Not Found", 406 => "Not Acceptable",
405 => "Method Not Allowed", 407 => "Proxy Authentication Required",
406 => "Not Acceptable", 408 => "Request Timeout",
407 => "Proxy Authentication Required", 409 => "Conflict",
408 => "Request Timeout", 410 => "Gone",
409 => "Conflict", 411 => "Length Required",
410 => "Gone", 412 => "Precondition Failed",
411 => "Length Required", 413 => "Payload Too Large",
412 => "Precondition Failed", 414 => "URI Too Long",
413 => "Payload Too Large", 415 => "Unsupported Media Type",
414 => "URI Too Long", 416 => "Range Not Satisfiable",
415 => "Unsupported Media Type", 417 => "Expectation Failed",
416 => "Range Not Satisfiable", 421 => "Misdirected Request",
417 => "Expectation Failed", 422 => "Unprocessable Entity",
421 => "Misdirected Request", 423 => "Locked",
422 => "Unprocessable Entity", 424 => "Failed Dependency",
423 => "Locked", 426 => "Upgrade Required",
424 => "Failed Dependency", 428 => "Precondition Required",
426 => "Upgrade Required", 429 => "Too Many Requests",
428 => "Precondition Required", 431 => "Request Header Fields Too Large",
429 => "Too Many Requests", 500 => "Internal Server Error",
431 => "Request Header Fields Too Large", 501 => "Not Implemented",
500 => "Internal Server Error", 502 => "Bad Gateway",
501 => "Not Implemented", 503 => "Service Unavailable",
502 => "Bad Gateway", 504 => "Gateway Timeout",
503 => "Service Unavailable", 505 => "HTTP Version Not Supported",
504 => "Gateway Timeout", 506 => "Variant Also Negotiates",
505 => "HTTP Version Not Supported", 507 => "Insufficient Storage",
506 => "Variant Also Negotiates", 508 => "Loop Detected",
507 => "Insufficient Storage", 510 => "Not Extended",
508 => "Loop Detected", 511 => "Network Authentication Required"
510 => "Not Extended", ];
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()
{ {
rewind($this->resource); $string = "";
return $this->getContents(); try {
rewind($this->resource);
$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"]
]; ];
} }