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