Security update for Core, with self-updated composer
[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 /**
18  * HTML response.
19  *
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
22  * text/html.
23  */
24 class HtmlResponse extends Response
25 {
26     use InjectContentTypeTrait;
27
28     /**
29      * Create an HTML response.
30      *
31      * Produces an HTML response with a Content-Type of text/html and a default
32      * status of 200.
33      *
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.
38      */
39     public function __construct($html, $status = 200, array $headers = [])
40     {
41         parent::__construct(
42             $this->createBody($html),
43             $status,
44             $this->injectContentType('text/html; charset=utf-8', $headers)
45         );
46     }
47
48     /**
49      * Create the message body.
50      *
51      * @param string|StreamInterface $html
52      * @return StreamInterface
53      * @throws InvalidArgumentException if $html is neither a string or stream.
54      */
55     private function createBody($html)
56     {
57         if ($html instanceof StreamInterface) {
58             return $html;
59         }
60
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)),
65                 __CLASS__
66             ));
67         }
68
69         $body = new Stream('php://temp', 'wb+');
70         $body->write($html);
71         $body->rewind();
72         return $body;
73     }
74 }