Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * Zend Framework (http://framework.zend.com/)
|
Chris@0
|
4 *
|
Chris@0
|
5 * @see http://github.com/zendframework/zend-diactoros for the canonical source repository
|
Chris@0
|
6 * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
|
Chris@0
|
7 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@0
|
10 namespace Zend\Diactoros\Response;
|
Chris@0
|
11
|
Chris@0
|
12 use InvalidArgumentException;
|
Chris@0
|
13 use Psr\Http\Message\StreamInterface;
|
Chris@0
|
14 use Zend\Diactoros\Response;
|
Chris@0
|
15 use Zend\Diactoros\Stream;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * HTML response.
|
Chris@0
|
19 *
|
Chris@0
|
20 * Allows creating a response by passing an HTML string to the constructor;
|
Chris@0
|
21 * by default, sets a status code of 200 and sets the Content-Type header to
|
Chris@0
|
22 * text/html.
|
Chris@0
|
23 */
|
Chris@0
|
24 class HtmlResponse extends Response
|
Chris@0
|
25 {
|
Chris@0
|
26 use InjectContentTypeTrait;
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * Create an HTML response.
|
Chris@0
|
30 *
|
Chris@0
|
31 * Produces an HTML response with a Content-Type of text/html and a default
|
Chris@0
|
32 * status of 200.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @param string|StreamInterface $html HTML or stream for the message body.
|
Chris@0
|
35 * @param int $status Integer status code for the response; 200 by default.
|
Chris@0
|
36 * @param array $headers Array of headers to use at initialization.
|
Chris@0
|
37 * @throws InvalidArgumentException if $html is neither a string or stream.
|
Chris@0
|
38 */
|
Chris@0
|
39 public function __construct($html, $status = 200, array $headers = [])
|
Chris@0
|
40 {
|
Chris@0
|
41 parent::__construct(
|
Chris@0
|
42 $this->createBody($html),
|
Chris@0
|
43 $status,
|
Chris@0
|
44 $this->injectContentType('text/html; charset=utf-8', $headers)
|
Chris@0
|
45 );
|
Chris@0
|
46 }
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * Create the message body.
|
Chris@0
|
50 *
|
Chris@0
|
51 * @param string|StreamInterface $html
|
Chris@0
|
52 * @return StreamInterface
|
Chris@0
|
53 * @throws InvalidArgumentException if $html is neither a string or stream.
|
Chris@0
|
54 */
|
Chris@0
|
55 private function createBody($html)
|
Chris@0
|
56 {
|
Chris@0
|
57 if ($html instanceof StreamInterface) {
|
Chris@0
|
58 return $html;
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 if (! is_string($html)) {
|
Chris@0
|
62 throw new InvalidArgumentException(sprintf(
|
Chris@0
|
63 'Invalid content (%s) provided to %s',
|
Chris@0
|
64 (is_object($html) ? get_class($html) : gettype($html)),
|
Chris@0
|
65 __CLASS__
|
Chris@0
|
66 ));
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 $body = new Stream('php://temp', 'wb+');
|
Chris@0
|
70 $body->write($html);
|
Chris@0
|
71 $body->rewind();
|
Chris@0
|
72 return $body;
|
Chris@0
|
73 }
|
Chris@0
|
74 }
|