|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (c) 2015 Nate Brunette. |
|
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Tebru\Retrofit\Generation\Handler; |
|
8
|
|
|
|
|
9
|
|
|
use Tebru\Retrofit\Generation\Handler; |
|
10
|
|
|
use Tebru\Retrofit\Generation\HandlerContext; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class HeaderHandler |
|
14
|
|
|
* |
|
15
|
|
|
* @author Nate Brunette <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class HeaderHandler implements Handler |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Create header |
|
21
|
|
|
* |
|
22
|
|
|
* @param HandlerContext $context |
|
23
|
|
|
* @return null |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __invoke(HandlerContext $context) |
|
26
|
|
|
{ |
|
27
|
|
|
$varHeaders = $context->annotations()->getHeaders(); |
|
28
|
|
|
$staticHeaders = $context->annotations()->getStaticHeaders(); |
|
29
|
|
|
|
|
30
|
|
|
$headers = $this->getContentTypeHeader($context); |
|
31
|
|
|
|
|
32
|
|
|
if (null !== $staticHeaders) { |
|
33
|
|
|
$headers = array_merge($headers, $staticHeaders); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
if (null !== $varHeaders) { |
|
37
|
|
|
$headers = array_merge($headers, $varHeaders); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$context->body()->add('$headers = %s;', $context->printer()->printArray($headers)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get content type and set to header |
|
45
|
|
|
* |
|
46
|
|
|
* @param HandlerContext $context |
|
47
|
|
|
* @return array |
|
48
|
|
|
*/ |
|
49
|
|
|
private function getContentTypeHeader(HandlerContext $context) |
|
50
|
|
|
{ |
|
51
|
|
|
if ($context->annotations()->isJsonEncoded()) { |
|
52
|
|
|
return ['Content-Type' => 'application/json']; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if ($context->annotations()->isFormUrlEncoded()) { |
|
56
|
|
|
return ['Content-Type' => 'application/x-www-form-urlencoded']; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if ($context->annotations()->isMultipart()) { |
|
60
|
|
|
return ['Content-Type' => 'multipart/form-data; boundary=' . $context->annotations()->getMultipartBoundary()]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$contentType = ['Content-Type' => 'application/x-www-form-urlencoded']; |
|
64
|
|
|
|
|
65
|
|
|
if (!$context->annotations()->hasBody()) { |
|
66
|
|
|
return $contentType; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
trigger_error('Retrofit Deprecation: The default content type is changing in the next' |
|
70
|
|
|
. ' major version of Retrofit from application/x-www-form-urlencoded' |
|
71
|
|
|
. ' to application/json. In order to ensure a clean upgrade, make' |
|
72
|
|
|
. ' sure you specify a specific content type with the proper annotation', |
|
73
|
|
|
E_USER_DEPRECATED |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
return $contentType; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|