Source

Ray / Ray / Response.php

Full commit
<?php

class Ray_Response{

    const OK = '200 OK';
    const CREATED = '201 Created';
    const ACCEPTED = '202 Accepted';
    const NON_AUTH_INFO = '203 Non-Authoritative Information';
    const NO_CONTENT = '204 No Content';
    const RESET_CONTENT = '205 Reset Content';
    const PARTIAL_CONTENT = '206 Partial Content';

    const MULTIPLE_CHOICES = '300 Multiple Choices';
    const MOVED_PERMANENTLY = '301 Moved Permanently';
    const FOUND = '302 Found';
    const SEE_OTHER = '303 See Other';
    const NOT_MODIFIED = '304 Not Modified';
    const USE_PROXY = '305 Use Proxy';
    const UNUSED = '306 (Unused)';
    const TEMPORARY_REDIRECT = '307 Temporary Redirect';

    const BAD_REQUEST = '400 Bad Request';
    const UNAUTHORIZED = '401 Unauthorized';
    const FORBIDDEN = '403 Forbidden';
    const NOT_FOUND = '404 Not Found';
    const METHOD_NOT_ALLOWED = '405 Method Not Allowed';
    const NOT_ACCEPTABLE = '406 Not Acceptable';
    const REQUEST_TIMEOUT = '408 Request Timeout';
    const CONFLICT = '409 Conflict';
    const GONE = '410 Gone';
    const LENGTH_REQUIRED = '411 Length Required';
    const UNSUPPORTED_MEDIA_TYPE = '415 Unsupported Media Type';

    const INTERNAL_ERROR = '500 Internal Server Error';
    const NOT_IMPLEMENTED = '501 Not Implemented';
    const BAD_GATEWAY = '502 Bad Gateway';
    const SERVICE_UNAVAILABLE = '503 Service Unavailable';
    const GATEWAY_TIMEOUT = '504 Gateway Timeout';
    const HTTP_VERSION_NOT_SUPPORTED = '505 HTTP Version Not Supported';
    
    private $headers;
    
    private $responseCode;
    
    public function __construct(){
        $this->responseCode = self::OK;
        $this->headers['Content-Type'] = 'text/html';
    }
    
    public function contentType($content){
        $this->headers['Content-Type'] = 'text/html';
        return $this;
    }
    
    public function responseCode($code = self::OK){
        $this->responseCode = $code;
        return $this;
    }
    
    public function header($key, $value=''){
        $this->headers[$key] = $value;
        return $this;
    }
    
    public function send( $send=null ){
        if( ! headers_sent() ){
            
            if ( substr(PHP_SAPI, 0, 3) === 'cgi') {
                $header = 'Status: %s';
            }else{
                $header = 'HTTP/1.1 %s';
            }
            header(sprintf($header, $this->responseCode));
            
            foreach($this->headers as $key=>$value){
                header($key . ': ' . $value);
            }
            
            if($send){
                echo $send;
            }
            
            flush();
        }
    }
    
}