- md5
- 5a86cf866218d5a01b3f848fd1ffed9e
- sha1
- fbdf738629e27a4628dcdad492835b17ce1875d1
- sha256
- 0ca6f38884f59532884847f1707f42aa8c6715dff6d22374095f5e9f682159d3
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
<?php
declare(strict_types=1);
namespace Laminas\Diactoros\Response;
use Laminas\Diactoros\Exception;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\Stream;
use Psr\Http\Message\StreamInterface;
use function gettype;
use function is_object;
use function is_string;
use function sprintf;
/**
* HTML response.
*
* Allows creating a response by passing an HTML string to the constructor;
* by default, sets a status code of 200 and sets the Content-Type header to
* text/html.
*/
class HtmlResponse extends Response
{
use InjectContentTypeTrait;
/**
* Create an HTML response.
*
* Produces an HTML response with a Content-Type of text/html and a default
* status of 200.
*
* @param string|StreamInterface $html HTML or stream for the message body.
* @param int $status Integer status code for the response; 200 by default.
* @param array $headers Array of headers to use at initialization.
* @throws Exception\InvalidArgumentException If $html is neither a string or stream.
*/
public function __construct($html, int $status = 200, array $headers = [])
{
parent::__construct(
$this->createBody($html),
$status,
$this->injectContentType('text/html; charset=utf-8', $headers)
);
}
/**
* Create the message body.
*
* @param string|StreamInterface $html
* @throws Exception\InvalidArgumentException If $html is neither a string or stream.
*/
private function createBody($html): StreamInterface
{
if ($html instanceof StreamInterface) {
return $html;
}
if (! is_string($html)) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid content (%s) provided to %s',
is_object($html) ? $html::class : gettype($html),
self::class
));
}
$body = new Stream('php://temp', 'wb+');
$body->write($html);
$body->rewind();
return $body;
}
}
The same content is in 109 releases, Joomla 5.0.0-alpha1 to 6.0.0-alpha3. See them all, or every version of this file.