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 Shlinkio\Shlink\Core\getOptionalBoolFromInputFilter; |
12
|
|
|
use function Shlinkio\Shlink\Core\getOptionalIntFromInputFilter; |
13
|
|
|
use function Shlinkio\Shlink\Core\parseDateField; |
14
|
|
|
|
15
|
|
|
use const Shlinkio\Shlink\Core\DEFAULT_SHORT_CODES_LENGTH; |
|
|
|
|
16
|
|
|
|
17
|
|
|
final class ShortUrlMeta |
18
|
|
|
{ |
19
|
|
|
private ?Chronos $validSince = null; |
20
|
|
|
private ?Chronos $validUntil = null; |
21
|
|
|
private ?string $customSlug = null; |
22
|
|
|
private ?int $maxVisits = null; |
23
|
|
|
private ?bool $findIfExists = null; |
24
|
|
|
private ?string $domain = null; |
25
|
|
|
private int $shortCodeLength = 5; |
26
|
|
|
private ?bool $validateUrl = null; |
27
|
|
|
private ?string $apiKey = null; |
28
|
|
|
|
29
|
115 |
|
// Enforce named constructors |
30
|
|
|
private function __construct() |
31
|
115 |
|
{ |
32
|
|
|
} |
33
|
85 |
|
|
34
|
|
|
public static function createEmpty(): self |
35
|
85 |
|
{ |
36
|
|
|
return new self(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @throws ValidationException |
41
|
41 |
|
*/ |
42
|
|
|
public static function fromRawData(array $data): self |
43
|
41 |
|
{ |
44
|
41 |
|
$instance = new self(); |
45
|
30 |
|
$instance->validateAndInit($data); |
46
|
|
|
return $instance; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws ValidationException |
51
|
41 |
|
*/ |
52
|
|
|
private function validateAndInit(array $data): void |
53
|
41 |
|
{ |
54
|
41 |
|
$inputFilter = new ShortUrlMetaInputFilter($data); |
55
|
11 |
|
if (! $inputFilter->isValid()) { |
56
|
|
|
throw ValidationException::fromInputFilter($inputFilter); |
57
|
|
|
} |
58
|
30 |
|
|
59
|
30 |
|
$this->validSince = parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_SINCE)); |
60
|
30 |
|
$this->validUntil = parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_UNTIL)); |
61
|
30 |
|
$this->customSlug = $inputFilter->getValue(ShortUrlMetaInputFilter::CUSTOM_SLUG); |
62
|
30 |
|
$this->maxVisits = getOptionalIntFromInputFilter($inputFilter, ShortUrlMetaInputFilter::MAX_VISITS); |
63
|
30 |
|
$this->findIfExists = $inputFilter->getValue(ShortUrlMetaInputFilter::FIND_IF_EXISTS); |
64
|
30 |
|
$this->validateUrl = getOptionalBoolFromInputFilter($inputFilter, ShortUrlMetaInputFilter::VALIDATE_URL); |
65
|
30 |
|
$this->domain = $inputFilter->getValue(ShortUrlMetaInputFilter::DOMAIN); |
66
|
30 |
|
$this->shortCodeLength = getOptionalIntFromInputFilter( |
67
|
30 |
|
$inputFilter, |
68
|
14 |
|
ShortUrlMetaInputFilter::SHORT_CODE_LENGTH, |
69
|
30 |
|
) ?? DEFAULT_SHORT_CODES_LENGTH; |
70
|
|
|
$this->apiKey = $inputFilter->getValue(ShortUrlMetaInputFilter::API_KEY); |
71
|
105 |
|
} |
72
|
|
|
|
73
|
105 |
|
public function getValidSince(): ?Chronos |
74
|
|
|
{ |
75
|
|
|
return $this->validSince; |
76
|
6 |
|
} |
77
|
|
|
|
78
|
6 |
|
public function hasValidSince(): bool |
79
|
|
|
{ |
80
|
|
|
return $this->validSince !== null; |
81
|
105 |
|
} |
82
|
|
|
|
83
|
105 |
|
public function getValidUntil(): ?Chronos |
84
|
|
|
{ |
85
|
|
|
return $this->validUntil; |
86
|
6 |
|
} |
87
|
|
|
|
88
|
6 |
|
public function hasValidUntil(): bool |
89
|
|
|
{ |
90
|
|
|
return $this->validUntil !== null; |
91
|
105 |
|
} |
92
|
|
|
|
93
|
105 |
|
public function getCustomSlug(): ?string |
94
|
|
|
{ |
95
|
|
|
return $this->customSlug; |
96
|
105 |
|
} |
97
|
|
|
|
98
|
105 |
|
public function hasCustomSlug(): bool |
99
|
|
|
{ |
100
|
|
|
return $this->customSlug !== null; |
101
|
105 |
|
} |
102
|
|
|
|
103
|
105 |
|
public function getMaxVisits(): ?int |
104
|
|
|
{ |
105
|
|
|
return $this->maxVisits; |
106
|
6 |
|
} |
107
|
|
|
|
108
|
6 |
|
public function hasMaxVisits(): bool |
109
|
|
|
{ |
110
|
|
|
return $this->maxVisits !== null; |
111
|
12 |
|
} |
112
|
|
|
|
113
|
12 |
|
public function findIfExists(): bool |
114
|
|
|
{ |
115
|
|
|
return (bool) $this->findIfExists; |
116
|
1 |
|
} |
117
|
|
|
|
118
|
1 |
|
public function hasDomain(): bool |
119
|
|
|
{ |
120
|
|
|
return $this->domain !== null; |
121
|
100 |
|
} |
122
|
|
|
|
123
|
100 |
|
public function getDomain(): ?string |
124
|
|
|
{ |
125
|
|
|
return $this->domain; |
126
|
100 |
|
} |
127
|
|
|
|
128
|
100 |
|
public function getShortCodeLength(): int |
129
|
|
|
{ |
130
|
|
|
return $this->shortCodeLength; |
131
|
8 |
|
} |
132
|
|
|
|
133
|
8 |
|
public function doValidateUrl(): ?bool |
134
|
|
|
{ |
135
|
|
|
return $this->validateUrl; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getApiKey(): ?string |
139
|
|
|
{ |
140
|
|
|
return $this->apiKey; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|