Repository navigation
Expand file tree
/
Copy pathBaseEndpoint.php
More file actions
330 lines (284 loc) · 12.1 KB
/
Copy pathBaseEndpoint.php
File metadata and controls
330 lines (284 loc) · 12.1 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
declare(strict_types=1);
namespace Docker\API\Runtime\Client;
use Http\Message\MultipartStream\MultipartStreamBuilder;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Serializer\SerializerInterface;
abstract class BaseEndpoint implements Endpoint
{
protected array $formParameters = [];
protected array $queryParameters = [];
protected array $headerParameters = [];
protected mixed $body;
abstract public function getMethod(): string;
abstract public function getBody(SerializerInterface $serializer, $streamFactory = null): array;
abstract public function getUri(): string;
abstract public function getAuthenticationScopes(): array;
abstract protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, ?string $contentType = null);
protected function getExtraHeaders(): array
{
return [];
}
public function getQueryString(): string
{
$optionsResolved = $this->getQueryOptionsResolver()->resolve($this->queryParameters);
$styles = $this->getQueryStyles();
$allowReserved = $this->getQueryAllowReserved();
$queryParameters = [];
foreach ($optionsResolved as $key => $value) {
if (\array_key_exists($key, $styles)) {
if (null === $value) {
continue;
}
foreach ($this->encodeStyledValue($key, $value, $styles[$key], \in_array($key, $allowReserved, true)) as $encodedValue) {
$queryParameters[] = $encodedValue;
}
continue;
}
$value ??= '';
$allowReservedKey = \in_array($key, $allowReserved, true);
$queryParameters[] = $this->encodeValue($key, $value, $allowReservedKey);
}
return implode('&', $queryParameters);
}
public function getHeaders(array $baseHeaders = []): array
{
$headersResolver = $this->getHeadersOptionsResolver();
$definedOptions = $headersResolver->getDefinedOptions();
$headerParameters = [];
foreach ($this->headerParameters as $name => $value) {
foreach ($definedOptions as $definedOption) {
if (0 === strcasecmp((string) $name, $definedOption)) {
$name = $definedOption;
break;
}
}
$headerParameters[$name] = $value;
}
return array_merge($this->getExtraHeaders(), $baseHeaders, $headersResolver->resolve($headerParameters));
}
protected function getQueryOptionsResolver(): OptionsResolver
{
return new OptionsResolver();
}
protected function getQueryAllowReserved(): array
{
return [];
}
/**
* Declares the OpenAPI serialization style of query parameters.
*
* Keys are query parameter names, values follow the shape
* ['style' => string, 'explode' => bool]. Parameters with an
* explicit `content` must not be declared here: content based
* serialization takes precedence over styles.
*
* @return array<string, array{style?: string, explode?: bool}>
*/
protected function getQueryStyles(): array
{
return [];
}
protected function getHeadersOptionsResolver(): OptionsResolver
{
return new OptionsResolver();
}
// ----------------------------------------------------------------------------------------------------
// Used for OpenApi2 compatibility
protected function getFormBody(): array
{
return [['Content-Type' => ['application/x-www-form-urlencoded']], http_build_query($this->getFormOptionsResolver()->resolve($this->formParameters))];
}
protected function getMultipartBody($streamFactory = null): array
{
$bodyBuilder = new MultipartStreamBuilder($streamFactory);
$formParameters = $this->getFormOptionsResolver()->resolve($this->formParameters);
foreach ($formParameters as $key => $value) {
$bodyBuilder->addResource($key, $value);
}
return [['Content-Type' => ['multipart/form-data; boundary="'.($bodyBuilder->getBoundary().'"')]], $bodyBuilder->build()];
}
protected function getFormOptionsResolver(): OptionsResolver
{
return new OptionsResolver();
}
protected function getSerializedBody(SerializerInterface $serializer): array
{
return [['Content-Type' => ['application/json']], $serializer->serialize($this->body, 'json')];
}
protected function getSerializedObjectBody(SerializerInterface $serializer): array
{
return [['Content-Type' => ['application/json']], JsonPayload::encode($serializer, $this->body)];
}
private function encodeValue(string $key, mixed $value, bool $allowReserved): string
{
return match (true) {
\is_int($value) => $this->encodeIntValue($key, $value, $allowReserved),
\is_bool($value) => $this->encodeIntValue($key, (int) $value, $allowReserved),
\is_string($value) => $this->encodeStringValue($key, $value, $allowReserved),
\is_float($value) => $this->encodeStringValue($key, (string) $value, $allowReserved),
\is_array($value) => $this->encodeArrayValue($key, $value, $allowReserved),
default => throw new \InvalidArgumentException(\sprintf('Query value for key %s must be either int|string|float|array|bool, %s given', $key, \gettype($value))),
};
}
private function encodeIntValue(string $queryParamName, int $value, bool $allowReserved): string
{
$queryParamName = rawurlencode($queryParamName);
return \sprintf('%s=%s', $queryParamName, $allowReserved ? $value : rawurlencode((string) $value));
}
private function encodeStringValue(string $queryParamName, string $value, bool $allowReserved): string
{
$queryParamName = rawurlencode($queryParamName);
return \sprintf('%s=%s', $queryParamName, $allowReserved ? $value : rawurlencode($value));
}
private function encodeArrayValue(string $queryParamName, array $value, bool $allowReserved): string
{
$params = [];
foreach ($value as $subKey => $subValue) {
$arrayKey = $queryParamName.'['.$subKey.']';
$params[] = $this->encodeValue($arrayKey, $subValue, $allowReserved);
}
return implode('&', $params);
}
/**
* @param array{style?: string, explode?: bool} $styleConfig
*
* @return string[]
*/
private function encodeStyledValue(string $name, mixed $value, array $styleConfig, bool $allowReserved): array
{
$style = $styleConfig['style'] ?? 'form';
$explode = $styleConfig['explode'] ?? false;
return match ($style) {
'form' => $this->encodeFormStyle($name, $value, $explode, $allowReserved),
'spaceDelimited' => $this->encodeDelimitedStyle($name, $value, 'spaceDelimited', $allowReserved),
'pipeDelimited' => $this->encodeDelimitedStyle($name, $value, 'pipeDelimited', $allowReserved),
'deepObject' => $this->encodeDeepObjectStyle($name, $value, $allowReserved),
default => throw new \InvalidArgumentException(\sprintf('Unsupported query parameter style "%s" for parameter "%s".', $style, $name)),
};
}
/**
* @return string[]
*/
private function encodeFormStyle(string $name, mixed $value, bool $explode, bool $allowReserved): array
{
if (!\is_array($value)) {
return [$this->encodeValue($name, $value, $allowReserved)];
}
if ([] === $value) {
return [];
}
if (!$explode) {
return [$this->implodeStyledValues($name, $value, 'form', $allowReserved)];
}
$pairs = [];
if (array_is_list($value)) {
// Exploded arrays repeat the parameter name for each item.
foreach ($value as $index => $item) {
if (\is_array($item)) {
// Nested levels use bracket notation.
$pairs = array_merge($pairs, $this->flattenBracketPairs($name.'['.$index.']', $item, $allowReserved));
continue;
}
if (null === $item) {
continue;
}
$pairs[] = $this->encodeValue($name, $item, $allowReserved);
}
return $pairs;
}
// Exploded objects drop the parent key: each property becomes a top level pair.
foreach ($value as $subKey => $subValue) {
if (\is_array($subValue)) {
$pairs = array_merge($pairs, $this->flattenBracketPairs((string) $subKey, $subValue, $allowReserved));
continue;
}
if (null === $subValue) {
continue;
}
$pairs[] = $this->encodeValue((string) $subKey, $subValue, $allowReserved);
}
return $pairs;
}
/**
* @return string[]
*/
private function encodeDelimitedStyle(string $name, mixed $value, string $style, bool $allowReserved): array
{
if (!\is_array($value)) {
return [$this->encodeValue($name, $value, $allowReserved)];
}
if ([] === $value) {
return [];
}
return [$this->implodeStyledValues($name, $value, $style, $allowReserved)];
}
/**
* @return string[]
*/
private function encodeDeepObjectStyle(string $name, mixed $value, bool $allowReserved): array
{
if (!\is_array($value)) {
return [$this->encodeValue($name, $value, $allowReserved)];
}
if ([] === $value) {
return [];
}
// deepObject uses bracket notation at every level, including the first one.
return $this->flattenBracketPairs($name, $value, $allowReserved);
}
/**
* Flattens nested values using PHP bracket notation with explicit indices
* (e.g. `filter[from]=a` or `tags[0]=b`).
*
* @return string[]
*/
private function flattenBracketPairs(string $prefix, mixed $value, bool $allowReserved): array
{
if (!\is_array($value)) {
if (null === $value) {
return [];
}
return [$this->encodeValue($prefix, $value, $allowReserved)];
}
$pairs = [];
foreach ($value as $subKey => $subValue) {
$pairs = array_merge($pairs, $this->flattenBracketPairs($prefix.'['.$subKey.']', $subValue, $allowReserved));
}
return $pairs;
}
private function implodeStyledValues(string $name, array $value, string $style, bool $allowReserved): string
{
// Delimiters stay literal (except spaces), values are percent encoded: this matches the OpenAPI examples.
$separator = match ($style) {
'spaceDelimited' => '%20',
'pipeDelimited' => '|',
default => ',',
};
$isList = array_is_list($value);
$parts = [];
foreach ($value as $subKey => $subValue) {
if (\is_array($subValue)) {
throw new \InvalidArgumentException(\sprintf('Query parameter "%s" only supports flat values with style "%s", nested arrays or objects are not supported.', $name, $style));
}
if (null === $subValue) {
continue;
}
if (!$isList) {
// Non exploded objects interleave keys and values.
$parts[] = rawurlencode((string) $subKey);
}
$parts[] = $allowReserved ? $this->stringifyScalar($subValue) : rawurlencode($this->stringifyScalar($subValue));
}
return \sprintf('%s=%s', rawurlencode($name), implode($separator, $parts));
}
private function stringifyScalar(mixed $value): string
{
return match (true) {
\is_bool($value) => (string) (int) $value,
\is_int($value), \is_float($value), \is_string($value) => (string) $value,
default => throw new \InvalidArgumentException(\sprintf('Query value must be either int|string|float|bool|array|null, %s given', \gettype($value))),
};
}
}