Made Response->statusCode into a property and added ->reasonPhrase.

PhpDoc updates.

When you set the ->statusCode to a standard HTTP status code, the instance will set the reason phrase appropriately. You may also set statusCode and reasonPhrase separately, or set them at once by passing two parameters to setStatusCode();
This commit is contained in:
PJ Dietz 2012-11-30 16:33:04 -05:00
parent 7ee4ee39b9
commit b3f997136f
3 changed files with 173 additions and 68 deletions

View File

@ -60,6 +60,11 @@ class Handler {
// -------------------------------------------------------------------------
// !Accessors
/**
* @param $name
* @return Response
* @throws \Exception
*/
public function __get($name) {
switch ($name) {

View File

@ -19,6 +19,8 @@ require_once(dirname(__FILE__) . '/exceptions/CurlException.inc.php');
* Second, you can create a custom Request and use it to obtain a Response
* from a server through cURL.
*
* @property string hostname Hostname part of the URI
* @property string method HTTP method (GET, POST, PUT, DELETE, etc.)
* @property string path Path component of the URI for the request
* @property string pathParts Fragments of the path, delimited by slashes
* @property array query Associative array of query parameters

View File

@ -15,27 +15,45 @@ require_once(dirname(__FILE__) . '/Message.inc.php');
******************************************************************************/
/**
* @property string body Entity body of the response
* @property array headers Associative array of headers
* @property string reasonPhrase Text explanation of status code.
* @property int statusCode HTTP status code
* @property string statusLine HTTP status line, e.g. "HTTP/1.1 200 OK"
*/
class Response extends Message {
/**
* The protocol. Set this to the protocol you wish to use, such as
* "HTTP/1.1". If unset, the class uses $_SERVER['SERVER_PROTOCOL']
* "HTTP/1.1". The default is $_SERVER['SERVER_PROTOCOL'] or HTTP/1.1
*
* @var string
*/
public $protocol;
/**
* Text explanation of the HTTP Status Code. You only need to set this if
* you are using nonstandard status codes. Otherwise, the instance will
* set the when you update the status code.
*
* @var string
*/
protected $reasonPhrase;
/**
* HTTP status code
* @var int
*/
public $statusCode;
protected $statusCode;
// -------------------------------------------------------------------------
/**
* Create a new Response instance, optionally passing a status code, body,
* and headers.
*
* @param int $statusCode
* @param string $body
* @param array $headers
*/
public function __construct($statusCode=500, $body=null, $headers=null) {
$this->statusCode = $statusCode;
@ -50,11 +68,57 @@ class Response extends Message {
$this->body = $body;
}
if (isset($_SERVER['SERVER_PROTOCOL'])) {
$this->protocol = $_SERVER['SERVER_PROTOCOL'];
} else {
$this->protocol = 'HTTP/1.1';
}
} // __construct()
// -------------------------------------------------------------------------
// !Accessors
/**
* @param string $name
* @return array|string
* @throws \Exception
*/
public function __get($name) {
switch ($name) {
case 'reasonPhrase':
return $this->getReasonPhrase();
case 'statusCode':
return $this->getStatusCode();
case 'statusLine':
return $this->getStatusLine();
default:
return parent::__get($name);
}
} // __get()
/**
* @param string $name
* @param mixed $value
* @throws \Exception
*/
public function __set($name, $value) {
switch ($name) {
case 'reasonPhrase':
$this->setReasonPhrase($value);
return;
case 'statusCode':
$this->setStatusCode($value);
return;
default:
parent::__set($name, $value);
}
}
/**
* Provide a new entity body for the respone.
* This method also updates the content-length header based on the length
@ -71,39 +135,40 @@ class Response extends Message {
$this->setHeader('Content-Length', strlen($value));
}
}
/**
* @return string
*/
public function getReasonPhrase() {
return $this->reasonPhrase;
}
/**
* @param string $statusCodeMessage
*/
public function setReasonPhrase($statusCodeMessage) {
$this->reasonPhrase = $statusCodeMessage;
}
/**
* @return int
*/
public function getStatusCode() {
return $this->statusCode;
} // setBody()
// -------------------------------------------------------------------------
/**
* Output the response to the client.
*
* @param bool $headersOnly Do not include the body, only the headers.
* @param int $statusCode
* @param string $reasonPhrase
* @throws \InvalidArgumentException
* @return void
*/
public function respond($headersOnly=false) {
public function setStatusCode($statusCode, $reasonPhrase=null) {
// Output the HTTP status code.
header($this->getStatusLine($this->statusCode));
$this->statusCode = (int) $statusCode;
// Output each header.
foreach ($this->headers as $header => $value) {
header($header . ': ' . $value);
}
// Output the entity body.
if (!$headersOnly && isset($this->body)) {
print $this->body;
}
} // respond()
/**
* Return HTTP status line, e.g. HTTP/1.1 200 OK.
*
* @return string
* @throws \UnexpectedValueException
*/
protected function getStatusLine() {
if (is_null($reasonPhrase)) {
switch ($this->statusCode) {
case 100: $text = 'Continue'; break;
@ -143,23 +208,56 @@ class Response extends Message {
case 503: $text = 'Service Unavailable'; break;
case 504: $text = 'Gateway Time-out'; break;
case 505: $text = 'HTTP Version not supported'; break;
default:
throw new \UnexpectedValueException('Unknown http status code "' . $this->statusCode . '"');
break;
default: $text = 'Nonstandard'; break;
}
if (isset($this->protocol)) {
$protocol = $this->protocol;
} elseif (isset($_SERVER['SERVER_PROTOCOL'])) {
$protocol = $_SERVER['SERVER_PROTOCOL'];
$this->reasonPhrase = $text;
} else {
$protocol = 'HTTP/1.1';
if (is_string($reasonPhrase)) {
$this->reasonPhrase = $reasonPhrase;
} else {
throw new \InvalidArgumentException('$reasonPhrase must be a string (or null to use standard HTTP Reason-Phrase');
}
return $protocol . ' ' . $this->statusCode . ' ' . $text;
}
}
/**
* Return HTTP status line, e.g. HTTP/1.1 200 OK.
*
* @return string
*/
protected function getStatusLine() {
return $this->protocol . ' ' . $this->statusCode . ' ' . $this->reasonPhrase;
}
// -------------------------------------------------------------------------
/**
* Output the response to the client.
*
* @param bool $headersOnly Do not include the body, only the headers.
*/
public function respond($headersOnly=false) {
// Output the HTTP status code.
header($this->statusLine);
// Output each header.
foreach ($this->headers as $header => $value) {
header($header . ': ' . $value);
}
// Output the entity body.
if (!$headersOnly && isset($this->body)) {
print $this->body;
}
} // respond()
} // Response
?>