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