-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
147 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
namespace App\Pagination; | ||
|
||
use Doctrine\ORM\QueryBuilder as DoctrineQueryBuilder; | ||
use Doctrine\ORM\Tools\Pagination\CountWalker; | ||
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator; | ||
|
||
final class Paginator | ||
{ | ||
final public const PAGE_SIZE = 10; | ||
|
||
private int $currentPage; | ||
private int $numResults; | ||
|
||
/** | ||
* @var \Traversable<int, object> | ||
*/ | ||
private \Traversable $results; | ||
|
||
public function __construct( | ||
private readonly DoctrineQueryBuilder $queryBuilder, | ||
private readonly int $pageSize = self::PAGE_SIZE | ||
) { | ||
} | ||
|
||
public function paginate(int $page = 1): self | ||
{ | ||
$this->currentPage = max(1, $page); | ||
$firstResult = ($this->currentPage - 1) * $this->pageSize; | ||
|
||
$query = $this->queryBuilder | ||
->setFirstResult($firstResult) | ||
->setMaxResults($this->pageSize) | ||
->getQuery(); | ||
|
||
// @INFO: Set Hydration to Array, to fit on JSON response | ||
$query->setHydrationMode(\Doctrine\ORM\Query::HYDRATE_ARRAY); | ||
|
||
/** @var array<string, mixed> $joinDqlParts */ | ||
$joinDqlParts = $this->queryBuilder->getDQLPart('join'); | ||
|
||
if (0 === \count($joinDqlParts)) { | ||
$query->setHint(CountWalker::HINT_DISTINCT, false); | ||
} | ||
|
||
$paginator = new DoctrinePaginator($query, true); | ||
|
||
/** @var array<string, mixed> $havingDqlParts */ | ||
$havingDqlParts = $this->queryBuilder->getDQLPart('having'); | ||
|
||
$useOutputWalkers = \count($havingDqlParts ?: []) > 0; | ||
$paginator->setUseOutputWalkers($useOutputWalkers); | ||
|
||
$this->results = $paginator->getIterator(); | ||
$this->numResults = $paginator->count(); | ||
|
||
return $this; | ||
} | ||
|
||
public function getCurrentPage(): int | ||
{ | ||
return $this->currentPage; | ||
} | ||
|
||
public function getLastPage(): int | ||
{ | ||
return (int) ceil($this->numResults / $this->pageSize); | ||
} | ||
|
||
public function getPageSize(): int | ||
{ | ||
return $this->pageSize; | ||
} | ||
|
||
public function hasPreviousPage(): bool | ||
{ | ||
return $this->currentPage > 1; | ||
} | ||
|
||
public function getPreviousPage(): int | ||
{ | ||
return max(1, $this->currentPage - 1); | ||
} | ||
|
||
public function hasNextPage(): bool | ||
{ | ||
return $this->currentPage < $this->getLastPage(); | ||
} | ||
|
||
public function getNextPage(): int | ||
{ | ||
return min($this->getLastPage(), $this->currentPage + 1); | ||
} | ||
|
||
public function hasToPaginate(): bool | ||
{ | ||
return $this->numResults > $this->pageSize; | ||
} | ||
|
||
public function getNumResults(): int | ||
{ | ||
return $this->numResults; | ||
} | ||
|
||
/** | ||
* @return \Traversable<int, object> | ||
*/ | ||
public function getResults(): \Traversable | ||
{ | ||
return $this->results; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters