|
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\MessageInterface; |
|
9
|
|
|
use Psr\Http\Message\RequestInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Creates a JSON document |
|
13
|
|
|
*/ |
|
14
|
|
|
class JsonLocation extends AbstractLocation |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var string Whether or not to add a Content-Type header when JSON is found */ |
|
17
|
|
|
private $jsonContentType; |
|
18
|
|
|
|
|
19
|
|
|
/** @var array */ |
|
20
|
|
|
private $jsonData; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param string $locationName Name of the location |
|
24
|
|
|
* @param string $contentType Content-Type header to add to the request if |
|
25
|
|
|
* JSON is added to the body. Pass an empty string to omit. |
|
26
|
|
|
*/ |
|
27
|
3 |
|
public function __construct($locationName = 'json', $contentType = 'application/json') |
|
28
|
|
|
{ |
|
29
|
3 |
|
parent::__construct($locationName); |
|
30
|
3 |
|
$this->jsonContentType = $contentType; |
|
31
|
3 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param CommandInterface $command |
|
35
|
|
|
* @param RequestInterface $request |
|
36
|
|
|
* @param Parameter $param |
|
37
|
|
|
* |
|
38
|
|
|
* @return RequestInterface |
|
39
|
|
|
*/ |
|
40
|
3 |
|
public function visit( |
|
41
|
|
|
CommandInterface $command, |
|
42
|
|
|
RequestInterface $request, |
|
43
|
|
|
Parameter $param |
|
44
|
|
|
) { |
|
45
|
3 |
|
$this->jsonData[$param->getWireName()] = $this->prepareValue( |
|
46
|
3 |
|
$command[$param->getName()], |
|
|
|
|
|
|
47
|
|
|
$param |
|
48
|
3 |
|
); |
|
49
|
|
|
|
|
50
|
3 |
|
return $request->withBody(Psr7\stream_for(\GuzzleHttp\json_encode($this->jsonData))); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param CommandInterface $command |
|
55
|
|
|
* @param RequestInterface $request |
|
56
|
|
|
* @param Operation $operation |
|
57
|
|
|
* |
|
58
|
|
|
* @return MessageInterface |
|
59
|
|
|
*/ |
|
60
|
3 |
|
public function after( |
|
61
|
|
|
CommandInterface $command, |
|
62
|
|
|
RequestInterface $request, |
|
63
|
|
|
Operation $operation |
|
64
|
|
|
) { |
|
65
|
3 |
|
$data = $this->jsonData; |
|
66
|
3 |
|
$this->jsonData = []; |
|
67
|
|
|
|
|
68
|
|
|
// Add additional parameters to the JSON document |
|
69
|
3 |
|
$additional = $operation->getAdditionalParameters(); |
|
70
|
3 |
View Code Duplication |
if ($additional && ($additional->getLocation() === $this->locationName)) { |
|
|
|
|
|
|
71
|
1 |
|
foreach ($command->toArray() as $key => $value) { |
|
72
|
1 |
|
if (!$operation->hasParam($key)) { |
|
73
|
1 |
|
$data[$key] = $this->prepareValue($value, $additional); |
|
74
|
1 |
|
} |
|
75
|
1 |
|
} |
|
76
|
1 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
// Don't overwrite the Content-Type if one is set |
|
79
|
3 |
|
if ($this->jsonContentType && !$request->hasHeader('Content-Type')) { |
|
80
|
3 |
|
$request = $request->withHeader('Content-Type', $this->jsonContentType); |
|
81
|
3 |
|
} |
|
82
|
|
|
|
|
83
|
3 |
|
return $request->withBody(Psr7\stream_for(\GuzzleHttp\json_encode($data))); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|