2
2
3
3
namespace Http \Message \MultipartStream ;
4
4
5
- use GuzzleHttp \Psr7 \AppendStream ;
6
5
use Http \Discovery \StreamFactoryDiscovery ;
7
6
use Http \Message \StreamFactory ;
8
7
use Psr \Http \Message \StreamInterface ;
9
- use Zend \Diactoros \CallbackStream ;
10
8
11
9
/**
12
10
* 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)
44
42
* Add a resource to the Multipart Stream. If the same $name is used twice the first resource will
45
43
* be overwritten.
46
44
*
47
- * @param string $name the formpost name
45
+ * @param string $name the formpost name
48
46
* @param string|resource|StreamInterface $resource
49
- * @param array $options {
47
+ * @param array $options {
50
48
*
51
49
* @var array $headers additional headers ['header-name' => 'header-value']
52
50
* @var string $filename
@@ -70,12 +68,11 @@ public function addResource($name, $resource, array $options = [])
70
68
if (substr ($ uri , 0 , 6 ) !== 'php:// ' ) {
71
69
$ options ['filename ' ] = $ uri ;
72
70
}
73
-
74
71
}
75
72
76
73
$ this ->prepareHeaders ($ name , $ stream , $ options ['filename ' ], $ options ['headers ' ]);
77
74
$ this ->data [$ name ] = ['contents ' => $ stream , 'headers ' => $ options ['headers ' ], 'filename ' => $ options ['filename ' ]];
78
-
75
+
79
76
return $ this ;
80
77
}
81
78
@@ -105,36 +102,34 @@ public function build()
105
102
}
106
103
107
104
/**
108
- * Add extra headers if they are missing
105
+ * Add extra headers if they are missing.
109
106
*
110
- * @param string $name
107
+ * @param string $name
111
108
* @param StreamInterface $stream
112
- * @param string $filename
113
- * @param array &$headers
109
+ * @param string $filename
110
+ * @param array &$headers
114
111
*/
115
112
private function prepareHeaders ($ name , StreamInterface $ stream , $ filename , array &$ headers )
116
113
{
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
+ }
125
122
}
126
123
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 ' )) {
130
126
if ($ length = $ stream ->getSize ()) {
131
127
$ headers ['Content-Length ' ] = (string ) $ length ;
132
128
}
133
129
}
134
130
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 ) {
138
133
if ($ type = MimetypeHelper::getMimetypeFromFilename ($ filename )) {
139
134
$ headers ['Content-Type ' ] = $ type ;
140
135
}
@@ -152,30 +147,30 @@ private function getHeaders(array $headers)
152
147
{
153
148
$ str = '' ;
154
149
foreach ($ headers as $ key => $ value ) {
155
- $ str .= "{ $ key } : { $ value } \r\n" ;
150
+ $ str .= sprintf ( " %s: %s \r\n", $ key , $ value ) ;
156
151
}
157
152
158
153
return $ str ;
159
154
}
160
155
161
156
/**
162
- * Get one header by its name .
157
+ * Check if header exist .
163
158
*
164
- * @param array $headers
165
- * @param string $key case insensitive
159
+ * @param array $headers
160
+ * @param string $key case insensitive
166
161
*
167
- * @return string|null
162
+ * @return bool
168
163
*/
169
- private function getHeader (array $ headers , $ key )
164
+ private function hasHeader (array $ headers , $ key )
170
165
{
171
166
$ lowercaseHeader = strtolower ($ key );
172
167
foreach ($ headers as $ k => $ v ) {
173
168
if (strtolower ($ k ) === $ lowercaseHeader ) {
174
- return $ v ;
169
+ return true ;
175
170
}
176
171
}
177
172
178
- return ;
173
+ return false ;
179
174
}
180
175
181
176
/**
0 commit comments