5
5
use RuntimeException ;
6
6
use Tobyz \JsonApiServer \Context ;
7
7
use Tobyz \JsonApiServer \Exception \BadRequestException ;
8
- use Tobyz \JsonApiServer \Pagination \Concerns \BuildsUrls ;
9
8
use Tobyz \JsonApiServer \Pagination \Concerns \HasSizeParameter ;
10
9
use Tobyz \JsonApiServer \Resource \Paginatable ;
11
10
12
11
class OffsetPagination implements Pagination
13
12
{
14
13
use HasSizeParameter;
15
- use BuildsUrls;
16
14
17
15
public function __construct (int $ defaultLimit = 20 , ?int $ maxLimit = 50 )
18
16
{
@@ -35,10 +33,7 @@ public function paginate(object $query, Context $context): array
35
33
$ page = $ collection ->paginate ($ query , $ offset , $ limit , $ context );
36
34
37
35
if ($ page ->isFirstPage !== true && $ offset > 0 ) {
38
- $ context ->documentLinks ['first ' ] = $ this ->buildUrl (
39
- ['page ' => ['offset ' => null ]],
40
- $ context ,
41
- );
36
+ $ context ->documentLinks ['first ' ] = $ context ->currentUrl (['page ' => ['offset ' => null ]]);
42
37
43
38
$ prevOffset = $ offset - $ limit ;
44
39
@@ -48,23 +43,21 @@ public function paginate(object $query, Context $context): array
48
43
$ params = ['page ' => ['offset ' => max (0 , $ prevOffset ) ?: null ]];
49
44
}
50
45
51
- $ context ->documentLinks ['prev ' ] = $ this -> buildUrl ($ params, $ context );
46
+ $ context ->documentLinks ['prev ' ] = $ context -> currentUrl ($ params );
52
47
}
53
48
54
49
$ total = $ context ->documentMeta ['page ' ]['total ' ] ?? null ;
55
50
56
51
if ($ total !== null && $ limit && $ offset + $ limit < $ total ) {
57
- $ context ->documentLinks ['last ' ] = $ this ->buildUrl (
58
- ['page ' => ['offset ' => floor (($ total - 1 ) / $ limit ) * $ limit ?: null ]],
59
- $ context ,
60
- );
52
+ $ context ->documentLinks ['last ' ] = $ context ->currentUrl ([
53
+ 'page ' => ['offset ' => floor (($ total - 1 ) / $ limit ) * $ limit ?: null ],
54
+ ]);
61
55
}
62
56
63
57
if (!$ page ->isLastPage ) {
64
- $ context ->documentLinks ['next ' ] = $ this ->buildUrl (
65
- ['page ' => ['offset ' => $ offset + $ limit ]],
66
- $ context ,
67
- );
58
+ $ context ->documentLinks ['next ' ] = $ context ->currentUrl ([
59
+ 'page ' => ['offset ' => $ offset + $ limit ],
60
+ ]);
68
61
}
69
62
70
63
return $ page ->results ;
0 commit comments