/
Pagination.php
265 lines (219 loc) · 9.73 KB
/
Pagination.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Core\DataProvider;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
/**
* Pagination configuration.
*
* @author Baptiste Meyer <baptiste.meyer@gmail.com>
*/
final class Pagination
{
private $options;
private $graphQlOptions;
private $resourceMetadataFactory;
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, array $options = [], array $graphQlOptions = [])
{
$this->resourceMetadataFactory = $resourceMetadataFactory;
$this->options = array_merge([
'enabled' => true,
'client_enabled' => false,
'client_items_per_page' => false,
'items_per_page' => 30,
'page_default' => 1,
'page_parameter_name' => 'page',
'enabled_parameter_name' => 'pagination',
'items_per_page_parameter_name' => 'itemsPerPage',
'maximum_items_per_page' => null,
'partial' => false,
'client_partial' => false,
'partial_parameter_name' => 'partial',
], $options);
$this->graphQlOptions = array_merge([
'enabled' => true,
], $graphQlOptions);
}
/**
* Gets the current page.
*
* @throws InvalidArgumentException
*/
public function getPage(array $context = []): int
{
$page = (int) $this->getParameterFromContext(
$context,
$this->options['page_parameter_name'],
$this->options['page_default']
);
if (1 > $page) {
throw new InvalidArgumentException('Page should not be less than 1');
}
return $page;
}
/**
* Gets the current offset.
*/
public function getOffset(string $resourceClass = null, string $operationName = null, array $context = []): int
{
$graphql = (bool) ($context['graphql_operation_name'] ?? false);
$limit = $this->getLimit($resourceClass, $operationName, $context);
if ($graphql && null !== ($after = $this->getParameterFromContext($context, 'after'))) {
return false === ($after = base64_decode($after, true)) ? 0 : (int) $after + 1;
}
if ($graphql && null !== ($before = $this->getParameterFromContext($context, 'before'))) {
return ($offset = (false === ($before = base64_decode($before, true)) ? 0 : (int) $before - $limit)) < 0 ? 0 : $offset;
}
if ($graphql && null !== ($last = $this->getParameterFromContext($context, 'last'))) {
return ($offset = ($context['count'] ?? 0) - $last) < 0 ? 0 : $offset;
}
return ($this->getPage($context) - 1) * $limit;
}
/**
* Gets the current limit.
*
* @throws InvalidArgumentException
*/
public function getLimit(string $resourceClass = null, string $operationName = null, array $context = []): int
{
$graphql = (bool) ($context['graphql_operation_name'] ?? false);
$limit = $this->options['items_per_page'];
$clientLimit = $this->options['client_items_per_page'];
if (null !== $resourceClass) {
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
$limit = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_items_per_page', $limit, true);
$clientLimit = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_items_per_page', $clientLimit, true);
}
if ($graphql && null !== ($first = $this->getParameterFromContext($context, 'first'))) {
$limit = $first;
}
if ($graphql && null !== ($last = $this->getParameterFromContext($context, 'last'))) {
$limit = $last;
}
if ($graphql && null !== ($before = $this->getParameterFromContext($context, 'before'))
&& (false === ($before = base64_decode($before, true)) ? 0 : (int) $before - $limit) < 0) {
$limit = (int) $before;
}
if ($clientLimit) {
$limit = (int) $this->getParameterFromContext($context, $this->options['items_per_page_parameter_name'], $limit);
$maxItemsPerPage = null;
if (null !== $resourceClass) {
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
$maxItemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'maximum_items_per_page', null, true);
if (null !== $maxItemsPerPage) {
@trigger_error('The "maximum_items_per_page" option has been deprecated since API Platform 2.5 in favor of "pagination_maximum_items_per_page" and will be removed in API Platform 3.', E_USER_DEPRECATED);
}
$maxItemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_maximum_items_per_page', $maxItemsPerPage ?? $this->options['maximum_items_per_page'], true);
}
if (null !== $maxItemsPerPage && $limit > $maxItemsPerPage) {
$limit = $maxItemsPerPage;
}
}
if (0 > $limit) {
throw new InvalidArgumentException('Limit should not be less than 0');
}
return $limit;
}
/**
* Gets info about the pagination.
*
* Returns an array with the following info as values:
* - the page {@see Pagination::getPage()}
* - the offset {@see Pagination::getOffset()}
* - the limit {@see Pagination::getLimit()}
*
* @throws InvalidArgumentException
*/
public function getPagination(string $resourceClass = null, string $operationName = null, array $context = []): array
{
$page = $this->getPage($context);
$limit = $this->getLimit($resourceClass, $operationName, $context);
if (0 === $limit && 1 < $page) {
throw new InvalidArgumentException('Page should not be greater than 1 if limit is equal to 0');
}
return [$page, $this->getOffset($resourceClass, $operationName, $context), $limit];
}
/**
* Is the pagination enabled?
*/
public function isEnabled(string $resourceClass = null, string $operationName = null, array $context = []): bool
{
return $this->getEnabled($context, $resourceClass, $operationName);
}
/**
* Is the pagination enabled for GraphQL?
*/
public function isGraphQlEnabled(?string $resourceClass = null, ?string $operationName = null, array $context = []): bool
{
return $this->getGraphQlEnabled($resourceClass, $operationName);
}
/**
* Is the partial pagination enabled?
*/
public function isPartialEnabled(string $resourceClass = null, string $operationName = null, array $context = []): bool
{
return $this->getEnabled($context, $resourceClass, $operationName, true);
}
public function getOptions(): array
{
return $this->options;
}
public function getGraphQlPaginationType(string $resourceClass, string $operationName): string
{
try {
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
} catch (ResourceClassNotFoundException $e) {
return 'cursor';
}
return (string) $resourceMetadata->getGraphqlAttribute($operationName, 'paginationType', 'cursor', true);
}
/**
* Is the classic or partial pagination enabled?
*/
private function getEnabled(array $context, string $resourceClass = null, string $operationName = null, bool $partial = false): bool
{
$enabled = $this->options[$partial ? 'partial' : 'enabled'];
$clientEnabled = $this->options[$partial ? 'client_partial' : 'client_enabled'];
if (null !== $resourceClass) {
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
$enabled = $resourceMetadata->getCollectionOperationAttribute($operationName, $partial ? 'pagination_partial' : 'pagination_enabled', $enabled, true);
$clientEnabled = $resourceMetadata->getCollectionOperationAttribute($operationName, $partial ? 'pagination_client_partial' : 'pagination_client_enabled', $clientEnabled, true);
}
if ($clientEnabled) {
return filter_var($this->getParameterFromContext($context, $this->options[$partial ? 'partial_parameter_name' : 'enabled_parameter_name'], $enabled), FILTER_VALIDATE_BOOLEAN);
}
return (bool) $enabled;
}
private function getGraphQlEnabled(?string $resourceClass, ?string $operationName): bool
{
$enabled = $this->graphQlOptions['enabled'];
if (null !== $resourceClass) {
try {
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
} catch (ResourceClassNotFoundException $e) {
return $enabled;
}
return (bool) $resourceMetadata->getGraphqlAttribute($operationName, 'pagination_enabled', $enabled, true);
}
return $enabled;
}
/**
* Gets the given pagination parameter name from the given context.
*
* @param mixed|null $default
*/
private function getParameterFromContext(array $context, string $parameterName, $default = null)
{
$filters = $context['filters'] ?? [];
return \array_key_exists($parameterName, $filters) ? $filters[$parameterName] : $default;
}
}