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
|
27 |
|
public function __construct() |
28
|
|
|
{ |
29
|
27 |
|
$this->fractal = new Manager(); |
30
|
27 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
3 |
|
public function params() |
36
|
|
|
{ |
37
|
3 |
|
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
|
12 |
|
public function setEndpointIdentifier(EndpointInterface $endpoint) |
52
|
|
|
{ |
53
|
12 |
|
$this->endpoint = $endpoint; |
54
|
12 |
|
} |
55
|
|
|
|
56
|
6 |
|
public function getEndpoint() |
57
|
|
|
{ |
58
|
6 |
|
return $this->endpoint; |
59
|
|
|
} |
60
|
|
|
|
61
|
6 |
|
public function setupLinkGenerator(LinkGenerator $linkGenerator) |
62
|
|
|
{ |
63
|
6 |
|
$this->linkGenerator = $linkGenerator; |
64
|
6 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
9 |
|
public function createLink($params) |
68
|
|
|
{ |
69
|
9 |
|
if (!$this->linkGenerator) { |
70
|
3 |
|
throw new InvalidStateException("You have setupLinkGenerator for this handler if you want to generate link in this handler"); |
71
|
|
|
} |
72
|
6 |
|
if (!$this->endpoint) { |
73
|
3 |
|
throw new InvalidStateException("You have setEndpoint() for this handler if you want to generate link in this handler"); |
74
|
|
|
} |
75
|
3 |
|
$params = array_merge([ |
76
|
3 |
|
'version' => $this->endpoint->getVersion(), |
77
|
3 |
|
'package' => $this->endpoint->getPackage(), |
78
|
3 |
|
'apiAction' => $this->endpoint->getApiAction() |
79
|
3 |
|
], $params); |
80
|
3 |
|
return $this->linkGenerator->link('Api:Api:default', $params); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
abstract public function handle($params); |
87
|
|
|
} |
88
|
|
|
|