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 |
|
$this->stockOptions($schema, $paramName, $paramValue, $options); |
31
|
182 |
|
} |
32
|
|
|
|
33
|
182 |
|
if (!empty($options['json']) && ($key = $operation->getJsonKey())) { |
34
|
36 |
|
$options['json'] = [$key => $options['json']]; |
35
|
36 |
|
} |
36
|
|
|
|
37
|
182 |
|
return $options; |
38
|
|
|
} |
39
|
|
|
|
40
|
167 |
|
private function stockOptions(Parameter $schema, $paramName, $paramValue, array &$options) |
41
|
|
|
{ |
42
|
167 |
|
switch ($schema->getLocation()) { |
43
|
167 |
|
case 'query': |
44
|
11 |
|
$options['query'][$schema->getName()] = $paramValue; |
45
|
11 |
|
break; |
46
|
157 |
|
case 'header': |
47
|
15 |
|
$options['headers'] += $this->parseHeader($schema, $paramName, $paramValue); |
48
|
15 |
|
break; |
49
|
149 |
|
case 'json': |
50
|
62 |
|
$json = isset($options['json']) ? $options['json'] : []; |
51
|
62 |
|
$options['json'] = $this->jsonSerializer->stockJson($schema, $paramValue, $json); |
52
|
62 |
|
break; |
53
|
116 |
|
case 'raw': |
54
|
3 |
|
$options['body'] = $paramValue; |
55
|
3 |
|
break; |
56
|
167 |
|
} |
57
|
167 |
|
} |
58
|
|
|
|
59
|
15 |
|
private function parseHeader(Parameter $param, $name, $value) |
60
|
|
|
{ |
61
|
15 |
|
if ($name == 'metadata' || $name == 'removeMetadata') { |
62
|
9 |
|
$headers = []; |
63
|
9 |
|
foreach ($value as $key => $keyVal) { |
64
|
9 |
|
$schema = $param->getItemSchema() ?: new Parameter(['prefix' => $param->getPrefix(), 'name' => $key]); |
65
|
9 |
|
$headers += $this->parseHeader($schema, $key, $keyVal); |
66
|
9 |
|
} |
67
|
9 |
|
return $headers; |
68
|
|
|
} |
69
|
|
|
|
70
|
15 |
|
return is_string($value) || is_numeric($value) |
71
|
15 |
|
? [$param->getPrefix() . $param->getName() => $value] |
72
|
15 |
|
: []; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|