annotate vendor/zendframework/zend-diactoros/src/Response/HtmlResponse.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents c2387f117808
children
rev   line source
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@16 17 use function get_class;
Chris@16 18 use function gettype;
Chris@16 19 use function is_object;
Chris@16 20 use function is_string;
Chris@16 21 use function sprintf;
Chris@16 22
Chris@0 23 /**
Chris@0 24 * HTML response.
Chris@0 25 *
Chris@0 26 * Allows creating a response by passing an HTML string to the constructor;
Chris@0 27 * by default, sets a status code of 200 and sets the Content-Type header to
Chris@0 28 * text/html.
Chris@0 29 */
Chris@0 30 class HtmlResponse extends Response
Chris@0 31 {
Chris@0 32 use InjectContentTypeTrait;
Chris@0 33
Chris@0 34 /**
Chris@0 35 * Create an HTML response.
Chris@0 36 *
Chris@0 37 * Produces an HTML response with a Content-Type of text/html and a default
Chris@0 38 * status of 200.
Chris@0 39 *
Chris@0 40 * @param string|StreamInterface $html HTML or stream for the message body.
Chris@0 41 * @param int $status Integer status code for the response; 200 by default.
Chris@0 42 * @param array $headers Array of headers to use at initialization.
Chris@0 43 * @throws InvalidArgumentException if $html is neither a string or stream.
Chris@0 44 */
Chris@0 45 public function __construct($html, $status = 200, array $headers = [])
Chris@0 46 {
Chris@0 47 parent::__construct(
Chris@0 48 $this->createBody($html),
Chris@0 49 $status,
Chris@0 50 $this->injectContentType('text/html; charset=utf-8', $headers)
Chris@0 51 );
Chris@0 52 }
Chris@0 53
Chris@0 54 /**
Chris@0 55 * Create the message body.
Chris@0 56 *
Chris@0 57 * @param string|StreamInterface $html
Chris@0 58 * @return StreamInterface
Chris@0 59 * @throws InvalidArgumentException if $html is neither a string or stream.
Chris@0 60 */
Chris@0 61 private function createBody($html)
Chris@0 62 {
Chris@0 63 if ($html instanceof StreamInterface) {
Chris@0 64 return $html;
Chris@0 65 }
Chris@0 66
Chris@0 67 if (! is_string($html)) {
Chris@0 68 throw new InvalidArgumentException(sprintf(
Chris@0 69 'Invalid content (%s) provided to %s',
Chris@0 70 (is_object($html) ? get_class($html) : gettype($html)),
Chris@0 71 __CLASS__
Chris@0 72 ));
Chris@0 73 }
Chris@0 74
Chris@0 75 $body = new Stream('php://temp', 'wb+');
Chris@0 76 $body->write($html);
Chris@0 77 $body->rewind();
Chris@0 78 return $body;
Chris@0 79 }
Chris@0 80 }