|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace WebServCo\Framework\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use WebServCo\Framework\Http\Response; |
|
8
|
|
|
use WebServCo\Framework\Interfaces\OutputLoaderInterface; |
|
9
|
|
|
|
|
10
|
|
|
trait OutputTrait |
|
11
|
|
|
{ |
|
12
|
|
|
protected int $outputCode; |
|
13
|
|
|
protected OutputLoaderInterface $outputLoader; |
|
14
|
|
|
|
|
15
|
|
|
final protected function setOutputLoader(OutputLoaderInterface $outputLoader): bool |
|
16
|
|
|
{ |
|
17
|
|
|
$this->outputCode = 200; // default |
|
18
|
|
|
$this->outputLoader = $outputLoader; |
|
19
|
|
|
return true; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
final protected function output(): OutputLoaderInterface |
|
23
|
|
|
{ |
|
24
|
|
|
return $this->outputLoader; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
final protected function outputCli(string $string = '', bool $eol = true): bool |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->output()->cli($string, $eol); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param array<int|string,mixed> $data |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function outputHtmlPartial(array $data, string $template): Response |
|
36
|
|
|
{ |
|
37
|
|
|
return new Response( |
|
38
|
|
|
$this->output()->html($data, $template), |
|
39
|
|
|
$this->outputCode, |
|
40
|
|
|
['Content-Type' => ['text/html']], |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param array<int|string,mixed> $data |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function outputHtml(array $data, string $pageTemplate, ?string $mainTemplate = null): Response |
|
48
|
|
|
{ |
|
49
|
|
|
return new Response( |
|
50
|
|
|
$this->output()->htmlPage($data, $pageTemplate, $mainTemplate), |
|
51
|
|
|
$this->outputCode, |
|
52
|
|
|
['Content-Type' => ['text/html']], |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param array<int|string,mixed> $content |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function outputJson(array $content, bool $result = true): Response |
|
60
|
|
|
{ |
|
61
|
|
|
$data = [ |
|
62
|
|
|
'data' => $content, |
|
63
|
|
|
'result' => $result, |
|
64
|
|
|
]; |
|
65
|
|
|
return new Response( |
|
66
|
|
|
$this->output()->json($data), |
|
67
|
|
|
$this->outputCode, |
|
68
|
|
|
['Content-Type' => ['application/json']], |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function setOutputCode(int $outputCode): bool |
|
73
|
|
|
{ |
|
74
|
|
|
$this->outputCode = $outputCode; |
|
75
|
|
|
return true; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|