|
1
|
|
|
<?php |
|
2
|
|
|
namespace GuzzleHttp\Command\Guzzle\RequestLocation; |
|
3
|
|
|
|
|
4
|
|
|
use GuzzleHttp\Command\CommandInterface; |
|
5
|
|
|
use GuzzleHttp\Command\Guzzle\Parameter; |
|
6
|
|
|
use GuzzleHttp\Psr7; |
|
7
|
|
|
use Psr\Http\Message\RequestInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Adds POST files to a request |
|
11
|
|
|
*/ |
|
12
|
|
|
class MultiPartLocation extends AbstractLocation |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var string $contentType */ |
|
15
|
|
|
protected $contentType = 'multipart/form-data; boundary='; |
|
16
|
|
|
|
|
17
|
|
|
/** @var array $formParamsData */ |
|
18
|
|
|
protected $multipartData = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Set the name of the location |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $locationName |
|
24
|
|
|
*/ |
|
25
|
1 |
|
public function __construct($locationName = 'multipart') |
|
26
|
|
|
{ |
|
27
|
1 |
|
parent::__construct($locationName); |
|
28
|
1 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param CommandInterface $command |
|
32
|
|
|
* @param RequestInterface $request |
|
33
|
|
|
* @param Parameter $param |
|
34
|
|
|
* @return RequestInterface |
|
35
|
|
|
*/ |
|
36
|
1 |
|
public function visit( |
|
37
|
|
|
CommandInterface $command, |
|
38
|
|
|
RequestInterface $request, |
|
39
|
|
|
Parameter $param |
|
40
|
|
|
) { |
|
41
|
1 |
|
$modify = []; |
|
42
|
|
|
|
|
43
|
1 |
|
$this->multipartData['multipart'] = [ |
|
44
|
1 |
|
'name' => $param->getWireName(), |
|
45
|
1 |
|
'contents' => $this->prepareValue($command[$param->getName()], $param) |
|
|
|
|
|
|
46
|
1 |
|
]; |
|
47
|
|
|
|
|
48
|
1 |
|
$body = new Psr7\MultipartStream($this->multipartData); |
|
49
|
1 |
|
$modify['body'] = Psr7\stream_for($body); |
|
50
|
1 |
|
$request = Psr7\modify_request($request, $modify); |
|
51
|
1 |
|
if ($request->getBody() instanceof Psr7\MultipartStream) { |
|
|
|
|
|
|
52
|
|
|
// Use a multipart/form-data POST if a Content-Type is not set. |
|
53
|
1 |
|
$request->withHeader('Content-Type', $this->contentType . $request->getBody()->getBoundary()); |
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
return $request; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|