2
2
3
3
namespace Tobyz \JsonApiServer \Endpoint ;
4
4
5
- use Closure ;
6
5
use Psr \Http \Message \ResponseInterface as Response ;
7
6
use RuntimeException ;
8
7
use Tobyz \JsonApiServer \Context ;
13
12
use Tobyz \JsonApiServer \Exception \MethodNotAllowedException ;
14
13
use Tobyz \JsonApiServer \Exception \Sourceable ;
15
14
use Tobyz \JsonApiServer \OpenApi \OpenApiPathsProvider ;
15
+ use Tobyz \JsonApiServer \Pagination \CursorPagination ;
16
16
use Tobyz \JsonApiServer \Pagination \OffsetPagination ;
17
+ use Tobyz \JsonApiServer \Pagination \Pagination ;
17
18
use Tobyz \JsonApiServer \Resource \Collection ;
18
19
use Tobyz \JsonApiServer \Resource \Countable ;
19
20
use Tobyz \JsonApiServer \Resource \Listable ;
@@ -34,26 +35,24 @@ class Index implements Endpoint, OpenApiPathsProvider
34
35
use HasDescription;
35
36
use BuildsOpenApiPaths;
36
37
37
- public Closure $ paginationResolver ;
38
+ public ? Pagination $ pagination = null ;
38
39
public ?string $ defaultSort = null ;
39
40
40
- public function __construct ()
41
+ public static function make (): static
41
42
{
42
- $ this -> paginationResolver = fn () => null ;
43
+ return new static () ;
43
44
}
44
45
45
- public static function make ( ): static
46
+ public function paginate ( int $ defaultLimit = 20 , ? int $ maxLimit = 50 ): static
46
47
{
47
- return new static ();
48
+ $ this ->pagination = new OffsetPagination ($ defaultLimit , $ maxLimit );
49
+
50
+ return $ this ;
48
51
}
49
52
50
- public function paginate (int $ defaultLimit = 20 , int $ maxLimit = 50 ): static
53
+ public function cursorPaginate (int $ defaultSize = 20 , ? int $ maxSize = 50 ): static
51
54
{
52
- $ this ->paginationResolver = fn (Context $ context ) => new OffsetPagination (
53
- $ context ,
54
- $ defaultLimit ,
55
- $ maxLimit ,
56
- );
55
+ $ this ->pagination = new CursorPagination ($ defaultSize , $ maxSize );
57
56
58
57
return $ this ;
59
58
}
@@ -94,22 +93,19 @@ public function handle(Context $context): ?Response
94
93
$ this ->applySorts ($ query , $ context );
95
94
$ this ->applyFilters ($ query , $ context );
96
95
97
- $ meta = $ this ->serializeMeta ($ context );
98
- $ links = [];
99
-
100
96
if (
101
97
$ collection instanceof Countable &&
102
98
!is_null ($ total = $ collection ->count ($ query , $ context ))
103
99
) {
104
- $ meta ['page ' ]['total ' ] = $ collection -> count ( $ query , $ context ) ;
100
+ $ context -> documentMeta ['page ' ]['total ' ] = $ total ;
105
101
}
106
102
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 );
109
107
}
110
108
111
- $ models = $ collection ->results ($ query , $ context );
112
-
113
109
$ serializer = new Serializer ($ context );
114
110
115
111
$ include = $ this ->getInclude ($ context );
@@ -124,11 +120,14 @@ public function handle(Context $context): ?Response
124
120
125
121
[$ data , $ included ] = $ serializer ->serialize ();
126
122
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 ;
130
127
}
131
128
129
+ $ links = $ context ->documentLinks ;
130
+
132
131
return json_api_response (compact ('data ' , 'included ' , 'meta ' , 'links ' ));
133
132
}
134
133
0 commit comments