0 follower

Final Class Yiisoft\DataResponse\Formatter\PlainTextFormatter

InheritanceYiisoft\DataResponse\Formatter\PlainTextFormatter
ImplementsYiisoft\DataResponse\Formatter\FormatterInterface

Formatter that converts data to plain text and sets appropriate response headers.

Supports scalar values, null, and objects implementing \Stringable.

Method Details

Hide inherited methods

__construct() public method

public __construct( string $contentType 'text/plain', string $encoding 'UTF-8' ): mixed
$contentType string

The content type for the response.

$encoding string

The character encoding for the response.

                public function __construct(
    private readonly string $contentType = 'text/plain',
    private readonly string $encoding = 'UTF-8',
) {}

            
formatData() public method

public formatData( mixed $data ): string
$data mixed

                public function formatData(mixed $data): string
{
    if ($data === null) {
        return '';
    }
    if (!is_scalar($data) && !$data instanceof Stringable) {
        throw new DataEncodingException(
            sprintf(
                'Data must be either a scalar value, null, or a stringable object. %s given.',
                get_debug_type($data),
            ),
        );
    }
    return (string) $data;
}

            
formatResponse() public method

public formatResponse( \Psr\Http\Message\ResponseInterface $response ): \Psr\Http\Message\ResponseInterface
$response \Psr\Http\Message\ResponseInterface

                public function formatResponse(ResponseInterface $response): ResponseInterface
{
    return $response->withHeader(Header::CONTENT_TYPE, "$this->contentType; charset=$this->encoding");
}