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 LinksTrait |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The links array. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $links; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get the links. |
25
|
|
|
* |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
3 |
|
public function getLinks() |
29
|
|
|
{ |
30
|
3 |
|
return $this->links; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set the links. |
35
|
|
|
* |
36
|
|
|
* @param array $links |
37
|
|
|
* |
38
|
|
|
* @return $this |
39
|
|
|
*/ |
40
|
3 |
|
public function setLinks(array $links) |
41
|
|
|
{ |
42
|
3 |
|
$this->links = $links; |
43
|
|
|
|
44
|
3 |
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Add a link. |
49
|
|
|
* |
50
|
|
|
* @param string $key |
51
|
|
|
* @param string $value |
52
|
|
|
* |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
3 |
|
public function addLink($key, $value) |
56
|
|
|
{ |
57
|
3 |
|
$this->links[$key] = $value; |
58
|
|
|
|
59
|
3 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add pagination links (first, prev, next, and last). |
64
|
|
|
* |
65
|
|
|
* @param string $url The base URL for pagination links. |
66
|
|
|
* @param array $queryParams The query params provided in the request. |
67
|
|
|
* @param int $offset The current offset. |
68
|
|
|
* @param int $limit The current limit. |
69
|
|
|
* @param int|null $total The total number of results, or null if unknown. |
70
|
|
|
* |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
3 |
|
public function addPaginationLinks($url, array $queryParams, $offset, $limit, $total = null) |
74
|
|
|
{ |
75
|
3 |
|
if (isset($queryParams['page']['number'])) { |
76
|
3 |
|
$offset = floor($offset / $limit) * $limit; |
77
|
3 |
|
} |
78
|
|
|
|
79
|
3 |
|
$this->addPaginationLink('first', $url, $queryParams, 0, $limit); |
80
|
|
|
|
81
|
3 |
|
if ($offset > 0) { |
82
|
3 |
|
$this->addPaginationLink('prev', $url, $queryParams, max(0, $offset - $limit), $limit); |
83
|
3 |
|
} |
84
|
|
|
|
85
|
3 |
|
if ($total === null || $offset + $limit < $total) { |
86
|
3 |
|
$this->addPaginationLink('next', $url, $queryParams, $offset + $limit, $limit); |
87
|
3 |
|
} |
88
|
|
|
|
89
|
3 |
|
if ($total) { |
|
|
|
|
90
|
3 |
|
$this->addPaginationLink('last', $url, $queryParams, floor(($total - 1) / $limit) * $limit, $limit); |
91
|
3 |
|
} |
92
|
3 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Add a pagination link. |
96
|
|
|
* |
97
|
|
|
* @param string $name The name of the link. |
98
|
|
|
* @param string $url The base URL for pagination links. |
99
|
|
|
* @param array $queryParams The query params provided in the request. |
100
|
|
|
* @param int $offset The offset to link to. |
101
|
|
|
* @param int $limit The current limit. |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
3 |
|
private function addPaginationLink($name, $url, array $queryParams, $offset, $limit) |
106
|
|
|
{ |
107
|
3 |
|
if (! isset($queryParams['page']) || ! is_array($queryParams['page'])) { |
108
|
3 |
|
$queryParams['page'] = []; |
109
|
3 |
|
} |
110
|
|
|
|
111
|
3 |
|
$page = &$queryParams['page']; |
112
|
|
|
|
113
|
3 |
|
if (isset($page['number'])) { |
114
|
3 |
|
$page['number'] = floor($offset / $limit) + 1; |
115
|
|
|
|
116
|
3 |
|
if ($page['number'] <= 1) { |
117
|
3 |
|
unset($page['number']); |
118
|
3 |
|
} |
119
|
3 |
|
} else { |
120
|
3 |
|
$page['offset'] = $offset; |
121
|
|
|
|
122
|
3 |
|
if ($page['offset'] <= 0) { |
123
|
3 |
|
unset($page['offset']); |
124
|
3 |
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
3 |
|
if (isset($page['limit'])) { |
128
|
3 |
|
$page['limit'] = $limit; |
129
|
3 |
|
} elseif (isset($page['size'])) { |
130
|
3 |
|
$page['size'] = $limit; |
131
|
3 |
|
} |
132
|
|
|
|
133
|
3 |
|
$queryString = http_build_query($queryParams); |
134
|
|
|
|
135
|
3 |
|
$this->addLink($name, $url.($queryString ? '?'.$queryString : '')); |
136
|
3 |
|
} |
137
|
|
|
} |
138
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: