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\AnnotationHandler; |
10
|
|
|
|
11
|
|
|
use InvalidArgumentException; |
12
|
|
|
use Tebru\AnnotationReader\AbstractAnnotation; |
13
|
|
|
use Tebru\Retrofit\Annotation\PartMap; |
14
|
|
|
use Tebru\Retrofit\AnnotationHandler; |
15
|
|
|
use Tebru\Retrofit\Converter; |
16
|
|
|
use Tebru\Retrofit\Internal\ParameterHandler\PartMapParamHandler; |
17
|
|
|
use Tebru\Retrofit\RequestBodyConverter; |
18
|
|
|
use Tebru\Retrofit\ServiceMethodBuilder; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class PartMapAnnotHandler |
22
|
|
|
* |
23
|
|
|
* @author Nate Brunette <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
final class PartMapAnnotHandler implements AnnotationHandler |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Add part map param handler |
29
|
|
|
* |
30
|
|
|
* @param PartMap|AbstractAnnotation $annotation The annotation to handle |
31
|
|
|
* @param ServiceMethodBuilder $serviceMethodBuilder Used to construct a [@see ServiceMethod] |
32
|
|
|
* @param Converter|RequestBodyConverter $converter Converter used to convert types before sending to service method |
33
|
|
|
* @param int|null $index The position of the parameter or null if annotation does not reference parameter |
34
|
|
|
* @return void |
35
|
|
|
* @throws \InvalidArgumentException |
36
|
|
|
*/ |
37
|
4 |
|
public function handle( |
38
|
|
|
AbstractAnnotation $annotation, |
39
|
|
|
ServiceMethodBuilder $serviceMethodBuilder, |
40
|
|
|
?Converter $converter, |
41
|
|
|
?int $index |
42
|
|
|
): void { |
43
|
4 |
|
if (!$annotation instanceof PartMap) { |
44
|
1 |
|
throw new InvalidArgumentException('Retrofit: Annotation must be a PartMap'); |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
if (!$converter instanceof RequestBodyConverter) { |
48
|
1 |
|
throw new InvalidArgumentException(sprintf( |
49
|
1 |
|
'Retrofit: Converter must be a RequestBodyConverter, %s found', |
50
|
1 |
|
\gettype($converter) |
51
|
|
|
)); |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
$serviceMethodBuilder->setIsMultipart(); |
55
|
2 |
|
$serviceMethodBuilder->addParameterHandler( |
56
|
2 |
|
$index, |
57
|
2 |
|
new PartMapParamHandler($converter, $annotation->getEncoding()) |
58
|
|
|
); |
59
|
2 |
|
} |
60
|
|
|
} |
61
|
|
|
|