Source of file Response.php

Size: 3,982 Bytes - Last Modified: 2015-12-22T09:42:40-05:00

../src/Response.php

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
Covered by 2 test(s):
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
535455565758596061626364
Covered by 5 test(s):
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
  • MvcLite\ResponseTest::testSetHeader with data set "plain content headers"
  • MvcLite\ResponseTest::testSetHeader with data set "csv content headers"
  • MvcLite\ResponseTest::testSetHeader with data set "testing headers"
65
Covered by 5 test(s):
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
  • MvcLite\ResponseTest::testSetHeader with data set "plain content headers"
  • MvcLite\ResponseTest::testSetHeader with data set "csv content headers"
  • MvcLite\ResponseTest::testSetHeader with data set "testing headers"
666768697071727374757677
Covered by 3 test(s):
  • MvcLite\ResponseTest::testGetHeaders
  • MvcLite\ResponseTest::testSetHeaders with data set #0
  • MvcLite\ResponseTest::testSetHeaders with data set #1
78
Covered by 2 test(s):
  • MvcLite\ResponseTest::testSetHeaders with data set #0
  • MvcLite\ResponseTest::testSetHeaders with data set #1
79
Covered by 3 test(s):
  • MvcLite\ResponseTest::testGetHeaders
  • MvcLite\ResponseTest::testSetHeaders with data set #0
  • MvcLite\ResponseTest::testSetHeaders with data set #1
8081
Covered by 3 test(s):
  • MvcLite\ResponseTest::testGetHeaders
  • MvcLite\ResponseTest::testSetHeaders with data set #0
  • MvcLite\ResponseTest::testSetHeaders with data set #1
828384858687888990919293
Covered by 3 test(s):
  • MvcLite\ResponseTest::testSetHeader with data set "plain content headers"
  • MvcLite\ResponseTest::testSetHeader with data set "csv content headers"
  • MvcLite\ResponseTest::testSetHeader with data set "testing headers"
949596979899100101102103
Covered by 3 test(s):
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
  • MvcLite\ResponseTest::testGetHeaders
104105106107108109110111112113114
Covered by 3 test(s):
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
  • MvcLite\ResponseTest::testSendHeaders with data set "simple test"
115
Covered by 3 test(s):
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
  • MvcLite\ResponseTest::testSendHeaders with data set "simple test"
116
Covered by 3 test(s):
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
  • MvcLite\ResponseTest::testSendHeaders with data set "simple test"
117118
Covered by 3 test(s):
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
  • MvcLite\ResponseTest::testSendHeaders with data set "simple test"
119120121122123124125126127128129130
Covered by 3 test(s):
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
  • MvcLite\ResponseTest::testSetBody
131
Covered by 3 test(s):
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
  • MvcLite\ResponseTest::testSetBody
132133134135136137138139140141
Covered by 3 test(s):
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
  • MvcLite\ResponseTest::testGetBody
142143144145146147148149150151152153154155
Covered by 9 test(s):
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
  • MvcLite\ResponseTest::testSetContentType with data set "No content type"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (application/json)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (application/javascript)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/html)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/plain)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/csv)"
  • MvcLite\ResponseTest::testSetContentType with data set "Bad content type, expect exception"
156
Covered by 1 test(s):
  • MvcLite\ResponseTest::testSetContentType with data set "No content type"
157
Covered by 1 test(s):
  • MvcLite\ResponseTest::testSetContentType with data set "No content type"
158159160
Covered by 9 test(s):
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
  • MvcLite\ResponseTest::testSetContentType with data set "No content type"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (application/json)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (application/javascript)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/html)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/plain)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/csv)"
  • MvcLite\ResponseTest::testSetContentType with data set "Bad content type, expect exception"
161
Covered by 9 test(s):
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
  • MvcLite\ResponseTest::testSetContentType with data set "No content type"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (application/json)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (application/javascript)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/html)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/plain)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/csv)"
  • MvcLite\ResponseTest::testSetContentType with data set "Bad content type, expect exception"
162
Covered by 9 test(s):
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
  • MvcLite\ResponseTest::testSetContentType with data set "No content type"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (application/json)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (application/javascript)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/html)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/plain)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/csv)"
  • MvcLite\ResponseTest::testSetContentType with data set "Bad content type, expect exception"
163
Covered by 9 test(s):
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
  • MvcLite\ResponseTest::testSetContentType with data set "No content type"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (application/json)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (application/javascript)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/html)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/plain)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/csv)"
  • MvcLite\ResponseTest::testSetContentType with data set "Bad content type, expect exception"
164
Covered by 9 test(s):
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
  • MvcLite\ResponseTest::testSetContentType with data set "No content type"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (application/json)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (application/javascript)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/html)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/plain)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/csv)"
  • MvcLite\ResponseTest::testSetContentType with data set "Bad content type, expect exception"
165
Covered by 9 test(s):
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
  • MvcLite\ResponseTest::testSetContentType with data set "No content type"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (application/json)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (application/javascript)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/html)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/plain)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/csv)"
  • MvcLite\ResponseTest::testSetContentType with data set "Bad content type, expect exception"
