gear/Network/Response.php
<?php
/**
* This file is part of Gear.
*
* @copyright 2015 Loïc Marchand
* @license http://www.spdx.org/licenses/MIT MIT License
*/
namespace Gear\Network;
use \InvalidArgumentException;
/**
* Represent response send to the user.
*/
class Response
{
/**
* HTTP status.
*
* @var int $status
*/
protected $status = 200;
/**
* HTTP headers.
*
* @var array $headers
*/
protected $headers = array();
/**
* Content type.
*
* @var string $contentType
*/
protected $contentType = 'text/html';
/**
* HTTP response body.
*
* @var string $body
*/
protected $body;
/**
* @var array HTTP status codes
*/
public static $codes = [
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
207 => 'Multi-Status',
208 => 'Already Reported',
226 => 'IM Used',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
306 => '(Unused)',
307 => 'Temporary Redirect',
308 => 'Permanent Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Payload Too Large',
414 => 'URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Range Not Satisfiable',
417 => 'Expectation Failed',
422 => 'Unprocessable Entity',
423 => 'Locked',
424 => 'Failed Dependency',
426 => 'Upgrade Required',
428 => 'Precondition Required',
429 => 'Too Many Requests',
431 => 'Request Header Fields Too Large',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
506 => 'Variant Also Negotiates',
507 => 'Insufficient Storage',
508 => 'Loop Detected',
510 => 'Not Extended',
511 => 'Network Authentication Required'
];
/**
* Sets the HTTP status of the response.
*
* @param int $code HTTP status code.
*
* @return object Self reference
* @throws \InvalidArgumentException If invalid status code
*/
public function status($code = null)
{
if ($code === null) {
return $this;
}
if (array_key_exists($code, self::$codes)) {
$this->status = $code;
} else {
throw new InvalidArgumentException('Invalid status code.');
}
return $this;
}
/**
* Adds a header to the response.
*
* @param string|array $name Header name or array of names and values
* @param string $value Header value
*
* @return object Self reference
*/
public function header($name, $value = null)
{
if (is_array($name)) {
foreach ($name as $k => $v) {
$this->headers[$k] = $v;
}
} else {
$this->headers[$name] = $value;
}
return $this;
}
/**
* Returns the headers from the response
*
* @return array
*/
public function headers()
{
return $this->headers;
}
/**
* Set response body.
*
* @param string $content
*
* @return void
*/
public function setContent($content)
{
$this->body = $content;
}
/**
* Append content to the response body.
*
* @param string $str Response content.
*
* @return object Self reference.
*/
public function append($str)
{
$this->body .= $str;
return $this;
}
/**
* Clears the response.
*
* @return object Self reference
*/
public function clear()
{
$this->status = 200;
$this->headers = [];
$this->body = '';
return $this;
}
/**
* Sets caching headers for the response.
*
* @param int|string $expires Expiration time
* @return object Self reference
*/
public function cache($expires)
{
if ($expires === false) {
$this->headers['Expires'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
$this->headers['Cache-Control'] = [
'no-store, no-cache, must-revalidate',
'post-check=0, pre-check=0',
'max-age=0'
];
$this->headers['Pragma'] = 'no-cache';
} else {
$expires = is_int($expires) ? $expires : strtotime($expires);
$this->headers['Expires'] = gmdate('D, d M Y H:i:s', $expires) . ' GMT';
$this->headers['Cache-Control'] = 'max-age='.($expires - time());
if (isset($this->headers['Pragma']) && $this->headers['Pragma'] == 'no-cache') {
unset($this->headers['Pragma']);
}
}
return $this;
}
/**
* Sends HTTP headers.
*
* @return object Self reference
*/
public function sendHeaders()
{
if (isset($_SERVER['SERVER_PROTOCOL'])) {
$header = $_SERVER['SERVER_PROTOCOL'];
} else {
$header = 'HTTP/1.1';
}
$header .= ' ' . $this->status . ' ' . self::$codes[$this->status];
header($header, true, $this->status);
header('Content-type: '.$this->contentType);
// Send other headers
foreach ($this->headers as $field => $value) {
if (is_array($value)) {
foreach ($value as $v) {
header($field.': '.$v, false);
}
} else {
header($field.': '.$value);
}
}
// Send content length
if (($length = strlen($this->body)) > 0) {
header('Content-Length: '.$length);
}
return $this;
}
/**
* Sends a HTTP response.
*
* @return void
*/
public function send()
{
if (ob_get_length() > 0) {
ob_end_clean();
}
if (!headers_sent()) {
$this->sendHeaders();
}
echo $this->body;
}
}