|
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
|
|
|
protected function getListValues($list): Generator |
|
38
|
|
|
{ |
|
39
|
|
|
foreach ((array)$list as $key => $element) { |
|
40
|
|
|
if (!is_int($key)) { |
|
41
|
|
|
throw new RuntimeException('Retrofit: Array value must use numeric keys'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
yield $element; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
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
|
|
|
protected function handlePart( |
|
62
|
|
|
RequestBuilder $requestBuilder, |
|
63
|
|
|
RequestBodyConverter $converter, |
|
64
|
|
|
string $name, |
|
65
|
|
|
$value, |
|
66
|
|
|
string $encoding |
|
67
|
|
|
): void { |
|
68
|
|
|
if ($value === null) { |
|
69
|
|
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// if not a MultipartBody, only set name, contents, and content header |
|
73
|
|
|
if (!$value instanceof MultipartBody) { |
|
74
|
|
|
$requestBuilder->addPart($name, $converter->convert($value), [self::HEADER_CON_TRANS_ENC => $encoding]); |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$headers = $value->getHeaders(); |
|
79
|
|
|
if (!isset($headers[self::HEADER_CON_TRANS_ENC])) { |
|
80
|
|
|
$headers[self::HEADER_CON_TRANS_ENC] = $encoding; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$requestBuilder->addPart($value->getName(), $value->getContents(), $headers, $value->getFilename()); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: