Final Class Yiisoft\DataResponse\Formatter\HtmlFormatter
| Inheritance | Yiisoft\DataResponse\Formatter\HtmlFormatter |
|---|---|
| Implements | Yiisoft\DataResponse\Formatter\FormatterInterface |
Formatter that converts data to HTML and sets appropriate response headers.
Supports scalar values, null, and objects implementing \Stringable.
Public Methods
Method Details
| public __construct( string $contentType = 'text/html', 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/html',
private readonly string $encoding = 'UTF-8',
) {}
| 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;
}
| 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");
}
Signup or Login in order to comment.