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