|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Selami; |
|
5
|
|
|
|
|
6
|
|
|
use Selami\Router\Router; |
|
7
|
|
|
use Zend\Diactoros\Stream; |
|
8
|
|
|
use finfo; |
|
9
|
|
|
|
|
10
|
|
|
final class ControllerResponse |
|
11
|
|
|
{ |
|
12
|
|
|
private $returnType; |
|
13
|
|
|
private $headers; |
|
14
|
|
|
private $data; |
|
15
|
|
|
private $statusCode; |
|
16
|
|
|
private $metaData; |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
public function __construct(int $returnType, int $statusCode, array $headers, array $data, array $metaData) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->returnType = $returnType; |
|
23
|
|
|
$this->statusCode = $statusCode; |
|
24
|
|
|
$this->headers = $headers; |
|
25
|
|
|
$this->data = $data; |
|
26
|
|
|
$this->metaData = $metaData; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public static function CUSTOM(int $statusCode, array $data, array $metaData, array $headers) : self |
|
30
|
|
|
{ |
|
31
|
|
|
return new self(Router::CUSTOM, $statusCode, $headers, $data, $metaData); |
|
32
|
|
|
} |
|
33
|
|
|
public static function EMPTY(int $statusCode, array $data, array $metaData, array $headers) : self |
|
34
|
|
|
{ |
|
35
|
|
|
return new self(Router::EMPTY, $statusCode, $headers, $data, $metaData); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public static function HTML(int $statusCode, array $data, ?array $metaData = []) : self |
|
39
|
|
|
{ |
|
40
|
|
|
return new self(Router::HTML, $statusCode, [], $data, $metaData); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function JSON(int $statusCode, array $data, ?array $metaData = []) : self |
|
44
|
|
|
{ |
|
45
|
|
|
return new self(Router::JSON, $statusCode, [], $data, $metaData); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public static function TEXT(int $statusCode, array $data, ?array $metaData = []) : self |
|
49
|
|
|
{ |
|
50
|
|
|
return new self(Router::TEXT, $statusCode, [], $data, $metaData); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public static function XML(int $statusCode, array $xmlData, ?array $metaData = []) : self |
|
54
|
|
|
{ |
|
55
|
|
|
return new self(Router::XML, $statusCode, [], $xmlData, $metaData); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public static function REDIRECT(int $statusCode, string $redirectUrl) : self |
|
59
|
|
|
{ |
|
60
|
|
|
return new self(Router::REDIRECT, $statusCode, [], [], ['uri' => $redirectUrl]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public static function DOWNLOAD(int $statusCode, string $filePath, ?string $fileName = null) : self |
|
64
|
|
|
{ |
|
65
|
|
|
|
|
66
|
|
|
$stream = new Stream(realpath($filePath), 'br'); |
|
67
|
|
|
$headers = [ |
|
68
|
|
|
'Content-Type' => (new finfo(FILEINFO_MIME))->file($filePath), |
|
69
|
|
|
'Content-Disposition' => 'attachment; filename=' . $fileName ?? basename($filePath), |
|
70
|
|
|
'Content-Transfer-Encoding' => 'Binary', |
|
71
|
|
|
'Content-Description' => 'File Transfer', |
|
72
|
|
|
'Pragma' => 'public', |
|
73
|
|
|
'Expires' => '0', |
|
74
|
|
|
'Cache-Control' => 'must-revalidate', |
|
75
|
|
|
'Content-Length' => (string) $stream->getSize() |
|
76
|
|
|
]; |
|
77
|
|
|
return new self(Router::DOWNLOAD, $statusCode, $headers, [], ['stream' => $stream]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getReturnType() : int |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->returnType; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function getStatusCode() : int |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->statusCode; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getHeaders() : array |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->headers; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function getData() : array |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->data; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function getMetaData() : array |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->metaData; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|