[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/laminas/laminas-diactoros/src/Response/ -> HtmlResponse.php (source)

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


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer