1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Shlinkio\Shlink\Core\Model; |
5
|
|
|
|
6
|
|
|
use Cake\Chronos\Chronos; |
7
|
|
|
use Shlinkio\Shlink\Core\Exception\ValidationException; |
8
|
|
|
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter; |
9
|
|
|
|
10
|
|
|
final class ShortUrlMeta |
11
|
|
|
{ |
12
|
|
|
/** @var Chronos|null */ |
13
|
|
|
private $validSince; |
14
|
|
|
/** @var Chronos|null */ |
15
|
|
|
private $validUntil; |
16
|
|
|
/** @var string|null */ |
17
|
|
|
private $customSlug; |
18
|
|
|
/** @var int|null */ |
19
|
|
|
private $maxVisits; |
20
|
|
|
/** @var bool|null */ |
21
|
|
|
private $findIfExists; |
22
|
|
|
/** @var string|null */ |
23
|
|
|
private $domain; |
24
|
|
|
|
25
|
|
|
// Force named constructors |
26
|
|
|
private function __construct() |
27
|
|
|
{ |
28
|
|
|
} |
29
|
|
|
|
30
|
46 |
|
public static function createEmpty(): self |
31
|
|
|
{ |
32
|
46 |
|
return new self(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param array $data |
37
|
|
|
* @throws ValidationException |
38
|
|
|
*/ |
39
|
8 |
|
public static function createFromRawData(array $data): self |
40
|
|
|
{ |
41
|
8 |
|
$instance = new self(); |
42
|
8 |
|
$instance->validate($data); |
43
|
5 |
|
return $instance; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string|Chronos|null $validSince |
48
|
|
|
* @param string|Chronos|null $validUntil |
49
|
|
|
* @param string|null $customSlug |
50
|
|
|
* @param int|null $maxVisits |
51
|
|
|
* @param bool|null $findIfExists |
52
|
|
|
* @param string|null $domain |
53
|
|
|
* @throws ValidationException |
54
|
|
|
*/ |
55
|
9 |
|
public static function createFromParams( |
56
|
|
|
$validSince = null, |
57
|
|
|
$validUntil = null, |
58
|
|
|
$customSlug = null, |
59
|
|
|
$maxVisits = null, |
60
|
|
|
$findIfExists = null, |
61
|
|
|
$domain = null |
62
|
|
|
): self { |
63
|
|
|
// We do not type hint the arguments because that will be done by the validation process and we would get a |
64
|
|
|
// type error if any of them do not match |
65
|
9 |
|
$instance = new self(); |
66
|
9 |
|
$instance->validate([ |
67
|
9 |
|
ShortUrlMetaInputFilter::VALID_SINCE => $validSince, |
68
|
9 |
|
ShortUrlMetaInputFilter::VALID_UNTIL => $validUntil, |
69
|
9 |
|
ShortUrlMetaInputFilter::CUSTOM_SLUG => $customSlug, |
70
|
9 |
|
ShortUrlMetaInputFilter::MAX_VISITS => $maxVisits, |
71
|
9 |
|
ShortUrlMetaInputFilter::FIND_IF_EXISTS => $findIfExists, |
72
|
9 |
|
ShortUrlMetaInputFilter::DOMAIN => $domain, |
73
|
|
|
]); |
74
|
9 |
|
return $instance; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array $data |
79
|
|
|
* @throws ValidationException |
80
|
|
|
*/ |
81
|
16 |
|
private function validate(array $data): void |
82
|
|
|
{ |
83
|
16 |
|
$inputFilter = new ShortUrlMetaInputFilter($data); |
84
|
16 |
|
if (! $inputFilter->isValid()) { |
85
|
3 |
|
throw ValidationException::fromInputFilter($inputFilter); |
86
|
|
|
} |
87
|
|
|
|
88
|
13 |
|
$this->validSince = $this->parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_SINCE)); |
|
|
|
|
89
|
13 |
|
$this->validUntil = $this->parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_UNTIL)); |
90
|
13 |
|
$this->customSlug = $inputFilter->getValue(ShortUrlMetaInputFilter::CUSTOM_SLUG); |
|
|
|
|
91
|
13 |
|
$this->maxVisits = $inputFilter->getValue(ShortUrlMetaInputFilter::MAX_VISITS); |
|
|
|
|
92
|
13 |
|
$this->maxVisits = $this->maxVisits !== null ? (int) $this->maxVisits : null; |
93
|
13 |
|
$this->findIfExists = $inputFilter->getValue(ShortUrlMetaInputFilter::FIND_IF_EXISTS); |
|
|
|
|
94
|
13 |
|
$this->domain = $inputFilter->getValue(ShortUrlMetaInputFilter::DOMAIN); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string|Chronos|null $date |
99
|
|
|
*/ |
100
|
13 |
|
private function parseDateField($date): ?Chronos |
101
|
|
|
{ |
102
|
13 |
|
if ($date === null || $date instanceof Chronos) { |
103
|
12 |
|
return $date; |
104
|
|
|
} |
105
|
|
|
|
106
|
2 |
|
return Chronos::parse($date); |
107
|
|
|
} |
108
|
|
|
|
109
|
48 |
|
public function getValidSince(): ?Chronos |
110
|
|
|
{ |
111
|
48 |
|
return $this->validSince; |
112
|
|
|
} |
113
|
|
|
|
114
|
10 |
|
public function hasValidSince(): bool |
115
|
|
|
{ |
116
|
10 |
|
return $this->validSince !== null; |
117
|
|
|
} |
118
|
|
|
|
119
|
49 |
|
public function getValidUntil(): ?Chronos |
120
|
|
|
{ |
121
|
49 |
|
return $this->validUntil; |
122
|
|
|
} |
123
|
|
|
|
124
|
10 |
|
public function hasValidUntil(): bool |
125
|
|
|
{ |
126
|
10 |
|
return $this->validUntil !== null; |
127
|
|
|
} |
128
|
|
|
|
129
|
50 |
|
public function getCustomSlug(): ?string |
130
|
|
|
{ |
131
|
50 |
|
return $this->customSlug; |
132
|
|
|
} |
133
|
|
|
|
134
|
13 |
|
public function hasCustomSlug(): bool |
135
|
|
|
{ |
136
|
13 |
|
return $this->customSlug !== null; |
137
|
|
|
} |
138
|
|
|
|
139
|
49 |
|
public function getMaxVisits(): ?int |
140
|
|
|
{ |
141
|
49 |
|
return $this->maxVisits; |
142
|
|
|
} |
143
|
|
|
|
144
|
10 |
|
public function hasMaxVisits(): bool |
145
|
|
|
{ |
146
|
10 |
|
return $this->maxVisits !== null; |
147
|
|
|
} |
148
|
|
|
|
149
|
13 |
|
public function findIfExists(): bool |
150
|
|
|
{ |
151
|
13 |
|
return (bool) $this->findIfExists; |
152
|
|
|
} |
153
|
|
|
|
154
|
47 |
|
public function getDomain(): ?string |
155
|
|
|
{ |
156
|
47 |
|
return $this->domain; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|