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