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 | 8 | 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 | 8 | public function addResource($name, $resource, array $options = []) |
|
83 | |||
84 | /** |
||
85 | * Build the stream. |
||
86 | * |
||
87 | * @return StreamInterface |
||
88 | */ |
||
89 | 8 | public function build() |
|
107 | |||
108 | /** |
||
109 | * Add extra headers if they are missing. |
||
110 | * |
||
111 | * @param string $name |
||
112 | * @param StreamInterface $stream |
||
113 | * @param string $filename |
||
114 | * @param array &$headers |
||
115 | */ |
||
116 | 8 | private function prepareHeaders($name, StreamInterface $stream, $filename, array &$headers) |
|
142 | |||
143 | /** |
||
144 | * Get the headers formatted for the HTTP message. |
||
145 | * |
||
146 | * @param array $headers |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | 7 | private function getHeaders(array $headers) |
|
159 | |||
160 | /** |
||
161 | * Check if header exist. |
||
162 | * |
||
163 | * @param array $headers |
||
164 | * @param string $key case insensitive |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | 8 | private function hasHeader(array $headers, $key) |
|
179 | |||
180 | /** |
||
181 | * Get the boundary that separates the streams. |
||
182 | * |
||
183 | * @return string |
||
184 | */ |
||
185 | 8 | public function getBoundary() |
|
193 | |||
194 | /** |
||
195 | * @param string $boundary |
||
196 | * |
||
197 | * @return MultipartStreamBuilder |
||
198 | */ |
||
199 | 2 | public function setBoundary($boundary) |
|
205 | |||
206 | /** |
||
207 | * @return MimetypeHelper |
||
208 | */ |
||
209 | 2 | private function getMimetypeHelper() |
|
217 | |||
218 | /** |
||
219 | * If you have custom file extension you may overwrite the default MimetypeHelper with your own. |
||
220 | * |
||
221 | * @param MimetypeHelper $mimetypeHelper |
||
222 | * |
||
223 | * @return MultipartStreamBuilder |
||
224 | */ |
||
225 | public function setMimetypeHelper(MimetypeHelper $mimetypeHelper) |
||
231 | |||
232 | /** |
||
233 | * Reset and clear all stored data. This allows you to use builder for a subsequent request. |
||
234 | * |
||
235 | * @return MultipartStreamBuilder |
||
236 | */ |
||
237 | 1 | public function reset() |
|
244 | |||
245 | /** |
||
246 | * Gets the filename from a given path. |
||
247 | * |
||
248 | * PHP's basename() does not properly support streams or filenames beginning with a non-US-ASCII character. |
||
249 | * |
||
250 | * @author Drupal 8.2 |
||
251 | * |
||
252 | * @param string $path |
||
253 | * |
||
254 | * @return string |
||
255 | */ |
||
256 | 2 | private function basename($path) |
|
272 | } |
||
273 |