Created TagsParams class

This commit is contained in:
Alejandro Celaya 2022-01-05 18:19:29 +01:00
parent 3dc46bc5a3
commit 5c0abb3d96
3 changed files with 74 additions and 34 deletions

View File

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Model;
use Shlinkio\Shlink\Common\Paginator\Paginator;
abstract class AbstractInfinitePaginableListParams
{
private const FIRST_PAGE = 1;
private int $page;
private int $itemsPerPage;
protected function __construct(?int $page, ?int $itemsPerPage)
{
$this->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;
}
}

View File

@ -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;

View File

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Tag\Model;
use Shlinkio\Shlink\Core\Model\AbstractInfinitePaginableListParams;
final class TagsParams extends AbstractInfinitePaginableListParams
{
private function __construct(private ?string $searchTerm, ?int $page, ?int $itemsPerPage)
{
parent::__construct($page, $itemsPerPage);
}
public static function fromRawData(array $query): self
{
return new self(
$query['searchTerm'] ?? null,
isset($query['page']) ? (int) $query['page'] : null,
isset($query['itemsPerPage']) ? (int) $query['itemsPerPage'] : null,
);
}
public function searchTerm(): ?string
{
return $this->searchTerm;
}
}