Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| HtmlResponse | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * This file is part of php-fast-forward/http-message. |
| 7 | * |
| 8 | * This source file is subject to the license bundled |
| 9 | * with this source code in the file LICENSE. |
| 10 | * |
| 11 | * @link https://github.com/php-fast-forward/http-message |
| 12 | * @copyright Copyright (c) 2025 Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 13 | * @license https://opensource.org/licenses/MIT MIT License |
| 14 | */ |
| 15 | |
| 16 | namespace FastForward\Http\Message; |
| 17 | |
| 18 | use FastForward\Http\Message\Header\ContentType; |
| 19 | use Nyholm\Psr7\Response; |
| 20 | use Nyholm\Psr7\Stream; |
| 21 | |
| 22 | /** |
| 23 | * Class HtmlResponse. |
| 24 | * |
| 25 | * Represents an HTTP response containing HTML content. |
| 26 | * |
| 27 | * This class MUST be used to generate HTTP responses with a `text/html` content type. |
| 28 | * It automatically sets the 'Content-Type' header, encodes the body using the specified charset, |
| 29 | * and applies the HTTP 200 (OK) status code by default. |
| 30 | * |
| 31 | * @package FastForward\Http\Message |
| 32 | */ |
| 33 | final class HtmlResponse extends Response |
| 34 | { |
| 35 | /** |
| 36 | * Constructs a new HtmlResponse instance. |
| 37 | * |
| 38 | * This constructor SHALL set the 'Content-Type' header to `text/html` with the specified charset |
| 39 | * and initialize the response body with the provided HTML content. The response status code |
| 40 | * will be set to 200 (OK) by default, with the corresponding reason phrase. |
| 41 | * |
| 42 | * @param string $html the HTML content to send in the response body |
| 43 | * @param string $charset The character encoding to declare in the 'Content-Type' header. Defaults to 'utf-8'. |
| 44 | * @param array<string, string|string[]> $headers optional additional headers to include in the response |
| 45 | */ |
| 46 | public function __construct(string $html, string $charset = 'utf-8', array $headers = []) |
| 47 | { |
| 48 | $headers['Content-Type'] = ContentType::TextHtml->withCharset($charset); |
| 49 | |
| 50 | parent::__construct( |
| 51 | status: StatusCode::Ok->value, |
| 52 | headers: $headers, |
| 53 | body: Stream::create($html), |
| 54 | reason: StatusCode::Ok->getReasonPhrase(), |
| 55 | ); |
| 56 | } |
| 57 | } |