1 | <?php |
||
16 | class MultipartStreamBuilder |
||
17 | { |
||
18 | /** |
||
19 | * @var StreamFactory |
||
20 | */ |
||
21 | private $streamFactory; |
||
22 | |||
23 | /** |
||
24 | * @var MimetypeHelper |
||
25 | */ |
||
26 | private $mimetypeHelper; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $boundary; |
||
32 | |||
33 | /** |
||
34 | * @var array Element where each Element is an array with keys ['contents', 'headers', 'filename'] |
||
35 | */ |
||
36 | private $data = []; |
||
37 | |||
38 | /** |
||
39 | * @param StreamFactory|null $streamFactory |
||
40 | */ |
||
41 | 9 | public function __construct(StreamFactory $streamFactory = null) |
|
45 | |||
46 | /** |
||
47 | * Add a resource to the Multipart Stream. If the same $name is used twice the first resource will |
||
48 | * be overwritten. |
||
49 | * |
||
50 | * @param string $name the formpost name |
||
51 | * @param string|resource|StreamInterface $resource |
||
52 | * @param array $options { |
||
53 | * |
||
54 | * @var array $headers additional headers ['header-name' => 'header-value'] |
||
55 | * @var string $filename |
||
56 | * } |
||
57 | * |
||
58 | * @return MultipartStreamBuilder |
||
59 | */ |
||
60 | 9 | public function addResource($name, $resource, array $options = []) |
|
83 | |||
84 | /** |
||
85 | * Build the stream. |
||
86 | * |
||
87 | * @return StreamInterface |
||
88 | */ |
||
89 | 9 | public function build() |
|
114 | |||
115 | /** |
||
116 | * Add extra headers if they are missing. |
||
117 | * |
||
118 | * @param string $name |
||
119 | * @param StreamInterface $stream |
||
120 | * @param string $filename |
||
121 | * @param array &$headers |
||
122 | */ |
||
123 | 9 | private function prepareHeaders($name, StreamInterface $stream, $filename, array &$headers) |
|
149 | |||
150 | /** |
||
151 | * Get the headers formatted for the HTTP message. |
||
152 | * |
||
153 | * @param array $headers |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | 8 | private function getHeaders(array $headers) |
|
166 | |||
167 | /** |
||
168 | * Check if header exist. |
||
169 | * |
||
170 | * @param array $headers |
||
171 | * @param string $key case insensitive |
||
172 | * |
||
173 | * @return bool |
||
174 | */ |
||
175 | 9 | private function hasHeader(array $headers, $key) |
|
186 | |||
187 | /** |
||
188 | * Get the boundary that separates the streams. |
||
189 | * |
||
190 | * @return string |
||
191 | */ |
||
192 | 9 | public function getBoundary() |
|
200 | |||
201 | /** |
||
202 | * @param string $boundary |
||
203 | * |
||
204 | * @return MultipartStreamBuilder |
||
205 | */ |
||
206 | 2 | public function setBoundary($boundary) |
|
212 | |||
213 | /** |
||
214 | * @return MimetypeHelper |
||
215 | */ |
||
216 | 3 | private function getMimetypeHelper() |
|
224 | |||
225 | /** |
||
226 | * If you have custom file extension you may overwrite the default MimetypeHelper with your own. |
||
227 | * |
||
228 | * @param MimetypeHelper $mimetypeHelper |
||
229 | * |
||
230 | * @return MultipartStreamBuilder |
||
231 | */ |
||
232 | public function setMimetypeHelper(MimetypeHelper $mimetypeHelper) |
||
238 | |||
239 | /** |
||
240 | * Reset and clear all stored data. This allows you to use builder for a subsequent request. |
||
241 | * |
||
242 | * @return MultipartStreamBuilder |
||
243 | */ |
||
244 | 1 | public function reset() |
|
251 | |||
252 | /** |
||
253 | * Gets the filename from a given path. |
||
254 | * |
||
255 | * PHP's basename() does not properly support streams or filenames beginning with a non-US-ASCII character. |
||
256 | * |
||
257 | * @author Drupal 8.2 |
||
258 | * |
||
259 | * @param string $path |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | 3 | private function basename($path) |
|
279 | } |
||
280 |