Passed
Pull Request — master (#233)
by Dmitriy
02:03
created

HtmlResponseFormatter::format()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Web\Formatter;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Yiisoft\Yii\Web\WebResponse;
9
10
final class HtmlResponseFormatter implements ResponseFormatterInterface
11
{
12
    /**
13
     * @var string the Content-Type header for the response
14
     */
15
    private string $contentType = 'text/html';
16
17
    /**
18
     * @var string the XML encoding.
19
     */
20
    private string $encoding = 'UTF-8';
21
22
    public function format(WebResponse $webResponse): ResponseInterface
23
    {
24
        $data = $webResponse->getData();
25
        $response = $webResponse->getResponse();
26
        $response->getBody()->write((string)$data);
27
28
        return $response->withHeader('Content-Type', $this->contentType . '; charset=' . $this->encoding);
29
    }
30
31
    public function withEncoding(string $encoding): self
32
    {
33
        $formatter = clone $this;
34
        $formatter->encoding = $encoding;
35
        return $formatter;
36
    }
37
38
    public function withContentType(string $contentType): self
39
    {
40
        $formatter = clone $this;
41
        $this->contentType = $contentType;
42
        return $formatter;
43
    }
44
}
45