1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of JSON-API. |
5
|
|
|
* |
6
|
|
|
* (c) Toby Zerner <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tobscure\JsonApi; |
13
|
|
|
|
14
|
|
|
trait PaginationLinksTrait |
15
|
|
|
{ |
16
|
|
|
abstract public function setLink($key, $value); |
|
|
|
|
17
|
|
|
|
18
|
|
|
abstract public function removeLink($key); |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Set pagination links (first, prev, next, and last). |
22
|
|
|
* |
23
|
|
|
* @param string $url The base URL for pagination links. |
24
|
|
|
* @param array $queryParams The query params provided in the request. |
25
|
|
|
* @param int $offset The current offset. |
26
|
|
|
* @param int $limit The current limit. |
27
|
|
|
* @param int|null $total The total number of results, or null if unknown. |
28
|
|
|
*/ |
29
|
|
|
public function setPaginationLinks($url, array $queryParams, $offset, $limit, $total = null) |
30
|
|
|
{ |
31
|
|
|
if (isset($queryParams['page']['number'])) { |
32
|
|
|
$offset = floor($offset / $limit) * $limit; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->setPaginationLink('first', $url, $queryParams, 0, $limit); |
36
|
|
|
|
37
|
|
|
$this->removeLink('prev'); |
38
|
|
|
$this->removeLink('next'); |
39
|
|
|
$this->removeLink('last'); |
40
|
|
|
|
41
|
|
|
if ($offset > 0) { |
42
|
|
|
$this->setPaginationLink('prev', $url, $queryParams, max(0, $offset - $limit), $limit); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($total === null || $offset + $limit < $total) { |
46
|
|
|
$this->setPaginationLink('next', $url, $queryParams, $offset + $limit, $limit); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($total) { |
|
|
|
|
50
|
|
|
$this->setPaginationLink('last', $url, $queryParams, floor(($total - 1) / $limit) * $limit, $limit); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set a pagination link. |
56
|
|
|
* |
57
|
|
|
* @param string $name The name of the link. |
58
|
|
|
* @param string $url The base URL for pagination links. |
59
|
|
|
* @param array $queryParams The query params provided in the request. |
60
|
|
|
* @param int $offset The offset to link to. |
61
|
|
|
* @param int $limit The current limit. |
62
|
|
|
*/ |
63
|
|
|
private function setPaginationLink($name, $url, array $queryParams, $offset, $limit) |
64
|
|
|
{ |
65
|
|
|
if (! isset($queryParams['page']) || ! is_array($queryParams['page'])) { |
66
|
|
|
$queryParams['page'] = []; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$page = &$queryParams['page']; |
70
|
|
|
|
71
|
|
|
if (isset($page['number'])) { |
72
|
|
|
$page['number'] = floor($offset / $limit) + 1; |
73
|
|
|
|
74
|
|
|
if ($page['number'] <= 1) { |
75
|
|
|
unset($page['number']); |
76
|
|
|
} |
77
|
|
|
} else { |
78
|
|
|
$page['offset'] = $offset; |
79
|
|
|
|
80
|
|
|
if ($page['offset'] <= 0) { |
81
|
|
|
unset($page['offset']); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (isset($page['limit'])) { |
86
|
|
|
$page['limit'] = $limit; |
87
|
|
|
} elseif (isset($page['size'])) { |
88
|
|
|
$page['size'] = $limit; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$queryString = http_build_query($queryParams); |
92
|
|
|
|
93
|
|
|
$this->setLink($name, $url.($queryString ? '?'.$queryString : '')); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@return
doc comment to communicate to implementors of these methods what they are expected to return.