166167
Covered by 9 test(s):
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
  • MvcLite\ResponseTest::testSetContentType with data set "No content type"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (application/json)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (application/javascript)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/html)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/plain)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/csv)"
  • MvcLite\ResponseTest::testSetContentType with data set "Bad content type, expect exception"
168
Covered by 1 test(s):
  • MvcLite\ResponseTest::testSetContentType with data set "Bad content type, expect exception"
169170171
Covered by 8 test(s):
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
  • MvcLite\ResponseTest::testSetContentType with data set "No content type"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (application/json)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (application/javascript)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/html)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/plain)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/csv)"
172173
Covered by 8 test(s):
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
  • MvcLite\ResponseTest::testSetContentType with data set "No content type"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (application/json)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (application/javascript)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/html)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/plain)"
  • MvcLite\ResponseTest::testSetContentType with data set "Good content type (text/csv)"
174175176177178179180181182183
Covered by 2 test(s):
  • MvcLite\ResponseTest::testGetContentType with data set "has header, expect it"
  • MvcLite\ResponseTest::testGetContentType with data set "does not have header, expect default"
184
Covered by 2 test(s):
  • MvcLite\ResponseTest::testGetContentType with data set "has header, expect it"
  • MvcLite\ResponseTest::testGetContentType with data set "does not have header, expect default"
185
Covered by 1 test(s):
  • MvcLite\ResponseTest::testGetContentType with data set "does not have header, expect default"
186
Covered by 1 test(s):
  • MvcLite\ResponseTest::testGetContentType with data set "does not have header, expect default"
187
Covered by 2 test(s):
  • MvcLite\ResponseTest::testGetContentType with data set "has header, expect it"
  • MvcLite\ResponseTest::testGetContentType with data set "does not have header, expect default"
188189190
<?php
/**
 * Base Response
 *
 * @category    PHP
 * @package     MvcLite
 * @subpackage  Response
 * @since       File available since release 1.0.1
 * @author      Cory Collier <corycollier@corycollier.com>
 */

namespace MvcLite;

use MvcLite\Traits\Singleton as SingletonTrait;

/**
 * Base Response
 *
 * @category    PHP
 * @package     MvcLite
 * @subpackage  Response
 * @since       Class available since release 1.0.1
 * @author      Cory Collier <corycollier@corycollier.com>
 */
class Response extends ObjectAbstract
{
    use SingletonTrait;

    const DEFAULT_CONTENT_TYPE = 'application/json';
    const ERR_INVALID_CONTENT_TYPE = 'The given content-type [%s] is not valid';

    /**
     * A list of headers to be output
     *
     * @var array
     */
    protected $headers = [];

    /**
     * The body of the response
     *
     * @var string
     */
    protected $body = '';

    /**
     * method to start the response up
     */
    public function init()
    {

    }

    /**
     * Set's a header value
     *
     * @param string $name
     * @param string $value
     *
     * @return MvcLite\Response $this for object-chaining.
     */
    public function setHeader($name, $value = '')
    {
        $this->headers[$name] = $value;
        return $this;
    }

    /**
     * Set multiple headers at one time
     *
     * @param array $headers
     *
     * @return MvcLite\Response $this for object-chaining.
     */
    public function setHeaders($headers = [])
    {
        foreach ($headers as $name => $value) {
            $this->setHeader($name, $value);
        }

        return $this;
    }

    /**
     * gets the header by name
     *
     * @param string $name
     *
     * @return string
     */
    public function getHeader($name)
    {
        return $this->headers[$name];
    }

    /**
     * Returns all of the headers
     *
     * @return array
     */
    public function getHeaders()
    {
        return $this->headers;
    }

    /**
     * Function to return a formatted header string
     *
     * @return MvcLite\Response $this for object-chaining.
     */
    public function sendHeaders()
    {
        // iterate over the headers, sending them out
        foreach ($this->getHeaders() as $name => $value) {
            header("{$name}: {$value}");
        }

        return $this;
    }

    /**
     * set the body of the response
     *
     * @param string $string
     *
     * @return MvcLite\Response $this for object-chaining.
     */
    public function setBody($string)
    {
        $this->body = (string)$string;
        return $this;
    }

    /**
     * gets the response body
     *
     * @return string
     */
    public function getBody()
    {
        return $this->body;
    }

    /**
     * Setter for the content type.
     *
     * @param string $contentType The value to set the content type to.
     *
     * @return MvcLite\Response Returns $this, for object-chaining.
     *
     * @throws MvcLite\Exception If the content type is invalid, an exception is thrown.
     */
    public function setContentType($contentType = '')
    {
        if (! $contentType) {
            $contentType = self::DEFAULT_CONTENT_TYPE;
        }

        $types = [
            'application/json',
            'application/javascript',
            'text/html',
            'text/plain',
            'text/csv',
        ];

        if (!in_array($contentType, $types)) {
            throw new Exception(sprintf(SELF::ERR_INVALID_CONTENT_TYPE, $contentType));
        }

        $this->setHeader('Content-Type', $contentType);

        return $this;
    }

    /**
     * Getter for the content type.
     *
     * @return string The content type for the response
     */
    public function getContentType()
    {
        $contentType = $this->getHeader('Content-Type');
        if (! $contentType) {
            $contentType = self::DEFAULT_CONTENT_TYPE;
        }
        return $contentType;
    }
}