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