|
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
|
|
|
use function array_key_exists; |
|
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
|
54 |
|
public static function createEmpty(): self |
|
32
|
|
|
{ |
|
33
|
54 |
|
return new self(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param array $data |
|
38
|
|
|
* @throws ValidationException |
|
39
|
|
|
*/ |
|
40
|
19 |
|
public static function fromRawData(array $data): self |
|
41
|
|
|
{ |
|
42
|
19 |
|
$instance = new self(); |
|
43
|
19 |
|
$instance->validate($data); |
|
44
|
11 |
|
return $instance; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param array $data |
|
49
|
|
|
* @throws ValidationException |
|
50
|
|
|
*/ |
|
51
|
19 |
|
private function validate(array $data): void |
|
52
|
|
|
{ |
|
53
|
19 |
|
$inputFilter = new ShortUrlMetaInputFilter($data); |
|
54
|
19 |
|
if (! $inputFilter->isValid()) { |
|
55
|
8 |
|
throw ValidationException::fromInputFilter($inputFilter); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
11 |
|
$this->validSince = $this->parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_SINCE)); |
|
59
|
11 |
|
$this->validSincePropWasProvided = array_key_exists(ShortUrlMetaInputFilter::VALID_SINCE, $data); |
|
60
|
11 |
|
$this->validUntil = $this->parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_UNTIL)); |
|
61
|
11 |
|
$this->validUntilPropWasProvided = array_key_exists(ShortUrlMetaInputFilter::VALID_UNTIL, $data); |
|
62
|
11 |
|
$this->customSlug = $inputFilter->getValue(ShortUrlMetaInputFilter::CUSTOM_SLUG); |
|
63
|
11 |
|
$maxVisits = $inputFilter->getValue(ShortUrlMetaInputFilter::MAX_VISITS); |
|
64
|
11 |
|
$this->maxVisits = $maxVisits !== null ? (int) $maxVisits : null; |
|
65
|
11 |
|
$this->maxVisitsPropWasProvided = array_key_exists(ShortUrlMetaInputFilter::MAX_VISITS, $data); |
|
66
|
11 |
|
$this->findIfExists = $inputFilter->getValue(ShortUrlMetaInputFilter::FIND_IF_EXISTS); |
|
67
|
11 |
|
$this->domain = $inputFilter->getValue(ShortUrlMetaInputFilter::DOMAIN); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string|DateTimeInterface|Chronos|null $date |
|
72
|
|
|
*/ |
|
73
|
11 |
|
private function parseDateField($date): ?Chronos |
|
74
|
|
|
{ |
|
75
|
11 |
|
if ($date === null || $date instanceof Chronos) { |
|
76
|
9 |
|
return $date; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
3 |
|
if ($date instanceof DateTimeInterface) { |
|
80
|
|
|
return Chronos::instance($date); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
3 |
|
return Chronos::parse($date); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
57 |
|
public function getValidSince(): ?Chronos |
|
87
|
|
|
{ |
|
88
|
57 |
|
return $this->validSince; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
11 |
|
public function hasValidSince(): bool |
|
92
|
|
|
{ |
|
93
|
11 |
|
return $this->validSincePropWasProvided; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
58 |
|
public function getValidUntil(): ?Chronos |
|
97
|
|
|
{ |
|
98
|
58 |
|
return $this->validUntil; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
11 |
|
public function hasValidUntil(): bool |
|
102
|
|
|
{ |
|
103
|
11 |
|
return $this->validUntilPropWasProvided; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
57 |
|
public function getCustomSlug(): ?string |
|
107
|
|
|
{ |
|
108
|
57 |
|
return $this->customSlug; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
64 |
|
public function hasCustomSlug(): bool |
|
112
|
|
|
{ |
|
113
|
64 |
|
return $this->customSlug !== null; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
58 |
|
public function getMaxVisits(): ?int |
|
117
|
|
|
{ |
|
118
|
58 |
|
return $this->maxVisits; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
11 |
|
public function hasMaxVisits(): bool |
|
122
|
|
|
{ |
|
123
|
11 |
|
return $this->maxVisitsPropWasProvided; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
14 |
|
public function findIfExists(): bool |
|
127
|
|
|
{ |
|
128
|
14 |
|
return (bool) $this->findIfExists; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
9 |
|
public function hasDomain(): bool |
|
132
|
|
|
{ |
|
133
|
9 |
|
return $this->domain !== null; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
56 |
|
public function getDomain(): ?string |
|
137
|
|
|
{ |
|
138
|
56 |
|
return $this->domain; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|