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;
17 use function get_class;
19 use function is_object;
20 use function is_string;
26 * Allows creating a response by passing an HTML string to the constructor;
27 * by default, sets a status code of 200 and sets the Content-Type header to
30 class HtmlResponse extends Response
32 use InjectContentTypeTrait;
35 * Create an HTML response.
37 * Produces an HTML response with a Content-Type of text/html and a default
40 * @param string|StreamInterface $html HTML or stream for the message body.
41 * @param int $status Integer status code for the response; 200 by default.
42 * @param array $headers Array of headers to use at initialization.
43 * @throws InvalidArgumentException if $html is neither a string or stream.
45 public function __construct($html, $status = 200, array $headers = [])
48 $this->createBody($html),
50 $this->injectContentType('text/html; charset=utf-8', $headers)
55 * Create the message body.
57 * @param string|StreamInterface $html
58 * @return StreamInterface
59 * @throws InvalidArgumentException if $html is neither a string or stream.
61 private function createBody($html)
63 if ($html instanceof StreamInterface) {
67 if (! is_string($html)) {
68 throw new InvalidArgumentException(sprintf(
69 'Invalid content (%s) provided to %s',
70 (is_object($html) ? get_class($html) : gettype($html)),
75 $body = new Stream('php://temp', 'wb+');