33namespace JoliCode \MediaBundle \Bridge \EasyAdmin \Paginator ;
44
55use EasyCorp \Bundle \EasyAdminBundle \Router \AdminUrlGenerator ;
6+ use JoliCode \MediaBundle \Model \Media ;
67
78/**
89 * Paginator adapter for media files to work with EasyAdmin's pagination template.
1213class MediaPaginator
1314{
1415 private int $ currentPage ;
16+
1517 private int $ pageSize ;
18+
1619 private int $ numResults ;
20+
21+ /**
22+ * @var array<Media>
23+ */
1724 private array $ results ;
25+
1826 private string $ routeName ;
27+
1928 private string $ currentKey ;
2029
2130 public function __construct (
2231 private readonly AdminUrlGenerator $ adminUrlGenerator ,
2332 ) {
2433 }
2534
35+ /**
36+ * @param array{items: array<Media>, total: int, page: int, perPage: int} $paginationData
37+ */
2638 public function paginate (array $ paginationData , string $ routeName , string $ currentKey ): self
2739 {
2840 $ this ->currentPage = $ paginationData ['page ' ];
@@ -40,7 +52,8 @@ public function generateUrlForPage(int $page): string
4052 return $ this ->adminUrlGenerator
4153 ->setRoute ($ this ->routeName , ['key ' => $ this ->currentKey ])
4254 ->set ('page ' , $ page )
43- ->generateUrl ();
55+ ->generateUrl ()
56+ ;
4457 }
4558
4659 public function getCurrentPage (): int
@@ -61,8 +74,8 @@ public function getLastPage(): int
6174 */
6275 public function getPageRange (?int $ pagesOnEachSide = null , ?int $ pagesOnEdges = null ): iterable
6376 {
64- $ pagesOnEachSide = $ pagesOnEachSide ?? 2 ;
65- $ pagesOnEdges = $ pagesOnEdges ?? 1 ;
77+ $ pagesOnEachSide ??= 2 ;
78+ $ pagesOnEdges ??= 1 ;
6679
6780 $ lastPage = $ this ->getLastPage ();
6881 $ currentPage = $ this ->currentPage ;
@@ -82,6 +95,7 @@ public function getPageRange(?int $pagesOnEachSide = null, ?int $pagesOnEdges =
8295 for ($ i = 1 ; $ i <= $ pagesOnEdges ; ++$ i ) {
8396 $ pages [] = $ i ;
8497 }
98+
8599 // Add ellipsis if there's a gap
86100 if ($ startPage > $ pagesOnEdges + 2 ) {
87101 $ pages [] = null ;
@@ -107,6 +121,7 @@ public function getPageRange(?int $pagesOnEachSide = null, ?int $pagesOnEdges =
107121 } elseif ($ endPage === $ lastPage - $ pagesOnEdges - 1 ) {
108122 $ pages [] = $ lastPage - $ pagesOnEdges ;
109123 }
124+
110125 for ($ i = $ lastPage - $ pagesOnEdges + 1 ; $ i <= $ lastPage ; ++$ i ) {
111126 $ pages [] = $ i ;
112127 }
@@ -154,6 +169,9 @@ public function getNumResults(): int
154169 return $ this ->numResults ;
155170 }
156171
172+ /**
173+ * @return array<Media>
174+ */
157175 public function getResults (): array
158176 {
159177 return $ this ->results ;
0 commit comments