1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recurrence\Model; |
4
|
|
|
|
5
|
|
|
use Recurrence\Constraint\DatetimeConstraint\DatetimeConstraintInterface; |
6
|
|
|
use Recurrence\Constraint\ProviderConstraint\ProviderConstraintInterface; |
7
|
|
|
use Recurrence\Constraint\RecurrenceConstraintInterface; |
8
|
|
|
|
9
|
|
|
class Recurrence |
10
|
|
|
{ |
11
|
|
|
private ?Frequency $frequency = null; |
12
|
|
|
private \Datetime $periodStartAt; |
13
|
|
|
private ?\Datetime $periodEndAt = null; |
14
|
|
|
private int $interval = 1; |
15
|
|
|
private ?int $count = null; |
16
|
|
|
private array $constraints = []; |
17
|
|
|
|
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
1 |
|
$this->setPeriodStartAt(new \DateTime()); |
21
|
1 |
|
} |
22
|
|
|
|
23
|
|
|
public function setPeriodStartAt(\Datetime $periodStartAt): self |
24
|
|
|
{ |
25
|
1 |
|
$this->periodStartAt = $periodStartAt; |
26
|
|
|
|
27
|
1 |
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getPeriodStartAt(): \Datetime |
31
|
|
|
{ |
32
|
1 |
|
return $this->periodStartAt; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function setPeriodEndAt(\Datetime $periodEndAt): self |
36
|
|
|
{ |
37
|
|
|
$this->periodEndAt = $periodEndAt; |
38
|
|
|
|
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getPeriodEndAt(): ?\Datetime |
43
|
|
|
{ |
44
|
|
|
return $this->periodEndAt; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function hasPeriodEndAt(): bool |
48
|
|
|
{ |
49
|
|
|
return $this->periodEndAt !== null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function setFrequency(Frequency $frequency): self |
53
|
|
|
{ |
54
|
|
|
$this->frequency = $frequency; |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getFrequency(): ?Frequency |
60
|
|
|
{ |
61
|
|
|
return $this->frequency; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getInterval(): int |
65
|
|
|
{ |
66
|
|
|
return $this->interval; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function setInterval(int $interval): self |
70
|
|
|
{ |
71
|
|
|
$this->interval = $interval; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getCount(): ?int |
77
|
|
|
{ |
78
|
|
|
return $this->count; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function hasCount(): bool |
82
|
|
|
{ |
83
|
|
|
return $this->count !== null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function setCount(int $count): self |
87
|
|
|
{ |
88
|
|
|
$this->count = $count; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getConstraints(): array |
94
|
|
|
{ |
95
|
1 |
|
return $this->constraints; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function setConstraints(array $constraints): self |
99
|
|
|
{ |
100
|
1 |
|
$this->constraints = []; |
101
|
|
|
|
102
|
1 |
|
foreach ($constraints as $constraint) { |
103
|
1 |
|
$this->addConstraint($constraint); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function hasConstraints(): bool |
110
|
|
|
{ |
111
|
1 |
|
return !empty($this->constraints); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function addConstraint(RecurrenceConstraintInterface $constraint): self |
115
|
|
|
{ |
116
|
1 |
|
if ($this->hasConstraint(get_class($constraint))) { |
117
|
1 |
|
throw new \InvalidArgumentException(sprintf('Duplicate constraint [%s]', get_class($constraint))); |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
$this->constraints[] = $constraint; |
121
|
|
|
|
122
|
1 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function removeConstraint(string $constraintClassName): self |
126
|
|
|
{ |
127
|
1 |
|
foreach ($this->constraints as $key => $constraint) { |
128
|
1 |
|
if (get_class($constraint) == $constraintClassName) { |
129
|
1 |
|
unset($this->constraints[$key]); |
130
|
|
|
|
131
|
1 |
|
break; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
$this->constraints = array_values($this->constraints); |
136
|
|
|
|
137
|
1 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function hasConstraint(string $constraintClassName): bool |
141
|
|
|
{ |
142
|
1 |
|
foreach ($this->constraints as $key => $constraint) { |
143
|
1 |
|
if (get_class($constraint) == $constraintClassName) { |
144
|
1 |
|
return true; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
return false; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function hasProviderConstraint(): bool |
152
|
|
|
{ |
153
|
1 |
|
foreach ($this->constraints as $key => $constraint) { |
154
|
1 |
|
if ($constraint instanceof ProviderConstraintInterface) { |
155
|
1 |
|
return true; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
1 |
|
return false; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function hasDatetimeConstraint(): bool |
163
|
|
|
{ |
164
|
1 |
|
foreach ($this->constraints as $key => $constraint) { |
165
|
1 |
|
if ($constraint instanceof DatetimeConstraintInterface) { |
166
|
1 |
|
return true; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
1 |
|
return false; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|