26
26
*/
27
27
class DownloadResponse extends Response
28
28
{
29
+ const DEFAULT_CONTENT_TYPE = 'application/octet-stream ' ;
30
+ const DEFAULT_DOWNLOAD_FILENAME = 'download ' ;
31
+
29
32
/**
30
33
* A list of header keys required to be sent with a download response
31
34
*
@@ -40,36 +43,52 @@ class DownloadResponse extends Response
40
43
'pragma '
41
44
];
42
45
46
+ /**
47
+ * @var string The filename to be sent with the response
48
+ */
49
+ private $ filename ;
50
+
51
+ /**
52
+ * @var string The content type to be sent with the response
53
+ */
54
+ private $ contentType ;
55
+
43
56
/**
44
57
* DownloadResponse constructor.
58
+ *
45
59
* @param string|StreamInterface $body String or stream for the message body.
46
60
* @param int $status Integer status code for the response; 200 by default.
47
- * @param string $filename The name of the file to be downloaded
48
- * @param array $headers Array of headers to use at initialization.
49
- * @throws InvalidArgumentException if $text is neither a string or stream.
50
- * @param array $headers
51
- */
52
- public function __construct ($ body , int $ status = 200 , string $ filename = 'download ' , array $ headers = [])
53
- {
61
+ * @param string $filename The file name to be sent with the response
62
+ * @param string $contentType The content type to be sent with the response
63
+ * @param array $headers An array of optional headers. These cannot override those set in getDownloadHeaders */
64
+ public function __construct (
65
+ $ body ,
66
+ int $ status = 200 ,
67
+ string $ filename = self ::DEFAULT_DOWNLOAD_FILENAME ,
68
+ string $ contentType = self ::DEFAULT_CONTENT_TYPE ,
69
+ array $ headers = []
70
+ ) {
71
+ $ this ->filename = $ filename ;
72
+ $ this ->contentType = $ contentType ;
73
+
54
74
parent ::__construct (
55
75
$ this ->createBody ($ body ),
56
76
$ status ,
57
- $ this ->prepareDownloadHeaders ($ filename , $ headers )
77
+ $ this ->prepareDownloadHeaders ($ headers )
58
78
);
59
79
}
60
80
61
81
/**
62
82
* Get download headers
63
83
*
64
- * @param string $filename
65
84
* @return array
66
85
*/
67
- private function getDownloadHeaders (string $ filename ): array
86
+ private function getDownloadHeaders (): array
68
87
{
69
88
$ headers = [];
70
89
$ headers ['cache-control ' ] = 'must-revalidate ' ;
71
90
$ headers ['content-description ' ] = 'File Transfer ' ;
72
- $ headers ['content-disposition ' ] = sprintf ('attachment; filename=%s ' , $ filename );
91
+ $ headers ['content-disposition ' ] = sprintf ('attachment; filename=%s ' , self :: DEFAULT_DOWNLOAD_FILENAME );
73
92
$ headers ['content-transfer-encoding ' ] = 'Binary ' ;
74
93
$ headers ['content-type ' ] = 'application/octet-stream ' ;
75
94
$ headers ['expires ' ] = '0 ' ;
@@ -104,11 +123,16 @@ public function overridesDownloadHeaders(array $downloadHeaders, array $headers
104
123
/**
105
124
* Prepare download response headers
106
125
*
107
- * @param string $filename
126
+ * This function prepares the download response headers. It does so by:
127
+ * - Merging the optional with over the default ones (the default ones cannot be overridden)
128
+ * - Set the content-type and content-disposition headers from $filename and $contentType passed
129
+ * to the constructor.
130
+ *
108
131
* @param array $headers
109
132
* @return array
133
+ * @throws InvalidArgumentException if an attempt is made to override a default header
110
134
*/
111
- private function prepareDownloadHeaders (string $ filename , array $ headers = []) : array
135
+ private function prepareDownloadHeaders (array $ headers = []) : array
112
136
{
113
137
if ($ this ->overridesDownloadHeaders ($ this ->downloadResponseHeaders , $ headers )) {
114
138
throw new InvalidArgumentException (
@@ -119,7 +143,14 @@ private function prepareDownloadHeaders(string $filename, array $headers = []) :
119
143
);
120
144
}
121
145
122
- return array_merge ($ headers , $ this ->getDownloadHeaders ($ filename ));
146
+ return array_merge (
147
+ $ headers ,
148
+ $ this ->getDownloadHeaders (),
149
+ [
150
+ 'content-disposition ' => sprintf ('attachment; filename=%s ' , $ this ->filename ),
151
+ 'content-type ' => $ this ->contentType ,
152
+ ]
153
+ );
123
154
}
124
155
125
156
/**
0 commit comments