mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Encapsulated in VisitsParams how the itemsPerPage param is handled
This commit is contained in:
@@ -8,18 +8,30 @@ use Shlinkio\Shlink\Common\Util\DateRange;
|
||||
|
||||
final class VisitsParams
|
||||
{
|
||||
private const FIRST_PAGE = 1;
|
||||
private const ALL_ITEMS = -1;
|
||||
|
||||
/** @var null|DateRange */
|
||||
private $dateRange;
|
||||
/** @var int */
|
||||
private $page = 1;
|
||||
/** @var null|int */
|
||||
private $page;
|
||||
/** @var int */
|
||||
private $itemsPerPage;
|
||||
|
||||
public function __construct(?DateRange $dateRange = null, int $page = 1, ?int $itemsPerPage = null)
|
||||
public function __construct(?DateRange $dateRange = null, int $page = self::FIRST_PAGE, ?int $itemsPerPage = null)
|
||||
{
|
||||
$this->dateRange = $dateRange ?? new DateRange();
|
||||
$this->page = $page;
|
||||
$this->itemsPerPage = $itemsPerPage;
|
||||
$this->itemsPerPage = $this->determineItemsPerPage($itemsPerPage);
|
||||
}
|
||||
|
||||
private function determineItemsPerPage(?int $itemsPerPage): int
|
||||
{
|
||||
if ($itemsPerPage !== null && $itemsPerPage < 0) {
|
||||
return self::ALL_ITEMS;
|
||||
}
|
||||
|
||||
return $itemsPerPage ?? self::ALL_ITEMS;
|
||||
}
|
||||
|
||||
public static function fromRawData(array $query): self
|
||||
@@ -49,13 +61,8 @@ final class VisitsParams
|
||||
return $this->page;
|
||||
}
|
||||
|
||||
public function getItemsPerPage(): ?int
|
||||
public function getItemsPerPage(): int
|
||||
{
|
||||
return $this->itemsPerPage;
|
||||
}
|
||||
|
||||
public function hasItemsPerPage(): bool
|
||||
{
|
||||
return $this->itemsPerPage !== null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ class VisitsTracker implements VisitsTrackerInterface
|
||||
/** @var VisitRepository $repo */
|
||||
$repo = $this->em->getRepository(Visit::class);
|
||||
$paginator = new Paginator(new VisitsPaginatorAdapter($repo, $shortCode, $params));
|
||||
$paginator->setItemCountPerPage($params->hasItemsPerPage() ? $params->getItemsPerPage() : -1)
|
||||
$paginator->setItemCountPerPage($params->getItemsPerPage())
|
||||
->setCurrentPageNumber($params->getPage());
|
||||
|
||||
return $paginator;
|
||||
|
||||
Reference in New Issue
Block a user