|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Shlinkio\Shlink\Core\Model; |
|
6
|
|
|
|
|
7
|
|
|
use Cake\Chronos\Chronos; |
|
8
|
|
|
use DateTimeInterface; |
|
9
|
|
|
use Shlinkio\Shlink\Core\Exception\ValidationException; |
|
10
|
|
|
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter; |
|
11
|
|
|
|
|
12
|
|
|
final class ShortUrlMeta |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var Chronos|null */ |
|
15
|
|
|
private $validSince; |
|
16
|
|
|
/** @var Chronos|null */ |
|
17
|
|
|
private $validUntil; |
|
18
|
|
|
/** @var string|null */ |
|
19
|
|
|
private $customSlug; |
|
20
|
|
|
/** @var int|null */ |
|
21
|
|
|
private $maxVisits; |
|
22
|
|
|
/** @var bool|null */ |
|
23
|
|
|
private $findIfExists; |
|
24
|
|
|
/** @var string|null */ |
|
25
|
|
|
private $domain; |
|
26
|
|
|
|
|
27
|
|
|
// Force named constructors |
|
28
|
|
|
private function __construct() |
|
29
|
|
|
{ |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
54 |
|
public static function createEmpty(): self |
|
33
|
|
|
{ |
|
34
|
54 |
|
return new self(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param array $data |
|
39
|
|
|
* @throws ValidationException |
|
40
|
|
|
*/ |
|
41
|
10 |
|
public static function createFromRawData(array $data): self |
|
42
|
|
|
{ |
|
43
|
10 |
|
$instance = new self(); |
|
44
|
10 |
|
$instance->validate($data); |
|
45
|
5 |
|
return $instance; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string|Chronos|null $validSince |
|
50
|
|
|
* @param string|Chronos|null $validUntil |
|
51
|
|
|
* @param string|null $customSlug |
|
52
|
|
|
* @param int|null $maxVisits |
|
53
|
|
|
* @param bool|null $findIfExists |
|
54
|
|
|
* @param string|null $domain |
|
55
|
|
|
* @throws ValidationException |
|
56
|
|
|
*/ |
|
57
|
13 |
|
public static function createFromParams( |
|
58
|
|
|
$validSince = null, |
|
59
|
|
|
$validUntil = null, |
|
60
|
|
|
$customSlug = null, |
|
61
|
|
|
$maxVisits = null, |
|
62
|
|
|
$findIfExists = null, |
|
63
|
|
|
$domain = null |
|
64
|
|
|
): self { |
|
65
|
|
|
// We do not type hint the arguments because that will be done by the validation process and we would get a |
|
66
|
|
|
// type error if any of them do not match |
|
67
|
13 |
|
$instance = new self(); |
|
68
|
13 |
|
$instance->validate([ |
|
69
|
13 |
|
ShortUrlMetaInputFilter::VALID_SINCE => $validSince, |
|
70
|
13 |
|
ShortUrlMetaInputFilter::VALID_UNTIL => $validUntil, |
|
71
|
13 |
|
ShortUrlMetaInputFilter::CUSTOM_SLUG => $customSlug, |
|
72
|
13 |
|
ShortUrlMetaInputFilter::MAX_VISITS => $maxVisits, |
|
73
|
13 |
|
ShortUrlMetaInputFilter::FIND_IF_EXISTS => $findIfExists, |
|
74
|
13 |
|
ShortUrlMetaInputFilter::DOMAIN => $domain, |
|
75
|
|
|
]); |
|
76
|
10 |
|
return $instance; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param array $data |
|
81
|
|
|
* @throws ValidationException |
|
82
|
|
|
*/ |
|
83
|
22 |
|
private function validate(array $data): void |
|
84
|
|
|
{ |
|
85
|
22 |
|
$inputFilter = new ShortUrlMetaInputFilter($data); |
|
86
|
22 |
|
if (! $inputFilter->isValid()) { |
|
87
|
8 |
|
throw ValidationException::fromInputFilter($inputFilter); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
14 |
|
$this->validSince = $this->parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_SINCE)); |
|
91
|
14 |
|
$this->validUntil = $this->parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_UNTIL)); |
|
92
|
14 |
|
$this->customSlug = $inputFilter->getValue(ShortUrlMetaInputFilter::CUSTOM_SLUG); |
|
|
|
|
|
|
93
|
14 |
|
$this->maxVisits = $inputFilter->getValue(ShortUrlMetaInputFilter::MAX_VISITS); |
|
|
|
|
|
|
94
|
14 |
|
$this->maxVisits = $this->maxVisits !== null ? (int) $this->maxVisits : null; |
|
95
|
14 |
|
$this->findIfExists = $inputFilter->getValue(ShortUrlMetaInputFilter::FIND_IF_EXISTS); |
|
|
|
|
|
|
96
|
14 |
|
$this->domain = $inputFilter->getValue(ShortUrlMetaInputFilter::DOMAIN); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string|DateTimeInterface|Chronos|null $date |
|
101
|
|
|
*/ |
|
102
|
14 |
|
private function parseDateField($date): ?Chronos |
|
103
|
|
|
{ |
|
104
|
14 |
|
if ($date === null || $date instanceof Chronos) { |
|
105
|
13 |
|
return $date; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
2 |
|
if ($date instanceof DateTimeInterface) { |
|
109
|
|
|
return Chronos::instance($date); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
2 |
|
return Chronos::parse($date); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
57 |
|
public function getValidSince(): ?Chronos |
|
116
|
|
|
{ |
|
117
|
57 |
|
return $this->validSince; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
11 |
|
public function hasValidSince(): bool |
|
121
|
|
|
{ |
|
122
|
11 |
|
return $this->validSince !== null; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
58 |
|
public function getValidUntil(): ?Chronos |
|
126
|
|
|
{ |
|
127
|
58 |
|
return $this->validUntil; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
11 |
|
public function hasValidUntil(): bool |
|
131
|
|
|
{ |
|
132
|
11 |
|
return $this->validUntil !== null; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
58 |
|
public function getCustomSlug(): ?string |
|
136
|
|
|
{ |
|
137
|
58 |
|
return $this->customSlug; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
64 |
|
public function hasCustomSlug(): bool |
|
141
|
|
|
{ |
|
142
|
64 |
|
return $this->customSlug !== null; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
58 |
|
public function getMaxVisits(): ?int |
|
146
|
|
|
{ |
|
147
|
58 |
|
return $this->maxVisits; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
11 |
|
public function hasMaxVisits(): bool |
|
151
|
|
|
{ |
|
152
|
11 |
|
return $this->maxVisits !== null; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
14 |
|
public function findIfExists(): bool |
|
156
|
|
|
{ |
|
157
|
14 |
|
return (bool) $this->findIfExists; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
9 |
|
public function hasDomain(): bool |
|
161
|
|
|
{ |
|
162
|
9 |
|
return $this->domain !== null; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
56 |
|
public function getDomain(): ?string |
|
166
|
|
|
{ |
|
167
|
56 |
|
return $this->domain; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.