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):
535455565758596061626364
Covered by 5 test(s):
65
Covered by 5 test(s):
666768697071727374757677
Covered by 3 test(s):
78
Covered by 2 test(s):
79
Covered by 3 test(s):
8081
Covered by 3 test(s):
828384858687888990919293
Covered by 3 test(s):
949596979899100101102103
Covered by 3 test(s):
104105106107108109110111112113114
Covered by 3 test(s):
115
Covered by 3 test(s):
116
Covered by 3 test(s):
117118
Covered by 3 test(s):
119120121122123124125126127128129130
Covered by 3 test(s):
131
Covered by 3 test(s):
132133134135136137138139140141
Covered by 3 test(s):
142143144145146147148149150151152153154155
Covered by 9 test(s):
156
Covered by 1 test(s):
157
Covered by 1 test(s):
158159160
Covered by 9 test(s):
161
Covered by 9 test(s):
162
Covered by 9 test(s):
163
Covered by 9 test(s):
164
Covered by 9 test(s):
165
Covered by 9 test(s):
166167
Covered by 9 test(s):
168
Covered by 1 test(s):
169170171
Covered by 8 test(s):
172173
Covered by 8 test(s):
174175176177178179180181182183
Covered by 2 test(s):
184
Covered by 2 test(s):
185
Covered by 1 test(s):
186
Covered by 1 test(s):
187
Covered by 2 test(s):
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; } } |