1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Common\Transport; |
4
|
|
|
|
5
|
|
|
use function GuzzleHttp\uri_template; |
6
|
|
|
use function GuzzleHttp\Psr7\build_query; |
7
|
|
|
use function GuzzleHttp\Psr7\modify_request; |
8
|
|
|
use OpenStack\Common\Api\Operation; |
9
|
|
|
use OpenStack\Common\Api\Parameter; |
10
|
|
|
|
11
|
|
|
class RequestSerializer |
12
|
|
|
{ |
13
|
|
|
private $jsonSerializer; |
14
|
|
|
|
15
|
209 |
|
public function __construct(JsonSerializer $jsonSerializer = null) |
16
|
|
|
{ |
17
|
209 |
|
$this->jsonSerializer = $jsonSerializer ?: new JsonSerializer(); |
18
|
209 |
|
} |
19
|
|
|
|
20
|
209 |
|
public function serializeOptions(Operation $operation, array $userValues = []): array |
21
|
|
|
{ |
22
|
209 |
|
$options = ['headers' => []]; |
23
|
|
|
|
24
|
209 |
|
foreach ($userValues as $paramName => $paramValue) { |
25
|
191 |
|
if (null === ($schema = $operation->getParam($paramName))) { |
26
|
1 |
|
continue; |
27
|
|
|
} |
28
|
|
|
|
29
|
190 |
|
$this->callStockingMethod($schema, $paramValue, $options); |
30
|
190 |
|
} |
31
|
209 |
|
|
32
|
|
|
if (!empty($options['json'])) { |
33
|
209 |
|
if ($key = $operation->getJsonKey()) { |
34
|
68 |
|
$options['json'] = [$key => $options['json']]; |
35
|
38 |
|
} |
36
|
38 |
|
if (strpos(json_encode($options['json']), '\/') !== false) { |
37
|
68 |
|
$options['body'] = json_encode($options['json'], JSON_UNESCAPED_SLASHES); |
38
|
6 |
|
$options['headers']['Content-Type'] = 'application/json'; |
39
|
6 |
|
unset($options['json']); |
40
|
6 |
|
} |
41
|
6 |
|
} |
42
|
68 |
|
|
43
|
|
|
return $options; |
44
|
209 |
|
} |
45
|
|
|
|
46
|
|
|
private function callStockingMethod(Parameter $schema, $paramValue, array &$options) |
47
|
134 |
|
{ |
48
|
|
|
$location = $schema->getLocation(); |
49
|
134 |
|
|
50
|
|
|
$methods = ['query', 'header', 'json', 'raw']; |
51
|
12 |
|
if (!in_array($location, $methods)) { |
52
|
|
|
return; |
53
|
12 |
|
} |
54
|
12 |
|
|
55
|
|
|
$method = sprintf('stock%s', ucfirst($location)); |
56
|
18 |
|
$this->$method($schema, $paramValue, $options); |
57
|
|
|
} |
58
|
18 |
|
|
59
|
|
|
private function stockQuery(Parameter $schema, $paramValue, array &$options) |
60
|
18 |
|
{ |
61
|
9 |
|
$options['query'][$schema->getName()] = $paramValue; |
62
|
|
|
} |
63
|
|
|
|
64
|
18 |
|
private function stockHeader(Parameter $schema, $paramValue, array &$options) |
65
|
18 |
|
{ |
66
|
|
|
$paramName = $schema->getName(); |
67
|
9 |
|
|
68
|
|
|
if (stripos($paramName, 'metadata') !== false) { |
69
|
9 |
|
return $this->stockMetadataHeader($schema, $paramValue, $options); |
70
|
9 |
|
} |
71
|
9 |
|
|
72
|
9 |
|
$options['headers'] += is_scalar($paramValue) ? [$schema->getPrefixedName() => $paramValue] : []; |
73
|
9 |
|
} |
74
|
|
|
|
75
|
68 |
|
private function stockMetadataHeader(Parameter $schema, $paramValue, array &$options) |
76
|
|
|
{ |
77
|
68 |
|
foreach ($paramValue as $key => $keyVal) { |
78
|
68 |
|
$schema = $schema->getItemSchema() ?: new Parameter(['prefix' => $schema->getPrefix(), 'name' => $key]); |
79
|
68 |
|
$this->stockHeader($schema, $keyVal, $options); |
80
|
|
|
} |
81
|
6 |
|
} |
82
|
|
|
|
83
|
6 |
|
private function stockJson(Parameter $schema, $paramValue, array &$options) |
84
|
6 |
|
{ |
85
|
|
|
$json = isset($options['json']) ? $options['json'] : []; |
86
|
|
|
$options['json'] = $this->jsonSerializer->stockJson($schema, $paramValue, $json); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function stockRaw(Parameter $schema, $paramValue, array &$options) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$options['body'] = $paramValue; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.