22
33namespace Http \Message \MultipartStream ;
44
5- use GuzzleHttp \Psr7 \AppendStream ;
65use Http \Discovery \StreamFactoryDiscovery ;
76use Http \Message \StreamFactory ;
87use Psr \Http \Message \StreamInterface ;
9- use Zend \Diactoros \CallbackStream ;
108
119/**
1210 * Build your own Multipart stream. A Multipart stream is a collection of streams separated with a $bounary. This
@@ -44,9 +42,9 @@ public function __construct(StreamFactory $streamFactory = null)
4442 * Add a resource to the Multipart Stream. If the same $name is used twice the first resource will
4543 * be overwritten.
4644 *
47- * @param string $name the formpost name
45+ * @param string $name the formpost name
4846 * @param string|resource|StreamInterface $resource
49- * @param array $options {
47+ * @param array $options {
5048 *
5149 * @var array $headers additional headers ['header-name' => 'header-value']
5250 * @var string $filename
@@ -70,12 +68,11 @@ public function addResource($name, $resource, array $options = [])
7068 if (substr ($ uri , 0 , 6 ) !== 'php:// ' ) {
7169 $ options ['filename ' ] = $ uri ;
7270 }
73-
7471 }
7572
7673 $ this ->prepareHeaders ($ name , $ stream , $ options ['filename ' ], $ options ['headers ' ]);
7774 $ this ->data [$ name ] = ['contents ' => $ stream , 'headers ' => $ options ['headers ' ], 'filename ' => $ options ['filename ' ]];
78-
75+
7976 return $ this ;
8077 }
8178
@@ -105,36 +102,34 @@ public function build()
105102 }
106103
107104 /**
108- * Add extra headers if they are missing
105+ * Add extra headers if they are missing.
109106 *
110- * @param string $name
107+ * @param string $name
111108 * @param StreamInterface $stream
112- * @param string $filename
113- * @param array &$headers
109+ * @param string $filename
110+ * @param array &$headers
114111 */
115112 private function prepareHeaders ($ name , StreamInterface $ stream , $ filename , array &$ headers )
116113 {
117- // Set a default content-disposition header if one was no provided
118- $ disposition = $ this -> getHeader ( $ headers , ' content-disposition ' );
119- if (! $ disposition) {
120- $ headers [ ' Content-Disposition ' ] = ( $ filename === ' 0 ' || $ filename )
121- ? sprintf ('form-data; name="%s"; filename="%s" ' ,
122- $ name ,
123- basename ($ filename ))
124- : " form-data; name= \"{ $ name }\"" ;
114+ $ hasFilename = $ filename === ' 0 ' || $ filename ;
115+
116+ // Set a default content- disposition header if one was not provided
117+ if (! $ this -> hasHeader ( $ headers , ' content-disposition ' )) {
118+ $ headers [ ' Content-Disposition ' ] = sprintf ('form-data; name="%s" ' , $ name );
119+ if ( $ hasFilename ) {
120+ $ headers [ ' Content-Disposition ' ] .= sprintf ( ' ; filename="%s" ' , basename ($ filename ));
121+ }
125122 }
126123
127- // Set a default content-length header if one was no provided
128- $ length = $ this ->getHeader ($ headers , 'content-length ' );
129- if (!$ length ) {
124+ // Set a default content-length header if one was not provided
125+ if (!$ this ->hasHeader ($ headers , 'content-length ' )) {
130126 if ($ length = $ stream ->getSize ()) {
131127 $ headers ['Content-Length ' ] = (string ) $ length ;
132128 }
133129 }
134130
135- // Set a default Content-Type if one was not supplied
136- $ type = $ this ->getHeader ($ headers , 'content-type ' );
137- if (!$ type && ($ filename === '0 ' || $ filename )) {
131+ // Set a default Content-Type if one was not provided
132+ if (!$ this ->hasHeader ($ headers , 'content-type ' ) && $ hasFilename ) {
138133 if ($ type = MimetypeHelper::getMimetypeFromFilename ($ filename )) {
139134 $ headers ['Content-Type ' ] = $ type ;
140135 }
@@ -152,30 +147,30 @@ private function getHeaders(array $headers)
152147 {
153148 $ str = '' ;
154149 foreach ($ headers as $ key => $ value ) {
155- $ str .= "{ $ key } : { $ value } \r\n" ;
150+ $ str .= sprintf ( " %s: %s \r\n", $ key , $ value ) ;
156151 }
157152
158153 return $ str ;
159154 }
160155
161156 /**
162- * Get one header by its name .
157+ * Check if header exist .
163158 *
164- * @param array $headers
165- * @param string $key case insensitive
159+ * @param array $headers
160+ * @param string $key case insensitive
166161 *
167- * @return string|null
162+ * @return bool
168163 */
169- private function getHeader (array $ headers , $ key )
164+ private function hasHeader (array $ headers , $ key )
170165 {
171166 $ lowercaseHeader = strtolower ($ key );
172167 foreach ($ headers as $ k => $ v ) {
173168 if (strtolower ($ k ) === $ lowercaseHeader ) {
174- return $ v ;
169+ return true ;
175170 }
176171 }
177172
178- return ;
173+ return false ;
179174 }
180175
181176 /**
0 commit comments