- md5
- da13139b5f4e3fa5e9acd6270fca7102
- sha1
- 264de1ed5c2f1ad76a095fd3bab02bebb7d8c852
- sha256
- 7738b583afcc2d11bcf09651d65cbb6ad3491069b0f05fa6f5cdc90c5663d8a2
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 get_debug_type;
use function is_string;
use function sprintf;
/**
* Plain text response.
*
* Allows creating a response by passing a string to the constructor;
* by default, sets a status code of 200 and sets the Content-Type header to
* text/plain.
*/
class TextResponse extends Response
{
use InjectContentTypeTrait;
/**
* Create a plain text response.
*
* Produces a text response with a Content-Type of text/plain and a default
* status of 200.
*
* @param string|StreamInterface $text String or stream for the message body.
* @param int $status Integer status code for the response; 200 by default.
* @param array<non-empty-string, string|string[]> $headers Array of headers to use at initialization.
* @throws Exception\InvalidArgumentException If $text is neither a string or stream.
*/
public function __construct($text, int $status = 200, array $headers = [])
{
parent::__construct(
$this->createBody($text),
$status,
$this->injectContentType('text/plain; charset=utf-8', $headers)
);
}
/**
* Create the message body.
*
* @param string|StreamInterface $text
* @throws Exception\InvalidArgumentException If $text is neither a string or stream.
*/
private function createBody($text): StreamInterface
{
if ($text instanceof StreamInterface) {
return $text;
}
/** @psalm-suppress DocblockTypeContradiction */
if (! is_string($text)) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid content (%s) provided to %s',
get_debug_type($text),
self::class
));
}
$body = new Stream('php://temp', 'wb+');
$body->write($text);
$body->rewind();
return $body;
}
}
The same content is in 32 releases, Joomla 6.1.0-alpha2 to 6.3.0-alpha1. See them all, or every version of this file.