-
-
Notifications
You must be signed in to change notification settings - Fork 829
/
SerializeAction.php
210 lines (180 loc) · 5.87 KB
/
SerializeAction.php
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\Actions;
use Flarum\Events\BuildApiAction;
use Flarum\Events\WillSerializeData;
use Flarum\Api\Request;
use Flarum\Api\JsonApiRequest;
use Tobscure\JsonApi\Criteria;
use Tobscure\JsonApi\Document;
use Tobscure\JsonApi\SerializerInterface;
use Zend\Diactoros\Response\JsonResponse;
abstract class SerializeAction implements Action
{
/**
* The name of the serializer class to output results with.
*
* @var string
*/
public $serializer;
/**
* The relationships that are available to be included (keys), and which
* ones are included by default (boolean values).
*
* @var array
*/
public $include = [];
/**
* The relationships that are linked by default.
*
* @var array
*/
public $link = [];
/**
* The maximum number of records that can be requested.
*
* @var integer
*/
public $limitMax = 50;
/**
* The number of records included by default.
*
* @var integer
*/
public $limit = 20;
/**
* The fields that are available to be sorted by.
*
* @var array
*/
public $sortFields = [];
/**
* The default sort field and order to user.
*
* @var string
*/
public $sort;
/**
* Handle an API request and return an API response.
*
* @param Request $request
* @return JsonResponse
*/
public function handle(Request $request)
{
$request = $this->buildJsonApiRequest($request);
$document = new Document();
$data = $this->data($request, $document);
event(new WillSerializeData($this, $data, $request));
$serializer = new $this->serializer($request->actor, $request->include, $request->link);
$document->setData($this->serialize($serializer, $data));
return new JsonResponse($document, 200, ['content-type' => 'application/vnd.api+json']);
}
/**
* Get the data to be serialized and assigned to the response document.
*
* @param JsonApiRequest $request
* @param Document $document
* @return array
*/
abstract protected function data(JsonApiRequest $request, Document $document);
/**
* Serialize the data as appropriate.
*
* @param SerializerInterface $serializer
* @param array $data
* @return \Tobscure\JsonApi\Elements\ElementInterface
*/
abstract protected function serialize(SerializerInterface $serializer, $data);
/**
* Extract parameters from the request input and assign them to the
* request, restricted by the action's specifications.
*
* @param Request $request
* @return JsonApiRequest
*/
protected function buildJsonApiRequest(Request $request)
{
$request = new JsonApiRequest($request->input, $request->actor, $request->http);
$criteria = new Criteria($request->input);
event(new BuildApiAction($this));
$request->include = $this->sanitizeInclude($criteria->getInclude());
$request->sort = $this->sanitizeSort($criteria->getSort());
$request->offset = $criteria->getOffset();
$request->limit = $this->sanitizeLimit($criteria->getLimit());
$request->link = $this->link;
return $request;
}
/**
* Sanitize an array of included relationships according to the action's
* configuration.
*
* @param array $include
* @return array
*/
protected function sanitizeInclude(array $include)
{
return array_intersect($include, array_keys($this->include)) ?: array_keys(array_filter($this->include));
}
/**
* Sanitize an array of sort criteria according to the action's
* configuration.
*
* @param array $sort
* @return array
*/
protected function sanitizeSort(array $sort)
{
return array_intersect_key($sort, array_flip($this->sortFields)) ?: $this->sort;
}
/**
* Sanitize a limit according to the action's configuration.
*
* @param int $limit
* @return int
*/
protected function sanitizeLimit($limit)
{
return min($limit, $this->limitMax) ?: $this->limit;
}
/**
* Add pagination links to a JSON-API response, based on input parameters
* and the default parameters of this action.
*
* @param Document $document
* @param JsonApiRequest $request
* @param string $url The base URL to build pagination links with.
* @param integer|boolean $total The total number of results (used to build
* a 'last' link), or just true if there are more results but how many
* is unknown ('last' link is ommitted).
* @return void
*/
protected function addPaginationLinks(Document $document, JsonApiRequest $request, $url, $total = true)
{
$input = [];
if ($request->limit != $this->limit) {
array_set($input, 'page.limit', $request->limit);
}
array_set($input, 'page.offset', 0);
$document->addLink('first', $url.'?'.http_build_query($input));
if ($request->offset > 0) {
array_set($input, 'page.offset', max(0, $request->offset - $request->limit));
$document->addLink('prev', $url.'?'.http_build_query($input));
}
if ($total === true || $request->offset + $request->limit < $total) {
array_set($input, 'page.offset', $request->offset + $request->limit);
$document->addLink('next', $url.'?'.http_build_query($input));
}
if ($total && $total !== true) {
array_set($input, 'page.offset', $total - $request->limit);
$document->addLink('last', $url.'?'.http_build_query($input));
}
}
}