<?php
/**
* @package phprouter
*/
namespace phprouter;
/**
* Class used to send HTTP response to client
* @package phprouter
* @see Http
* @see Router
* @see Request
*/
abstract class Response
{
/**
* HTTP status code to return to client
* @var integer
* @access private
* @see status()
*/
static $status_code = 200;
/**
* Send a raw HTTP header
* @param string $name Header name
* @param string $value Header value
* @param boolean $replace Whether the header should replace a
* previous similar header, or add a second header of the same type
*/
static function header( $name, $value, $replace = true )
{
header( $name . ': ' . $value, (bool) $replace );
}
/**
* Set response status code
* @param integer $status_code
* @throws InvalidArgumentException on invalid HTTP status code
* @see Http::$status_codes
*/
static function status( $status_code )
{
if ( !isset( Http::$status_codes[ $status_code ] ) )
{
throw new \InvalidArgumentException( 'Invalid HTTP status code' );
}
$http_reason_phrase = Http::$status_codes[ $status_code ];
if ( PHP_SAPI == 'cgi_fcgi' )
{
$header = 'Status: ' . $http_reason_phrase;
}
else
{
$header = Request::server_protocol() . ' ' . $http_reason_phrase;
}
header( $header, true, $status_code );
self::$status_code = $status_code;
}
}
/* EOF */