1
|
|
|
<?php |
2
|
|
|
namespace GuzzleHttp\Command\Guzzle\RequestLocation; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp\Command\CommandInterface; |
5
|
|
|
use GuzzleHttp\Command\Guzzle\Operation; |
6
|
|
|
use GuzzleHttp\Command\Guzzle\Parameter; |
7
|
|
|
use GuzzleHttp\Psr7; |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Add form_params to a request |
12
|
|
|
*/ |
13
|
|
|
class FormParamLocation extends AbstractLocation |
14
|
|
|
{ |
15
|
|
|
/** @var string $contentType */ |
16
|
|
|
protected $contentType = 'application/x-www-form-urlencoded; charset=utf-8'; |
17
|
|
|
|
18
|
|
|
/** @var array $formParamsData */ |
19
|
|
|
protected $formParamsData = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Set the name of the location |
23
|
|
|
* |
24
|
|
|
* @param string $locationName |
25
|
|
|
*/ |
26
|
2 |
|
public function __construct($locationName = 'formParam') |
27
|
|
|
{ |
28
|
2 |
|
parent::__construct($locationName); |
29
|
2 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param CommandInterface $command |
33
|
|
|
* @param RequestInterface $request |
34
|
|
|
* @param Parameter $param |
35
|
|
|
* |
36
|
|
|
* @return RequestInterface |
37
|
|
|
*/ |
38
|
2 |
View Code Duplication |
public function visit( |
|
|
|
|
39
|
|
|
CommandInterface $command, |
40
|
|
|
RequestInterface $request, |
41
|
|
|
Parameter $param |
42
|
|
|
) { |
43
|
2 |
|
$this->formParamsData['form_params'][$param->getWireName()] = $this->prepareValue( |
44
|
2 |
|
$command[$param->getName()], |
|
|
|
|
45
|
|
|
$param |
46
|
2 |
|
); |
47
|
|
|
|
48
|
2 |
|
return $request; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param CommandInterface $command |
53
|
|
|
* @param RequestInterface $request |
54
|
|
|
* @param Operation $operation |
55
|
|
|
* |
56
|
|
|
* @return RequestInterface |
57
|
|
|
*/ |
58
|
2 |
|
public function after( |
59
|
|
|
CommandInterface $command, |
60
|
|
|
RequestInterface $request, |
61
|
|
|
Operation $operation |
62
|
|
|
) { |
63
|
2 |
|
$data = $this->formParamsData; |
64
|
2 |
|
$this->formParamsData = []; |
65
|
2 |
|
$modify = []; |
66
|
|
|
|
67
|
|
|
// Add additional parameters to the form_params array |
68
|
2 |
|
$additional = $operation->getAdditionalParameters(); |
69
|
2 |
|
if ($additional && $additional->getLocation() == $this->locationName) { |
70
|
1 |
|
foreach ($command->toArray() as $key => $value) { |
71
|
1 |
|
if (!$operation->hasParam($key)) { |
72
|
1 |
|
$data['form_params'][$key] = $this->prepareValue($value, $additional); |
73
|
1 |
|
} |
74
|
1 |
|
} |
75
|
1 |
|
} |
76
|
|
|
|
77
|
2 |
|
$body = http_build_query($data['form_params'], '', '&'); |
78
|
2 |
|
$modify['body'] = Psr7\stream_for($body); |
79
|
2 |
|
$modify['set_headers']['Content-Type'] = $this->contentType; |
80
|
2 |
|
$request = Psr7\modify_request($request, $modify); |
81
|
|
|
|
82
|
2 |
|
return $request; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.