statusCode = $statusCode; if (is_array($headers)) { $this->headers = $headers; } else { $this->headers = array(); } if (!is_null($body)) { $this->body = $body; } } // __construct() // ------------------------------------------------------------------------- // !Accessors public function __get($name) { switch ($name) { case 'body': return $this->getBody(); case 'headers': return $this->getHeaders(); default: throw new \Exception('Property ' . $name . ' does not exist.'); } } // __get() public function __set($name, $value) { switch ($name) { case 'body': $this->setBody($value); break; default: throw new \Exception('Property ' . $name . ' does not exist or is read only.'); } } // __set() public function getBody() { return $this->body; } public function getHeaders() { return $this->headers; } /** * Provide a new entity body for the respone. * This method also updates the content-length header based on the length * of the new body string. * * @param string $value * @param bool $setContentLength Automatically add a Content-length header */ public function setBody($value, $setContentLength=true) { $this->body = $value; if ($setContentLength === true) { $this->setHeader('Content-Length', strlen($value)); } } // setBody() /** * Add or update a header to a given value * * @param string $header * @param string $value */ public function setHeader($header, $value) { $this->headers[$header] = $value; } /** * Return if the response contains a header with the given key. * * @param string $header * @return bool */ public function hasHeader($header) { return isset($this->headers[$header]); } /** * Return the value of a given header, or false if it does not exist. * * @param string $header * @return string|bool */ public function getHeader($header) { if ($this->hasHeader($header)) { return $this->headers[$header]; } return false; } /** * Remove a header. This method does nothing if the header does not exist. * * @param string $header */ public function unsetHeader($header) { if ($this->hasHeader($header)) { unset($this->headers[$header]); } } // ------------------------------------------------------------------------- /** * Output the response to the client. This function also terminates the * script to prevent and additional output from contaminating the response. * * @param bool $headersOnly Do not include the body, only the headers. */ public function respond($headersOnly = false) { // Output the HTTP status code. // TODO: Available in 5.4+. PHP.net has a good alternative in the comments. http_response_code($this->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() } // Response ?>