55use RuntimeException ;
66use Tobyz \JsonApiServer \Context ;
77use Tobyz \JsonApiServer \Exception \BadRequestException ;
8- use Tobyz \JsonApiServer \Pagination \Concerns \BuildsUrls ;
98use Tobyz \JsonApiServer \Pagination \Concerns \HasSizeParameter ;
109use Tobyz \JsonApiServer \Resource \Paginatable ;
1110
1211class OffsetPagination implements Pagination
1312{
1413 use HasSizeParameter;
15- use BuildsUrls;
1614
1715 public function __construct (int $ defaultLimit = 20 , ?int $ maxLimit = 50 )
1816 {
@@ -35,10 +33,7 @@ public function paginate(object $query, Context $context): array
3533 $ page = $ collection ->paginate ($ query , $ offset , $ limit , $ context );
3634
3735 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 ]]);
4237
4338 $ prevOffset = $ offset - $ limit ;
4439
@@ -48,23 +43,21 @@ public function paginate(object $query, Context $context): array
4843 $ params = ['page ' => ['offset ' => max (0 , $ prevOffset ) ?: null ]];
4944 }
5045
51- $ context ->documentLinks ['prev ' ] = $ this -> buildUrl ($ params, $ context );
46+ $ context ->documentLinks ['prev ' ] = $ context -> currentUrl ($ params );
5247 }
5348
5449 $ total = $ context ->documentMeta ['page ' ]['total ' ] ?? null ;
5550
5651 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+ ]);
6155 }
6256
6357 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+ ]);
6861 }
6962
7063 return $ page ->results ;
0 commit comments