9
9
10
10
namespace Zend \Diactoros \Response ;
11
11
12
+ use Psr \Http \Message \StreamInterface ;
12
13
use Zend \Diactoros \Exception \InvalidArgumentException ;
13
14
use Zend \Diactoros \Response ;
14
15
@@ -41,20 +42,20 @@ class DownloadResponse extends Response
41
42
42
43
/**
43
44
* 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.
47
50
* @param array $headers
48
51
*/
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 = [])
50
53
{
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
+ );
58
59
}
59
60
60
61
/**
@@ -66,13 +67,13 @@ public function __construct($body, int $status = 200, string $filename = '', arr
66
67
private function getDownloadHeaders (string $ filename ): array
67
68
{
68
69
$ 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 ' ;
76
77
77
78
return $ headers ;
78
79
}
@@ -120,4 +121,29 @@ private function prepareDownloadHeaders(string $filename, array $headers = []) :
120
121
121
122
return array_merge ($ headers , $ this ->getDownloadHeaders ($ filename ));
122
123
}
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
+ }
123
149
}
0 commit comments