<?php
namespace Qii\Response;

class Http extends \Qii\Base\Response
{
    protected $_sendheader = true;
    protected $_responseCode = 200;

    /**
     * Set HTTP response code to use with headers
     *
     * @param int $code
     * @return Qii_Response_Http
     */
    public function setResponseCode($code)
    {
        if (!is_int($code) || (100 > $code) || (599 < $code)) {
            throw new \Qii\Exceptions\Response('Invalid HTTP response code');
        }

        $this->_responseCode = $code;
        return $this;
    }

    /**
     * Retrieve HTTP response code
     *
     * @return int
     */
    public function getResponseCode()
    {
        return $this->_responseCode;
    }

    /**
     * Send all headers
     *
     * Sends any headers specified.
     * If an {@link setResponseCode() HTTP response code}
     * has been specified, it is sent with the first header.
     *
     * @return Qii_Response_Http
     */
    protected function sendHeaders()
    {
        $httpCodeSent = false;

        foreach ($this->_headers as $header) {
            if (!$httpCodeSent && $this->_responseCode) {
                header(
                    $header['name'] . ': ' . $header['value'],
                    $header['replace'], $this->_responseCode
                );
                $httpCodeSent = true;
            } else {
                header(
                    $header['name'] . ': ' . $header['value'],
                    $header['replace']
                );
            }
        }
        return $this;
    }

    /**
     * Set redirect URL
     *
     * Sets Location header. Forces replacement of any prior redirects.
     *
     * @param string $url
     * @return Qii_Response_Abstract
     */
    public function setRedirect($url)
    {
        $this->setHeader('Location', $url, true)
            ->setResponseCode(302);
        return $this;
    }
}