@@ -62,7 +62,7 @@ public function normalize($object, $format = null, array $context = [])
62
62
}
63
63
64
64
$ currentPage = $ lastPage = $ itemsPerPage = $ pageTotalItems = null ;
65
- if ($ paginated = $ object instanceof PartialPaginatorInterface) {
65
+ if ($ paginated = ( $ object instanceof PartialPaginatorInterface) ) {
66
66
if ($ object instanceof PaginatorInterface) {
67
67
$ paginated = 1. !== $ lastPage = $ object ->getLastPage ();
68
68
} else {
@@ -81,41 +81,20 @@ public function normalize($object, $format = null, array $context = [])
81
81
return $ data ;
82
82
}
83
83
84
+ $ cursorPaginationAttribute = null ;
84
85
$ metadata = isset ($ context ['resource_class ' ]) && null !== $ this ->resourceMetadataFactory ? $ this ->resourceMetadataFactory ->create ($ context ['resource_class ' ]) : null ;
85
86
$ isPaginatedWithCursor = $ paginated && null !== $ metadata && null !== $ cursorPaginationAttribute = $ metadata ->getCollectionOperationAttribute ($ context ['collection_operation_name ' ] ?? $ context ['subresource_operation_name ' ], 'pagination_via_cursor ' , null , true );
86
87
87
- $ data ['hydra:view ' ] = [
88
- '@id ' => IriHelper::createIri ($ parsed ['parts ' ], $ parsed ['parameters ' ], $ this ->pageParameterName , $ paginated && !$ isPaginatedWithCursor ? $ currentPage : null ),
89
- '@type ' => 'hydra:PartialCollectionView ' ,
90
- ];
88
+ $ data ['hydra:view ' ] = ['@id ' => null , '@type ' => 'hydra:PartialCollectionView ' ];
91
89
92
90
if ($ isPaginatedWithCursor ) {
93
- $ objects = iterator_to_array ($ object );
94
- $ firstObject = current ($ objects );
95
- $ lastObject = end ($ objects );
96
-
97
- $ data ['hydra:view ' ]['@id ' ] = IriHelper::createIri ($ parsed ['parts ' ], $ parsed ['parameters ' ]);
98
-
99
- if (false !== $ lastObject && isset ($ cursorPaginationAttribute )) {
100
- $ data ['hydra:view ' ]['hydra:next ' ] = IriHelper::createIri ($ parsed ['parts ' ], array_merge ($ parsed ['parameters ' ], $ this ->cursorPaginationFields ($ cursorPaginationAttribute , 1 , $ lastObject )));
101
- }
102
-
103
- if (false !== $ firstObject && isset ($ cursorPaginationAttribute )) {
104
- $ data ['hydra:view ' ]['hydra:previous ' ] = IriHelper::createIri ($ parsed ['parts ' ], array_merge ($ parsed ['parameters ' ], $ this ->cursorPaginationFields ($ cursorPaginationAttribute , -1 , $ firstObject )));
105
- }
106
- } elseif ($ paginated ) {
107
- if (null !== $ lastPage ) {
108
- $ data ['hydra:view ' ]['hydra:first ' ] = IriHelper::createIri ($ parsed ['parts ' ], $ parsed ['parameters ' ], $ this ->pageParameterName , 1. );
109
- $ data ['hydra:view ' ]['hydra:last ' ] = IriHelper::createIri ($ parsed ['parts ' ], $ parsed ['parameters ' ], $ this ->pageParameterName , $ lastPage );
110
- }
91
+ return $ this ->populateDataWithCursorBasedPagination ($ data , $ parsed , $ object , $ cursorPaginationAttribute );
92
+ }
111
93
112
- if (1. !== $ currentPage ) {
113
- $ data ['hydra:view ' ]['hydra:previous ' ] = IriHelper::createIri ($ parsed ['parts ' ], $ parsed ['parameters ' ], $ this ->pageParameterName , $ currentPage - 1. );
114
- }
94
+ $ data ['hydra:view ' ]['@id ' ] = IriHelper::createIri ($ parsed ['parts ' ], $ parsed ['parameters ' ], $ this ->pageParameterName , $ paginated ? $ currentPage : null );
115
95
116
- if (null !== $ lastPage && $ currentPage < $ lastPage || null === $ lastPage && $ pageTotalItems >= $ itemsPerPage ) {
117
- $ data ['hydra:view ' ]['hydra:next ' ] = IriHelper::createIri ($ parsed ['parts ' ], $ parsed ['parameters ' ], $ this ->pageParameterName , $ currentPage + 1. );
118
- }
96
+ if ($ paginated ) {
97
+ return $ this ->populateDataWithPagination ($ data , $ parsed , $ currentPage , $ lastPage , $ itemsPerPage , $ pageTotalItems );
119
98
}
120
99
121
100
return $ data ;
@@ -164,4 +143,41 @@ private function cursorPaginationFields(array $fields, int $direction, $object)
164
143
165
144
return $ paginationFilters ;
166
145
}
146
+
147
+ private function populateDataWithCursorBasedPagination (array $ data , array $ parsed , \Traversable $ object , $ cursorPaginationAttribute ): array
148
+ {
149
+ $ objects = iterator_to_array ($ object );
150
+ $ firstObject = current ($ objects );
151
+ $ lastObject = end ($ objects );
152
+
153
+ $ data ['hydra:view ' ]['@id ' ] = IriHelper::createIri ($ parsed ['parts ' ], $ parsed ['parameters ' ]);
154
+
155
+ if (false !== $ lastObject && \is_array ($ cursorPaginationAttribute )) {
156
+ $ data ['hydra:view ' ]['hydra:next ' ] = IriHelper::createIri ($ parsed ['parts ' ], array_merge ($ parsed ['parameters ' ], $ this ->cursorPaginationFields ($ cursorPaginationAttribute , 1 , $ lastObject )));
157
+ }
158
+
159
+ if (false !== $ firstObject && \is_array ($ cursorPaginationAttribute )) {
160
+ $ data ['hydra:view ' ]['hydra:previous ' ] = IriHelper::createIri ($ parsed ['parts ' ], array_merge ($ parsed ['parameters ' ], $ this ->cursorPaginationFields ($ cursorPaginationAttribute , -1 , $ firstObject )));
161
+ }
162
+
163
+ return $ data ;
164
+ }
165
+
166
+ private function populateDataWithPagination (array $ data , array $ parsed , ?float $ currentPage , ?float $ lastPage , ?float $ itemsPerPage , ?float $ pageTotalItems ): array
167
+ {
168
+ if (null !== $ lastPage ) {
169
+ $ data ['hydra:view ' ]['hydra:first ' ] = IriHelper::createIri ($ parsed ['parts ' ], $ parsed ['parameters ' ], $ this ->pageParameterName , 1. );
170
+ $ data ['hydra:view ' ]['hydra:last ' ] = IriHelper::createIri ($ parsed ['parts ' ], $ parsed ['parameters ' ], $ this ->pageParameterName , $ lastPage );
171
+ }
172
+
173
+ if (1. !== $ currentPage ) {
174
+ $ data ['hydra:view ' ]['hydra:previous ' ] = IriHelper::createIri ($ parsed ['parts ' ], $ parsed ['parameters ' ], $ this ->pageParameterName , $ currentPage - 1. );
175
+ }
176
+
177
+ if ((null !== $ lastPage && $ currentPage < $ lastPage ) || (null === $ lastPage && null === $ lastPage && $ pageTotalItems >= $ itemsPerPage )) {
178
+ $ data ['hydra:view ' ]['hydra:next ' ] = IriHelper::createIri ($ parsed ['parts ' ], $ parsed ['parameters ' ], $ this ->pageParameterName , $ currentPage + 1. );
179
+ }
180
+
181
+ return $ data ;
182
+ }
167
183
}
0 commit comments