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 {@see \Stringable}.
Public Methods
Method Details
| public mixed __construct ( string $contentType = 'text/html', string $encoding = 'UTF-8' ) | ||
| $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 string formatData ( mixed $data ) | ||
| $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 \Psr\Http\Message\ResponseInterface formatResponse ( \Psr\Http\Message\ResponseInterface $response ) | ||
| $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.