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 LogicException; |
10
|
|
|
use Tebru\Retrofit\Generation\Handler; |
11
|
|
|
use Tebru\Retrofit\Generation\HandlerContext; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class UrlHandler |
15
|
|
|
* |
16
|
|
|
* @author Nate Brunette <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class UrlHandler implements Handler |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Handle request url code generation |
22
|
|
|
* |
23
|
|
|
* @param HandlerContext $context |
24
|
|
|
* @return null |
25
|
|
|
* @throws LogicException |
26
|
|
|
*/ |
27
|
|
|
public function __invoke(HandlerContext $context) |
28
|
|
|
{ |
29
|
|
|
$baseUrl = $context->annotations()->getBaseUrl() ?: '$this->baseUrl'; |
30
|
|
|
$queryMap = $context->annotations()->getQueryMap(); |
31
|
|
|
$queries = $context->annotations()->getQueries(); |
32
|
|
|
$uri = $context->annotations()->getRequestUri(); |
33
|
|
|
|
34
|
|
|
// if there aren't queries or a query map, just set request url |
35
|
|
|
if (null === $queryMap && null === $queries) { |
36
|
|
|
$context->body()->add('$requestUrl = %s . "%s";', $baseUrl, $uri); |
37
|
|
|
|
38
|
|
|
return null; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// if there's a query map, check to see if there are also queries |
42
|
|
|
if (null !== $queryMap) { |
43
|
|
|
// if we have regular queries, add them to the query builder |
44
|
|
|
if (null !== $queries) { |
45
|
|
|
$queryArray = $context->printer()->printArray($queries); |
46
|
|
|
$context->body()->add('$queryArray = \Tebru\Retrofit\Generation\Manipulator\QueryManipulator::boolToString(%s + %s);', $queryArray, $queryMap); |
47
|
|
|
$context->body()->add('$queryString = http_build_query($queryArray);'); |
48
|
|
|
} else { |
49
|
|
|
$context->body()->add('$queryArray = \Tebru\Retrofit\Generation\Manipulator\QueryManipulator::boolToString(%s);', $queryMap); |
50
|
|
|
$context->body()->add('$queryString = http_build_query($queryArray);'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$context->body()->add('$requestUrl = %s . "%s?" . $queryString;', $baseUrl, $uri); |
54
|
|
|
|
55
|
|
|
return null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// add queries to request url |
59
|
|
|
$queryArray = $context->printer()->printArray($queries); |
60
|
|
|
$context->body()->add('$queryArray = \Tebru\Retrofit\Generation\Manipulator\QueryManipulator::boolToString(%s);', $queryArray); |
61
|
|
|
$context->body()->add('$queryString = http_build_query($queryArray);'); |
62
|
|
|
$context->body()->add('$requestUrl = %s . "%s" . "?" . $queryString;', $baseUrl, $uri); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|