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