Skip to content

Commit

Permalink
Response::setCode() added $reason
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 20, 2016
1 parent 0221544 commit b9f06ea
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,36 @@ public function __construct()
/**
* Sets HTTP response code.
* @param int
* @param string
* @return static
* @throws Nette\InvalidArgumentException if code is invalid
* @throws Nette\InvalidStateException if HTTP headers have been sent
*/
public function setCode($code)
public function setCode($code, $reason = NULL)
{
$code = (int) $code;
if ($code < 100 || $code > 599) {
throw new Nette\InvalidArgumentException("Bad HTTP response '$code'.");
}
self::checkHeaders();
$this->code = $code;
http_response_code($code);

static $hasReason = [ // hardcoded in PHP
100, 101,
200, 201, 202, 203, 204, 205, 206,
300, 301, 302, 303, 304, 305, 307, 308,
400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 426, 428, 429, 431,
500, 501, 502, 503, 504, 505, 506, 511,
];
if (!$reason && !in_array($code, $hasReason, TRUE)) {
$reason = 'Unknown status';
}
if ($reason) {
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
header("$protocol $code $reason");
} else {
http_response_code($code);
}
return $this;
}

Expand Down

0 comments on commit b9f06ea

Please sign in to comment.