3 * Zend Framework (http://framework.zend.com/)
5 * @see http://github.com/zendframework/zend-diactoros for the canonical source repository
6 * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
10 namespace Zend\Diactoros\Response;
12 use InvalidArgumentException;
13 use Psr\Http\Message\StreamInterface;
14 use Zend\Diactoros\Response;
15 use Zend\Diactoros\Stream;
18 * Plain text response.
20 * Allows creating a response by passing a string to the constructor;
21 * by default, sets a status code of 200 and sets the Content-Type header to
24 class TextResponse extends Response
26 use InjectContentTypeTrait;
29 * Create a plain text response.
31 * Produces a text response with a Content-Type of text/plain and a default
34 * @param string|StreamInterface $text String or stream for the message body.
35 * @param int $status Integer status code for the response; 200 by default.
36 * @param array $headers Array of headers to use at initialization.
37 * @throws InvalidArgumentException if $text is neither a string or stream.
39 public function __construct($text, $status = 200, array $headers = [])
42 $this->createBody($text),
44 $this->injectContentType('text/plain; charset=utf-8', $headers)
49 * Create the message body.
51 * @param string|StreamInterface $text
52 * @return StreamInterface
53 * @throws InvalidArgumentException if $html is neither a string or stream.
55 private function createBody($text)
57 if ($text instanceof StreamInterface) {
61 if (! is_string($text)) {
62 throw new InvalidArgumentException(sprintf(
63 'Invalid content (%s) provided to %s',
64 (is_object($text) ? get_class($text) : gettype($text)),
69 $body = new Stream('php://temp', 'wb+');