1
|
|
|
<?php |
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
|
|
|
|
9
|
|
|
use OpenStack\Common\Api\Operation; |
10
|
|
|
use OpenStack\Common\Api\Parameter; |
11
|
|
|
|
12
|
|
|
class RequestSerializer |
13
|
|
|
{ |
14
|
|
|
private $jsonSerializer; |
15
|
|
|
|
16
|
182 |
|
public function __construct(JsonSerializer $jsonSerializer = null) |
17
|
|
|
{ |
18
|
182 |
|
$this->jsonSerializer = $jsonSerializer ?: new JsonSerializer(); |
19
|
182 |
|
} |
20
|
|
|
|
21
|
182 |
|
public function serializeOptions(Operation $operation, array $userValues = []) |
22
|
|
|
{ |
23
|
182 |
|
$options = ['headers' => []]; |
24
|
|
|
|
25
|
182 |
|
foreach ($userValues as $paramName => $paramValue) { |
26
|
168 |
|
if (null === ($schema = $operation->getParam($paramName))) { |
27
|
1 |
|
continue; |
28
|
|
|
} |
29
|
|
|
|
30
|
167 |
|
$method = sprintf('stock%s', ucfirst($schema->getLocation())); |
31
|
167 |
|
$this->$method($schema, $paramValue, $options); |
32
|
182 |
|
} |
33
|
|
|
|
34
|
182 |
|
if (!empty($options['json']) && ($key = $operation->getJsonKey())) { |
35
|
36 |
|
$options['json'] = [$key => $options['json']]; |
36
|
36 |
|
} |
37
|
|
|
|
38
|
182 |
|
return $options; |
39
|
|
|
} |
40
|
|
|
|
41
|
115 |
|
private function stockUrl() |
42
|
115 |
|
{} |
43
|
|
|
|
44
|
11 |
|
private function stockQuery(Parameter $schema, $paramValue, array &$options) |
45
|
|
|
{ |
46
|
11 |
|
$options['query'][$schema->getName()] = $paramValue; |
47
|
11 |
|
} |
48
|
|
|
|
49
|
15 |
|
private function stockHeader(Parameter $schema, $paramValue, array &$options) |
50
|
|
|
{ |
51
|
15 |
|
$paramName = $schema->getName(); |
52
|
|
|
|
53
|
15 |
|
if (strpos(strtolower($paramName), 'metadata') !== false) { |
54
|
9 |
|
return $this->stockMetadataHeader($schema, $paramValue, $options); |
55
|
|
|
} |
56
|
|
|
|
57
|
15 |
|
$options['headers'] += is_scalar($paramValue) ? [$schema->getPrefixedName() => $paramValue] : []; |
58
|
15 |
|
} |
59
|
|
|
|
60
|
9 |
|
private function stockMetadataHeader(Parameter $schema, $paramValue, array &$options) |
61
|
|
|
{ |
62
|
9 |
|
foreach ($paramValue as $key => $keyVal) { |
63
|
9 |
|
$schema = $schema->getItemSchema() ?: new Parameter(['prefix' => $schema->getPrefix(), 'name' => $key]); |
64
|
9 |
|
$this->stockHeader($schema, $keyVal, $options); |
65
|
9 |
|
} |
66
|
9 |
|
} |
67
|
|
|
|
68
|
62 |
|
private function stockJson(Parameter $schema, $paramValue, array &$options) |
69
|
|
|
{ |
70
|
62 |
|
$json = isset($options['json']) ? $options['json'] : []; |
71
|
62 |
|
$options['json'] = $this->jsonSerializer->stockJson($schema, $paramValue, $json); |
72
|
62 |
|
} |
73
|
|
|
|
74
|
3 |
|
private function stockRaw(Parameter $schema, $paramValue, array &$options) |
|
|
|
|
75
|
|
|
{ |
76
|
3 |
|
$options['body'] = $paramValue; |
77
|
|
|
} |
78
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.