ad5ec8e19e0a420aff410bfa0796fbea4212c3be
[yaffs-website] / vendor / zendframework / zend-diactoros / src / Response / HtmlResponse.php
1 <?php
2 /**
3  * Zend Framework (http://framework.zend.com/)
4  *
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
8  */
9
10 namespace Zend\Diactoros\Response;
11
12 use InvalidArgumentException;
13 use Psr\Http\Message\StreamInterface;
14 use Zend\Diactoros\Response;
15 use Zend\Diactoros\Stream;
16
17 use function get_class;
18 use function gettype;
19 use function is_object;
20 use function is_string;
21 use function sprintf;
22
23 /**
24  * HTML response.
25  *
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
28  * text/html.
29  */
30 class HtmlResponse extends Response
31 {
32     use InjectContentTypeTrait;
33
34     /**
35      * Create an HTML response.
36      *
37      * Produces an HTML response with a Content-Type of text/html and a default
38      * status of 200.
39      *
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.
44      */
45     public function __construct($html, $status = 200, array $headers = [])
46     {
47         parent::__construct(
48             $this->createBody($html),
49             $status,
50             $this->injectContentType('text/html; charset=utf-8', $headers)
51         );
52     }
53
54     /**
55      * Create the message body.
56      *
57      * @param string|StreamInterface $html
58      * @return StreamInterface
59      * @throws InvalidArgumentException if $html is neither a string or stream.
60      */
61     private function createBody($html)
62     {
63         if ($html instanceof StreamInterface) {
64             return $html;
65         }
66
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)),
71                 __CLASS__
72             ));
73         }
74
75         $body = new Stream('php://temp', 'wb+');
76         $body->write($html);
77         $body->rewind();
78         return $body;
79     }
80 }