1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Nate Brunette. |
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace Tebru\Retrofit\Internal\ParameterHandler; |
10
|
|
|
|
11
|
|
|
use Generator; |
12
|
|
|
use RuntimeException; |
13
|
|
|
use Tebru\Retrofit\Http\MultipartBody; |
14
|
|
|
use Tebru\Retrofit\ParameterHandler; |
15
|
|
|
use Tebru\Retrofit\Internal\RequestBuilder; |
16
|
|
|
use Tebru\Retrofit\RequestBodyConverter; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class AbstractFieldHandler |
20
|
|
|
* |
21
|
|
|
* @author Nate Brunette <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
abstract class AbstractParameterHandler implements ParameterHandler |
24
|
|
|
{ |
25
|
|
|
private const HEADER_CON_TRANS_ENC = 'Content-Transfer-Encoding'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Convert a value to a generator |
29
|
|
|
* |
30
|
|
|
* This method is used when a value can optionally be an array and each element in the |
31
|
|
|
* array should be processed the same way. |
32
|
|
|
* |
33
|
|
|
* @param array|mixed $list |
34
|
|
|
* @return Generator |
35
|
|
|
* @throws \RuntimeException |
36
|
|
|
*/ |
37
|
20 |
|
protected function getListValues($list): Generator |
38
|
|
|
{ |
39
|
20 |
|
foreach ((array)$list as $key => $element) { |
40
|
20 |
|
if (!\is_int($key)) { |
41
|
1 |
|
throw new RuntimeException('Retrofit: Array value must use numeric keys'); |
42
|
|
|
} |
43
|
|
|
|
44
|
19 |
|
yield $element; |
45
|
|
|
} |
46
|
19 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Handle Part or PartMap annotations |
50
|
|
|
* |
51
|
|
|
* This could use a simple method using name and value, or if a [@see MultipartBody] is passed in as the |
52
|
|
|
* value, then a filename and additional headers could be set as well. |
53
|
|
|
* |
54
|
|
|
* @param RequestBuilder $requestBuilder |
55
|
|
|
* @param RequestBodyConverter $converter |
56
|
|
|
* @param string $name |
57
|
|
|
* @param mixed $value |
58
|
|
|
* @param string $encoding |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
6 |
|
protected function handlePart( |
62
|
|
|
RequestBuilder $requestBuilder, |
63
|
|
|
RequestBodyConverter $converter, |
64
|
|
|
string $name, |
65
|
|
|
$value, |
66
|
|
|
string $encoding |
67
|
|
|
): void { |
68
|
6 |
|
if ($value === null) { |
69
|
2 |
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// if not a MultipartBody, only set name, contents, and content header |
73
|
6 |
|
if (!$value instanceof MultipartBody) { |
74
|
4 |
|
$requestBuilder->addPart($name, $converter->convert($value), [self::HEADER_CON_TRANS_ENC => $encoding]); |
75
|
4 |
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
5 |
|
$headers = $value->getHeaders(); |
79
|
5 |
|
if (!isset($headers[self::HEADER_CON_TRANS_ENC])) { |
80
|
5 |
|
$headers[self::HEADER_CON_TRANS_ENC] = $encoding; |
81
|
|
|
} |
82
|
|
|
|
83
|
5 |
|
$requestBuilder->addPart($value->getName(), $value->getContents(), $headers, $value->getFilename()); |
84
|
5 |
|
} |
85
|
|
|
} |
86
|
|
|
|