1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tomaj\NetteApi\Handlers; |
4
|
|
|
|
5
|
|
|
use Tomaj\NetteApi\ApiDecider; |
6
|
|
|
use Tomaj\NetteApi\Api; |
7
|
|
|
use Tomaj\NetteApi\Link\ApiLink; |
8
|
|
|
use Tomaj\NetteApi\Params\InputParam; |
9
|
|
|
use Tomaj\NetteApi\Response\JsonApiResponse; |
10
|
|
|
use Tomaj\NetteApi\Response\ResponseInterface; |
11
|
|
|
|
12
|
|
|
class ApiListingHandler extends BaseHandler |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ApiDecider |
16
|
|
|
*/ |
17
|
|
|
private $apiDecider; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ApiLink |
21
|
|
|
*/ |
22
|
|
|
private $apiLink; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* ApiListingHandler constructor. |
26
|
|
|
* |
27
|
|
|
* @param ApiDecider $apiDecider |
28
|
|
|
* @param ApiLink $apiLink |
29
|
|
|
*/ |
30
|
6 |
|
public function __construct(ApiDecider $apiDecider, ApiLink $apiLink) |
31
|
|
|
{ |
32
|
6 |
|
parent::__construct(); |
33
|
6 |
|
$this->apiDecider = $apiDecider; |
34
|
6 |
|
$this->apiLink = $apiLink; |
35
|
6 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
6 |
|
public function handle(array $params): ResponseInterface |
41
|
|
|
{ |
42
|
6 |
|
$version = $this->getEndpoint()->getVersion(); |
43
|
6 |
|
$endpoints = $this->getApiList($version); |
44
|
6 |
|
return new JsonApiResponse(200, ['endpoints' => $endpoints]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Create handler list for specified version |
49
|
|
|
* |
50
|
|
|
* @param integer $version |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
6 |
|
private function getApiList(int $version): array |
55
|
|
|
{ |
56
|
|
|
$versionApis = array_filter($this->apiDecider->getApis(), function (Api $api) use ($version) { |
57
|
6 |
|
return $version == $api->getEndpoint()->getVersion(); |
58
|
6 |
|
}); |
59
|
|
|
|
60
|
|
|
return array_map(function (Api $api) { |
61
|
|
|
return [ |
62
|
6 |
|
'method' => $api->getEndpoint()->getMethod(), |
63
|
6 |
|
'version' => $api->getEndpoint()->getVersion(), |
64
|
6 |
|
'package' => $api->getEndpoint()->getPackage(), |
65
|
6 |
|
'api_action' => $api->getEndpoint()->getApiAction(), |
66
|
6 |
|
'authorization' => get_class($api->getAuthorization()), |
67
|
6 |
|
'url' => $this->apiLink->link($api->getEndpoint()), |
68
|
6 |
|
'params' => $this->createParamsList($api->getHandler()), |
69
|
|
|
]; |
70
|
6 |
|
}, $versionApis); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Create array with params for specified handler |
75
|
|
|
* |
76
|
|
|
* @param ApiHandlerInterface $handler |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
6 |
|
private function createParamsList(ApiHandlerInterface $handler): array |
81
|
|
|
{ |
82
|
|
|
return array_map(function (InputParam $param) { |
83
|
|
|
$parameter = [ |
84
|
3 |
|
'type' => $param->getType(), |
85
|
3 |
|
'key' => $param->getKey(), |
86
|
3 |
|
'is_required' => $param->isRequired(), |
87
|
|
|
]; |
88
|
3 |
|
if ($param->getAvailableValues()) { |
89
|
3 |
|
$parameter['available_values'] = $param->getAvailableValues(); |
90
|
|
|
} |
91
|
3 |
|
return $parameter; |
92
|
6 |
|
}, $handler->params()); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|