22
33namespace Tobyz \JsonApiServer \Endpoint ;
44
5- use Closure ;
65use Psr \Http \Message \ResponseInterface as Response ;
76use RuntimeException ;
87use Tobyz \JsonApiServer \Context ;
1312use Tobyz \JsonApiServer \Exception \MethodNotAllowedException ;
1413use Tobyz \JsonApiServer \Exception \Sourceable ;
1514use Tobyz \JsonApiServer \OpenApi \OpenApiPathsProvider ;
15+ use Tobyz \JsonApiServer \Pagination \CursorPagination ;
1616use Tobyz \JsonApiServer \Pagination \OffsetPagination ;
17+ use Tobyz \JsonApiServer \Pagination \Pagination ;
1718use Tobyz \JsonApiServer \Resource \Collection ;
1819use Tobyz \JsonApiServer \Resource \Countable ;
1920use Tobyz \JsonApiServer \Resource \Listable ;
@@ -34,26 +35,24 @@ class Index implements Endpoint, OpenApiPathsProvider
3435 use HasDescription;
3536 use BuildsOpenApiPaths;
3637
37- public Closure $ paginationResolver ;
38+ public ? Pagination $ pagination = null ;
3839 public ?string $ defaultSort = null ;
3940
40- public function __construct ()
41+ public static function make (): static
4142 {
42- $ this -> paginationResolver = fn () => null ;
43+ return new static () ;
4344 }
4445
45- public static function make ( ): static
46+ public function paginate ( int $ defaultLimit = 20 , ? int $ maxLimit = 50 ): static
4647 {
47- return new static ();
48+ $ this ->pagination = new OffsetPagination ($ defaultLimit , $ maxLimit );
49+
50+ return $ this ;
4851 }
4952
50- public function paginate (int $ defaultLimit = 20 , int $ maxLimit = 50 ): static
53+ public function cursorPaginate (int $ defaultSize = 20 , ? int $ maxSize = 50 ): static
5154 {
52- $ this ->paginationResolver = fn (Context $ context ) => new OffsetPagination (
53- $ context ,
54- $ defaultLimit ,
55- $ maxLimit ,
56- );
55+ $ this ->pagination = new CursorPagination ($ defaultSize , $ maxSize );
5756
5857 return $ this ;
5958 }
@@ -94,22 +93,19 @@ public function handle(Context $context): ?Response
9493 $ this ->applySorts ($ query , $ context );
9594 $ this ->applyFilters ($ query , $ context );
9695
97- $ meta = $ this ->serializeMeta ($ context );
98- $ links = [];
99-
10096 if (
10197 $ collection instanceof Countable &&
10298 !is_null ($ total = $ collection ->count ($ query , $ context ))
10399 ) {
104- $ meta ['page ' ]['total ' ] = $ collection -> count ( $ query , $ context ) ;
100+ $ context -> documentMeta ['page ' ]['total ' ] = $ total ;
105101 }
106102
107- if ($ pagination = ($ this ->paginationResolver )($ context )) {
108- $ pagination ->apply ($ query );
103+ if ($ this ->pagination ) {
104+ $ models = $ this ->pagination ->paginate ($ query , $ context );
105+ } else {
106+ $ models = $ collection ->results ($ query , $ context );
109107 }
110108
111- $ models = $ collection ->results ($ query , $ context );
112-
113109 $ serializer = new Serializer ($ context );
114110
115111 $ include = $ this ->getInclude ($ context );
@@ -124,11 +120,14 @@ public function handle(Context $context): ?Response
124120
125121 [$ data , $ included ] = $ serializer ->serialize ();
126122
127- if ($ pagination ) {
128- $ meta ['page ' ] = array_merge ($ meta ['page ' ] ?? [], $ pagination ->meta ());
129- $ links = array_merge ($ links , $ pagination ->links (count ($ data ), $ total ?? null ));
123+ $ meta = $ context ->documentMeta ;
124+
125+ foreach ($ this ->serializeMeta ($ context ) as $ key => $ value ) {
126+ $ meta [$ key ] = $ value ;
130127 }
131128
129+ $ links = $ context ->documentLinks ;
130+
132131 return json_api_response (compact ('data ' , 'included ' , 'meta ' , 'links ' ));
133132 }
134133
0 commit comments