mirror of
https://github.com/shlinkio/shlink.git
synced 2026-10-12 21:31:58 -05:00
86 lines
2.1 KiB
PHP
86 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Shlinkio\Shlink\Core\Model;
|
|
|
|
use Shlinkio\Shlink\Common\Util\DateRange;
|
|
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
|
use Shlinkio\Shlink\Core\Validation\ShortUrlsParamsInputFilter;
|
|
|
|
use function Shlinkio\Shlink\Core\parseDateField;
|
|
|
|
final class ShortUrlsParams
|
|
{
|
|
private int $page;
|
|
private ?string $searchTerm;
|
|
private array $tags;
|
|
private ShortUrlsOrdering $orderBy;
|
|
private ?DateRange $dateRange;
|
|
|
|
private function __construct()
|
|
{
|
|
}
|
|
|
|
public static function emptyInstance(): self
|
|
{
|
|
return self::fromRawData([]);
|
|
}
|
|
|
|
/**
|
|
* @throws ValidationException
|
|
*/
|
|
public static function fromRawData(array $query): self
|
|
{
|
|
$instance = new self();
|
|
$instance->validateAndInit($query);
|
|
|
|
return $instance;
|
|
}
|
|
|
|
/**
|
|
* @throws ValidationException
|
|
*/
|
|
private function validateAndInit(array $query): void
|
|
{
|
|
$inputFilter = new ShortUrlsParamsInputFilter($query);
|
|
if (! $inputFilter->isValid()) {
|
|
throw ValidationException::fromInputFilter($inputFilter);
|
|
}
|
|
|
|
$this->page = (int) ($inputFilter->getValue(ShortUrlsParamsInputFilter::PAGE) ?? 1);
|
|
$this->searchTerm = $inputFilter->getValue(ShortUrlsParamsInputFilter::SEARCH_TERM);
|
|
$this->tags = (array) $inputFilter->getValue(ShortUrlsParamsInputFilter::TAGS);
|
|
$this->dateRange = new DateRange(
|
|
parseDateField($inputFilter->getValue(ShortUrlsParamsInputFilter::START_DATE)),
|
|
parseDateField($inputFilter->getValue(ShortUrlsParamsInputFilter::END_DATE)),
|
|
);
|
|
$this->orderBy = ShortUrlsOrdering::fromRawData($query);
|
|
}
|
|
|
|
public function page(): int
|
|
{
|
|
return $this->page;
|
|
}
|
|
|
|
public function searchTerm(): ?string
|
|
{
|
|
return $this->searchTerm;
|
|
}
|
|
|
|
public function tags(): array
|
|
{
|
|
return $this->tags;
|
|
}
|
|
|
|
public function orderBy(): ShortUrlsOrdering
|
|
{
|
|
return $this->orderBy;
|
|
}
|
|
|
|
public function dateRange(): ?DateRange
|
|
{
|
|
return $this->dateRange;
|
|
}
|
|
}
|