99
1010namespace Zend \Diactoros \Response ;
1111
12+ use Psr \Http \Message \StreamInterface ;
1213use Zend \Diactoros \Exception \InvalidArgumentException ;
1314use Zend \Diactoros \Response ;
1415
@@ -41,20 +42,20 @@ class DownloadResponse extends Response
4142
4243 /**
4344 * DownloadResponse constructor.
44- * @param $body
45- * @param int $status
46- * @param string $filename
45+ * @param string|StreamInterface $body String or stream for the message body.
46+ * @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.
4750 * @param array $headers
4851 */
49- public function __construct ($ body , int $ status = 200 , string $ filename = '' , array $ headers = [])
52+ public function __construct ($ body , int $ status = 200 , string $ filename = 'download ' , array $ headers = [])
5053 {
51- $ content = new Stream ('php://temp ' , 'wb+ ' );
52- $ content ->write ($ body );
53- $ content ->rewind ();
54-
55- $ headers = $ this ->prepareDownloadHeaders ($ filename , $ headers );
56-
57- parent ::__construct ($ content , $ status , $ headers );
54+ parent ::__construct (
55+ $ this ->createBody ($ body ),
56+ $ status ,
57+ $ this ->prepareDownloadHeaders ($ filename , $ headers )
58+ );
5859 }
5960
6061 /**
@@ -66,13 +67,13 @@ public function __construct($body, int $status = 200, string $filename = '', arr
6667 private function getDownloadHeaders (string $ filename ): array
6768 {
6869 $ headers = [];
69- $ headers ['cache-control ' ] = [ 'must-revalidate ' ] ;
70- $ headers ['content-description ' ] = [ 'File Transfer ' ] ;
71- $ headers ['content-disposition ' ] = [ sprintf ('attachment; filename=%s ' , $ filename )] ;
72- $ headers ['content-transfer-encoding ' ] = [ 'Binary ' ] ;
73- $ headers ['content-type ' ] = [ ' text/csv; charset=utf-8 ' ] ;
74- $ headers ['expires ' ] = [ '0 ' ] ;
75- $ headers ['pragma ' ] = [ 'Public ' ] ;
70+ $ headers ['cache-control ' ] = 'must-revalidate ' ;
71+ $ headers ['content-description ' ] = 'File Transfer ' ;
72+ $ headers ['content-disposition ' ] = sprintf ('attachment; filename=%s ' , $ filename );
73+ $ headers ['content-transfer-encoding ' ] = 'Binary ' ;
74+ $ headers ['content-type ' ] = ' application/octet-stream ' ;
75+ $ headers ['expires ' ] = '0 ' ;
76+ $ headers ['pragma ' ] = 'Public ' ;
7677
7778 return $ headers ;
7879 }
@@ -120,4 +121,29 @@ private function prepareDownloadHeaders(string $filename, array $headers = []) :
120121
121122 return array_merge ($ headers , $ this ->getDownloadHeaders ($ filename ));
122123 }
124+
125+ /**
126+ * @param string|StreamInterface $content
127+ * @return StreamInterface
128+ * @throws InvalidArgumentException if $body is neither a string nor a stream
129+ */
130+ private function createBody ($ content ): StreamInterface
131+ {
132+ if ($ content instanceof StreamInterface) {
133+ return $ content ;
134+ }
135+
136+ if (!is_string ($ content )) {
137+ throw new InvalidArgumentException (sprintf (
138+ 'Invalid content (%s) provided to %s ' ,
139+ (is_object ($ content ) ? get_class ($ content ) : gettype ($ content )),
140+ __CLASS__
141+ ));
142+ }
143+
144+ $ body = new Stream ('php://temp ' , 'wb+ ' );
145+ $ body ->write ($ content );
146+ $ body ->rewind ();
147+ return $ body ;
148+ }
123149}
0 commit comments