2626 */
2727class DownloadResponse extends Response
2828{
29+ const DEFAULT_CONTENT_TYPE = 'application/octet-stream ' ;
30+ const DEFAULT_DOWNLOAD_FILENAME = 'download ' ;
31+
2932 /**
3033 * A list of header keys required to be sent with a download response
3134 *
@@ -40,36 +43,52 @@ class DownloadResponse extends Response
4043 'pragma '
4144 ];
4245
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+
4356 /**
4457 * DownloadResponse constructor.
58+ *
4559 * @param string|StreamInterface $body String or stream for the message body.
4660 * @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+
5474 parent ::__construct (
5575 $ this ->createBody ($ body ),
5676 $ status ,
57- $ this ->prepareDownloadHeaders ($ filename , $ headers )
77+ $ this ->prepareDownloadHeaders ($ headers )
5878 );
5979 }
6080
6181 /**
6282 * Get download headers
6383 *
64- * @param string $filename
6584 * @return array
6685 */
67- private function getDownloadHeaders (string $ filename ): array
86+ private function getDownloadHeaders (): array
6887 {
6988 $ headers = [];
7089 $ headers ['cache-control ' ] = 'must-revalidate ' ;
7190 $ 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 );
7392 $ headers ['content-transfer-encoding ' ] = 'Binary ' ;
7493 $ headers ['content-type ' ] = 'application/octet-stream ' ;
7594 $ headers ['expires ' ] = '0 ' ;
@@ -104,11 +123,16 @@ public function overridesDownloadHeaders(array $downloadHeaders, array $headers
104123 /**
105124 * Prepare download response headers
106125 *
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+ *
108131 * @param array $headers
109132 * @return array
133+ * @throws InvalidArgumentException if an attempt is made to override a default header
110134 */
111- private function prepareDownloadHeaders (string $ filename , array $ headers = []) : array
135+ private function prepareDownloadHeaders (array $ headers = []) : array
112136 {
113137 if ($ this ->overridesDownloadHeaders ($ this ->downloadResponseHeaders , $ headers )) {
114138 throw new InvalidArgumentException (
@@ -119,7 +143,14 @@ private function prepareDownloadHeaders(string $filename, array $headers = []) :
119143 );
120144 }
121145
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+ );
123154 }
124155
125156 /**
0 commit comments