1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Shlinkio\Shlink\Core\Model; |
6
|
|
|
|
7
|
|
|
use Cake\Chronos\Chronos; |
8
|
|
|
use Shlinkio\Shlink\Core\Exception\ValidationException; |
9
|
|
|
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter; |
10
|
|
|
|
11
|
|
|
use function array_key_exists; |
12
|
|
|
use function Shlinkio\Shlink\Core\parseDateField; |
13
|
|
|
|
14
|
|
|
final class ShortUrlMeta |
15
|
|
|
{ |
16
|
|
|
private bool $validSincePropWasProvided = false; |
17
|
|
|
private ?Chronos $validSince = null; |
18
|
|
|
private bool $validUntilPropWasProvided = false; |
19
|
|
|
private ?Chronos $validUntil = null; |
20
|
|
|
private ?string $customSlug = null; |
21
|
|
|
private bool $maxVisitsPropWasProvided = false; |
22
|
|
|
private ?int $maxVisits = null; |
23
|
|
|
private ?bool $findIfExists = null; |
24
|
|
|
private ?string $domain = null; |
25
|
|
|
|
26
|
|
|
// Force named constructors |
27
|
|
|
private function __construct() |
28
|
|
|
{ |
29
|
|
|
} |
30
|
|
|
|
31
|
55 |
|
public static function createEmpty(): self |
32
|
|
|
{ |
33
|
55 |
|
return new self(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @throws ValidationException |
38
|
|
|
*/ |
39
|
19 |
|
public static function fromRawData(array $data): self |
40
|
|
|
{ |
41
|
19 |
|
$instance = new self(); |
42
|
19 |
|
$instance->validateAndInit($data); |
43
|
11 |
|
return $instance; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @throws ValidationException |
48
|
|
|
*/ |
49
|
19 |
|
private function validateAndInit(array $data): void |
50
|
|
|
{ |
51
|
19 |
|
$inputFilter = new ShortUrlMetaInputFilter($data); |
52
|
19 |
|
if (! $inputFilter->isValid()) { |
53
|
8 |
|
throw ValidationException::fromInputFilter($inputFilter); |
54
|
|
|
} |
55
|
|
|
|
56
|
11 |
|
$this->validSince = parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_SINCE)); |
57
|
11 |
|
$this->validSincePropWasProvided = array_key_exists(ShortUrlMetaInputFilter::VALID_SINCE, $data); |
58
|
11 |
|
$this->validUntil = parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_UNTIL)); |
59
|
11 |
|
$this->validUntilPropWasProvided = array_key_exists(ShortUrlMetaInputFilter::VALID_UNTIL, $data); |
60
|
11 |
|
$this->customSlug = $inputFilter->getValue(ShortUrlMetaInputFilter::CUSTOM_SLUG); |
61
|
11 |
|
$maxVisits = $inputFilter->getValue(ShortUrlMetaInputFilter::MAX_VISITS); |
62
|
11 |
|
$this->maxVisits = $maxVisits !== null ? (int) $maxVisits : null; |
63
|
11 |
|
$this->maxVisitsPropWasProvided = array_key_exists(ShortUrlMetaInputFilter::MAX_VISITS, $data); |
64
|
11 |
|
$this->findIfExists = $inputFilter->getValue(ShortUrlMetaInputFilter::FIND_IF_EXISTS); |
65
|
11 |
|
$this->domain = $inputFilter->getValue(ShortUrlMetaInputFilter::DOMAIN); |
66
|
|
|
} |
67
|
|
|
|
68
|
58 |
|
public function getValidSince(): ?Chronos |
69
|
|
|
{ |
70
|
58 |
|
return $this->validSince; |
71
|
|
|
} |
72
|
|
|
|
73
|
11 |
|
public function hasValidSince(): bool |
74
|
|
|
{ |
75
|
11 |
|
return $this->validSincePropWasProvided; |
76
|
|
|
} |
77
|
|
|
|
78
|
59 |
|
public function getValidUntil(): ?Chronos |
79
|
|
|
{ |
80
|
59 |
|
return $this->validUntil; |
81
|
|
|
} |
82
|
|
|
|
83
|
11 |
|
public function hasValidUntil(): bool |
84
|
|
|
{ |
85
|
11 |
|
return $this->validUntilPropWasProvided; |
86
|
|
|
} |
87
|
|
|
|
88
|
58 |
|
public function getCustomSlug(): ?string |
89
|
|
|
{ |
90
|
58 |
|
return $this->customSlug; |
91
|
|
|
} |
92
|
|
|
|
93
|
65 |
|
public function hasCustomSlug(): bool |
94
|
|
|
{ |
95
|
65 |
|
return $this->customSlug !== null; |
96
|
|
|
} |
97
|
|
|
|
98
|
59 |
|
public function getMaxVisits(): ?int |
99
|
|
|
{ |
100
|
59 |
|
return $this->maxVisits; |
101
|
|
|
} |
102
|
|
|
|
103
|
11 |
|
public function hasMaxVisits(): bool |
104
|
|
|
{ |
105
|
11 |
|
return $this->maxVisitsPropWasProvided; |
106
|
|
|
} |
107
|
|
|
|
108
|
14 |
|
public function findIfExists(): bool |
109
|
|
|
{ |
110
|
14 |
|
return (bool) $this->findIfExists; |
111
|
|
|
} |
112
|
|
|
|
113
|
9 |
|
public function hasDomain(): bool |
114
|
|
|
{ |
115
|
9 |
|
return $this->domain !== null; |
116
|
|
|
} |
117
|
|
|
|
118
|
57 |
|
public function getDomain(): ?string |
119
|
|
|
{ |
120
|
57 |
|
return $this->domain; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|