|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tomaj\NetteApi\Handlers; |
|
4
|
|
|
|
|
5
|
|
|
use League\Fractal\Manager; |
|
6
|
|
|
use Nette\Application\LinkGenerator; |
|
7
|
|
|
use Nette\InvalidStateException; |
|
8
|
|
|
use Tomaj\NetteApi\EndpointInterface; |
|
9
|
|
|
|
|
10
|
|
|
abstract class BaseHandler implements ApiHandlerInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Manager |
|
14
|
|
|
*/ |
|
15
|
|
|
private $fractal; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var EndpointInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $endpoint; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var LinkGenerator |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $linkGenerator; |
|
26
|
|
|
|
|
27
|
63 |
|
public function __construct() |
|
28
|
|
|
{ |
|
29
|
63 |
|
$this->fractal = new Manager(); |
|
30
|
63 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
9 |
|
public function params() |
|
36
|
|
|
{ |
|
37
|
9 |
|
return []; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function getFractal() |
|
41
|
|
|
{ |
|
42
|
|
|
if (!$this->fractal) { |
|
43
|
|
|
throw new InvalidStateException("Fractal manager isnt initialized. Did you call parent::__construct() in your handler constructor?"); |
|
44
|
|
|
} |
|
45
|
|
|
return $this->fractal; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
30 |
|
public function setEndpointIdentifier(EndpointInterface $endpoint) |
|
52
|
|
|
{ |
|
53
|
30 |
|
$this->endpoint = $endpoint; |
|
54
|
30 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return EndpointInterface |
|
58
|
|
|
*/ |
|
59
|
12 |
|
public function getEndpoint() |
|
60
|
|
|
{ |
|
61
|
12 |
|
return $this->endpoint; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Set link generator to handler |
|
66
|
|
|
* |
|
67
|
|
|
* @param LinkGenerator $linkGenerator |
|
68
|
|
|
* |
|
69
|
|
|
* @return $this |
|
70
|
|
|
*/ |
|
71
|
12 |
|
public function setupLinkGenerator(LinkGenerator $linkGenerator) |
|
72
|
|
|
{ |
|
73
|
12 |
|
$this->linkGenerator = $linkGenerator; |
|
74
|
12 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Create link to actual handler endpoint |
|
79
|
|
|
* |
|
80
|
|
|
* @param array $params |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
* @throws \Nette\Application\UI\InvalidLinkException it handler doesn't have linkgenerator or endpoint |
|
84
|
|
|
*/ |
|
85
|
18 |
|
public function createLink($params) |
|
86
|
|
|
{ |
|
87
|
18 |
|
if (!$this->linkGenerator) { |
|
88
|
6 |
|
throw new InvalidStateException("You have setupLinkGenerator for this handler if you want to generate link in this handler"); |
|
89
|
|
|
} |
|
90
|
12 |
|
if (!$this->endpoint) { |
|
91
|
6 |
|
throw new InvalidStateException("You have setEndpoint() for this handler if you want to generate link in this handler"); |
|
92
|
|
|
} |
|
93
|
6 |
|
$params = array_merge([ |
|
94
|
6 |
|
'version' => $this->endpoint->getVersion(), |
|
95
|
6 |
|
'package' => $this->endpoint->getPackage(), |
|
96
|
6 |
|
'apiAction' => $this->endpoint->getApiAction() |
|
97
|
6 |
|
], $params); |
|
98
|
6 |
|
return $this->linkGenerator->link('Api:Api:default', $params); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
|
|
abstract public function handle($params); |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
*/ |
|
109
|
|
|
public function outputs(): array |
|
110
|
|
|
{ |
|
111
|
|
|
return []; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|