Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
69.23% |
18 / 26 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
DataActionResult | |
69.23% |
18 / 26 |
|
66.67% |
4 / 6 |
16.19 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
convertText | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
convertJsonCore | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
convertJson | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
convertRaw | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
3.58 | |||
createResponse | |
62.50% |
10 / 16 |
|
0.00% |
0 / 1 |
6.32 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\Core\Mvc\Result; |
6 | |
7 | use PeServer\Core\Binary; |
8 | use PeServer\Core\Http\ContentType; |
9 | use PeServer\Core\Http\HttpResponse; |
10 | use PeServer\Core\Mime; |
11 | use PeServer\Core\Mvc\DataContent; |
12 | use PeServer\Core\Mvc\DownloadDataContent; |
13 | use PeServer\Core\Mvc\Result\IActionResult; |
14 | use PeServer\Core\Serialization\Json; |
15 | use PeServer\Core\Serialization\JsonSerializer; |
16 | use PeServer\Core\Serialization\SerializerBase; |
17 | use PeServer\Core\Text; |
18 | use PeServer\Core\Throws\ArgumentException; |
19 | |
20 | /** |
21 | * 結果操作: データ。 |
22 | */ |
23 | readonly class DataActionResult implements IActionResult |
24 | { |
25 | #region variable |
26 | |
27 | protected JsonSerializer $jsonSerializer; |
28 | |
29 | #endregion |
30 | |
31 | /** |
32 | * 生成。 |
33 | * |
34 | * @param DataContent $content |
35 | */ |
36 | public function __construct( |
37 | private DataContent $content, |
38 | ?JsonSerializer $jsonSerializer = null |
39 | ) { |
40 | $this->jsonSerializer = $jsonSerializer ?? new JsonSerializer(); |
41 | } |
42 | |
43 | #region function |
44 | |
45 | private function convertText(DataContent $content): string |
46 | { |
47 | return Text::toString($content->data); |
48 | } |
49 | |
50 | /** |
51 | * 配列をJSONに変換。 |
52 | * |
53 | * @param array<mixed> $data |
54 | * @return string |
55 | */ |
56 | private function convertJsonCore(array $data): string |
57 | { |
58 | $result = $this->jsonSerializer->save($data); |
59 | |
60 | return $result->toString(); |
61 | } |
62 | |
63 | private function convertJson(DataContent $content): string |
64 | { |
65 | return $this->convertJsonCore($content->data); //@phpstan-ignore-line json array |
66 | } |
67 | |
68 | private function convertRaw(DataContent $content): string |
69 | { |
70 | if ($content->data instanceof Binary) { |
71 | return $content->data->raw; |
72 | } |
73 | |
74 | if (is_array($content->data)) { |
75 | return $this->convertJsonCore($content->data); |
76 | } |
77 | |
78 | return strval($content->data); |
79 | } |
80 | |
81 | #endregion |
82 | |
83 | #region IActionResult |
84 | |
85 | public function createResponse(): HttpResponse |
86 | { |
87 | $response = new HttpResponse(); |
88 | |
89 | $response->status = $this->content->httpStatus; |
90 | |
91 | $response->header->setContentType(ContentType::create($this->content->mime)); |
92 | |
93 | if ($this->content instanceof DownloadDataContent) { |
94 | $fileName = urlencode($this->content->fileName); |
95 | $response->header->addValue('Content-Disposition', "attachment; filename*=UTF-8''$fileName"); |
96 | $response->header->addValue('Content-Length', (string)$this->content->data->count()); //@phpstan-ignore-line DownloadDataContent |
97 | $response->header->addValue('X-Content-Type-Options', 'nosniff'); |
98 | $response->header->addValue('Connection', 'close'); |
99 | $response->content = $this->convertRaw($this->content); |
100 | } else { |
101 | $response->content = match ($this->content->mime) { |
102 | Mime::TEXT => $this->convertText($this->content), |
103 | Mime::JSON => $this->convertJson($this->content), |
104 | default => $this->convertRaw($this->content), |
105 | }; |
106 | } |
107 | |
108 | return $response; |
109 | } |
110 | |
111 | #endregion |
112 | } |