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;
20 * Allows creating a response by passing an HTML string to the constructor;
21 * by default, sets a status code of 200 and sets the Content-Type header to
24 class HtmlResponse extends Response
26 use InjectContentTypeTrait;
29 * Create an HTML response.
31 * Produces an HTML response with a Content-Type of text/html and a default
34 * @param string|StreamInterface $html HTML 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 $html is neither a string or stream.
39 public function __construct($html, $status = 200, array $headers = [])
42 $this->createBody($html),
44 $this->injectContentType('text/html; charset=utf-8', $headers)
49 * Create the message body.
51 * @param string|StreamInterface $html
52 * @return StreamInterface
53 * @throws InvalidArgumentException if $html is neither a string or stream.
55 private function createBody($html)
57 if ($html instanceof StreamInterface) {
61 if (! is_string($html)) {
62 throw new InvalidArgumentException(sprintf(
63 'Invalid content (%s) provided to %s',
64 (is_object($html) ? get_class($html) : gettype($html)),
69 $body = new Stream('php://temp', 'wb+');