|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Shlinkio\Shlink\Core\Model; |
|
6
|
|
|
|
|
7
|
|
|
use Shlinkio\Shlink\Common\Util\DateRange; |
|
8
|
|
|
use Shlinkio\Shlink\Core\Exception\ValidationException; |
|
9
|
|
|
use Shlinkio\Shlink\Core\Validation\ShortUrlsParamsInputFilter; |
|
10
|
|
|
|
|
11
|
|
|
use function Shlinkio\Shlink\Core\parseDateField; |
|
12
|
|
|
|
|
13
|
|
|
final class ShortUrlsParams |
|
14
|
|
|
{ |
|
15
|
|
|
public const DEFAULT_ITEMS_PER_PAGE = 10; |
|
16
|
|
|
|
|
17
|
|
|
private int $page; |
|
18
|
|
|
private ?string $searchTerm; |
|
19
|
|
|
private array $tags; |
|
20
|
|
|
private ShortUrlsOrdering $orderBy; |
|
21
|
|
|
private ?DateRange $dateRange; |
|
22
|
|
|
private ?int $itemsPerPage = null; |
|
23
|
|
|
|
|
24
|
51 |
|
private function __construct() |
|
25
|
|
|
{ |
|
26
|
51 |
|
} |
|
27
|
|
|
|
|
28
|
3 |
|
public static function emptyInstance(): self |
|
29
|
|
|
{ |
|
30
|
3 |
|
return self::fromRawData([]); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @throws ValidationException |
|
35
|
|
|
*/ |
|
36
|
51 |
|
public static function fromRawData(array $query): self |
|
37
|
|
|
{ |
|
38
|
51 |
|
$instance = new self(); |
|
39
|
51 |
|
$instance->validateAndInit($query); |
|
40
|
|
|
|
|
41
|
51 |
|
return $instance; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @throws ValidationException |
|
46
|
|
|
*/ |
|
47
|
51 |
|
private function validateAndInit(array $query): void |
|
48
|
|
|
{ |
|
49
|
51 |
|
$inputFilter = new ShortUrlsParamsInputFilter($query); |
|
50
|
51 |
|
if (! $inputFilter->isValid()) { |
|
51
|
|
|
throw ValidationException::fromInputFilter($inputFilter); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
51 |
|
$this->page = (int) ($inputFilter->getValue(ShortUrlsParamsInputFilter::PAGE) ?? 1); |
|
55
|
51 |
|
$this->searchTerm = $inputFilter->getValue(ShortUrlsParamsInputFilter::SEARCH_TERM); |
|
56
|
51 |
|
$this->tags = (array) $inputFilter->getValue(ShortUrlsParamsInputFilter::TAGS); |
|
57
|
51 |
|
$this->dateRange = new DateRange( |
|
58
|
51 |
|
parseDateField($inputFilter->getValue(ShortUrlsParamsInputFilter::START_DATE)), |
|
59
|
51 |
|
parseDateField($inputFilter->getValue(ShortUrlsParamsInputFilter::END_DATE)), |
|
60
|
|
|
); |
|
61
|
51 |
|
$this->orderBy = ShortUrlsOrdering::fromRawData($query); |
|
62
|
51 |
|
$this->itemsPerPage = (int) ( |
|
63
|
51 |
|
$inputFilter->getValue(ShortUrlsParamsInputFilter::ITEMS_PER_PAGE) ?? self::DEFAULT_ITEMS_PER_PAGE |
|
64
|
|
|
); |
|
65
|
51 |
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
public function page(): int |
|
68
|
|
|
{ |
|
69
|
2 |
|
return $this->page; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
2 |
|
public function itemsPerPage(): int |
|
73
|
|
|
{ |
|
74
|
2 |
|
return $this->itemsPerPage; |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
24 |
|
public function searchTerm(): ?string |
|
78
|
|
|
{ |
|
79
|
24 |
|
return $this->searchTerm; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
24 |
|
public function tags(): array |
|
83
|
|
|
{ |
|
84
|
24 |
|
return $this->tags; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
13 |
|
public function orderBy(): ShortUrlsOrdering |
|
88
|
|
|
{ |
|
89
|
13 |
|
return $this->orderBy; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
24 |
|
public function dateRange(): ?DateRange |
|
93
|
|
|
{ |
|
94
|
24 |
|
return $this->dateRange; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|