From 5c0abb3d96c38daed7c942a311697fa92e0e9f3c Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 5 Jan 2022 18:19:29 +0100 Subject: [PATCH] Created TagsParams class --- .../AbstractInfinitePaginableListParams.php | 41 +++++++++++++++++++ module/Core/src/Model/VisitsParams.php | 38 ++--------------- module/Core/src/Tag/Model/TagsParams.php | 29 +++++++++++++ 3 files changed, 74 insertions(+), 34 deletions(-) create mode 100644 module/Core/src/Model/AbstractInfinitePaginableListParams.php create mode 100644 module/Core/src/Tag/Model/TagsParams.php diff --git a/module/Core/src/Model/AbstractInfinitePaginableListParams.php b/module/Core/src/Model/AbstractInfinitePaginableListParams.php new file mode 100644 index 00000000..ae107fdc --- /dev/null +++ b/module/Core/src/Model/AbstractInfinitePaginableListParams.php @@ -0,0 +1,41 @@ +page = $this->determinePage($page); + $this->itemsPerPage = $this->determineItemsPerPage($itemsPerPage); + } + + private function determinePage(?int $page): int + { + return $page === null || $page <= 0 ? self::FIRST_PAGE : $page; + } + + private function determineItemsPerPage(?int $itemsPerPage): int + { + return $itemsPerPage === null || $itemsPerPage < 0 ? Paginator::ALL_ITEMS : $itemsPerPage; + } + + public function getPage(): int + { + return $this->page; + } + + public function getItemsPerPage(): int + { + return $this->itemsPerPage; + } +} diff --git a/module/Core/src/Model/VisitsParams.php b/module/Core/src/Model/VisitsParams.php index dd5a656d..718a4bc5 100644 --- a/module/Core/src/Model/VisitsParams.php +++ b/module/Core/src/Model/VisitsParams.php @@ -4,49 +4,29 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Model; -use Shlinkio\Shlink\Common\Paginator\Paginator; use Shlinkio\Shlink\Common\Util\DateRange; use function Shlinkio\Shlink\Core\parseDateRangeFromQuery; -final class VisitsParams +final class VisitsParams extends AbstractInfinitePaginableListParams { - private const FIRST_PAGE = 1; - private DateRange $dateRange; - private int $page; - private int $itemsPerPage; public function __construct( ?DateRange $dateRange = null, - int $page = self::FIRST_PAGE, + ?int $page = null, ?int $itemsPerPage = null, private bool $excludeBots = false, ) { + parent::__construct($page, $itemsPerPage); $this->dateRange = $dateRange ?? DateRange::emptyInstance(); - $this->page = $this->determinePage($page); - $this->itemsPerPage = $this->determineItemsPerPage($itemsPerPage); - } - - private function determinePage(int $page): int - { - return $page > 0 ? $page : self::FIRST_PAGE; - } - - private function determineItemsPerPage(?int $itemsPerPage): int - { - if ($itemsPerPage !== null && $itemsPerPage < 0) { - return Paginator::ALL_ITEMS; - } - - return $itemsPerPage ?? Paginator::ALL_ITEMS; } public static function fromRawData(array $query): self { return new self( parseDateRangeFromQuery($query, 'startDate', 'endDate'), - (int) ($query['page'] ?? self::FIRST_PAGE), + isset($query['page']) ? (int) $query['page'] : null, isset($query['itemsPerPage']) ? (int) $query['itemsPerPage'] : null, isset($query['excludeBots']), ); @@ -57,16 +37,6 @@ final class VisitsParams return $this->dateRange; } - public function getPage(): int - { - return $this->page; - } - - public function getItemsPerPage(): int - { - return $this->itemsPerPage; - } - public function excludeBots(): bool { return $this->excludeBots; diff --git a/module/Core/src/Tag/Model/TagsParams.php b/module/Core/src/Tag/Model/TagsParams.php new file mode 100644 index 00000000..7fd12348 --- /dev/null +++ b/module/Core/src/Tag/Model/TagsParams.php @@ -0,0 +1,29 @@ +searchTerm; + } +}