|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Recurrence\Rrule; |
|
4
|
|
|
|
|
5
|
|
|
use Recurrence\Model\Exception\InvalidRruleException; |
|
6
|
|
|
use Recurrence\Model\Exception\InvalidRruleExpressionException; |
|
7
|
|
|
use Recurrence\Model\Recurrence; |
|
8
|
|
|
|
|
9
|
|
|
class RecurrenceProvider |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Map extractor/transformer names with Recurrence parameters. |
|
13
|
|
|
*/ |
|
14
|
|
|
private array $options = [ |
|
15
|
|
|
'Count', |
|
16
|
|
|
'Interval', |
|
17
|
|
|
'Freq', |
|
18
|
|
|
'DtStartTimezoned', |
|
19
|
|
|
'DtStart', |
|
20
|
|
|
'UntilTimezoned', |
|
21
|
|
|
'Until', |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @throws InvalidRruleException |
|
26
|
|
|
*/ |
|
27
|
|
|
public function create(string $rRule): Recurrence |
|
28
|
|
|
{ |
|
29
|
1 |
|
$parameters = []; |
|
30
|
|
|
|
|
31
|
|
|
// Process all options supported |
|
32
|
1 |
|
foreach ($this->options as $option) { |
|
33
|
1 |
|
$parameters[$option] = null; |
|
34
|
|
|
|
|
35
|
|
|
// Create extractor |
|
36
|
1 |
|
$className = 'Recurrence\Rrule\Extractor\\'.$option.'Extractor'; |
|
37
|
1 |
|
$extractor = new $className(); |
|
38
|
|
|
|
|
39
|
1 |
|
if ($values = $extractor->extract($rRule)) { |
|
40
|
|
|
// Create corresponding transformer |
|
41
|
1 |
|
$className = 'Recurrence\Rrule\Transformer\\'.$option.'Transformer'; |
|
42
|
1 |
|
$transformer = new $className(); |
|
43
|
|
|
|
|
44
|
1 |
|
$parameters[$option] = $transformer->transform($values); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
$this->validate($parameters); |
|
49
|
|
|
|
|
50
|
1 |
|
return new Recurrence( |
|
51
|
1 |
|
$parameters['Freq'], |
|
52
|
1 |
|
$parameters['Interval'], |
|
53
|
1 |
|
$parameters['DtStartTimezoned'] ?? $parameters['DtStart'], |
|
54
|
1 |
|
$parameters['UntilTimezoned'] ?? $parameters['Until'], |
|
55
|
1 |
|
$parameters['Count'], |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
private function validate(array $parameters): void |
|
60
|
|
|
{ |
|
61
|
1 |
|
if (empty($parameters['Freq'])) { |
|
62
|
1 |
|
throw new InvalidRruleExpressionException(sprintf('Missing [Freq] option.')); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
if (empty($parameters['Interval'])) { |
|
66
|
|
|
throw new InvalidRruleExpressionException(sprintf('Missing [Interval] option.')); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
if (empty($parameters['DtStart']) && empty($parameters['DtStartTimezoned'])) { |
|
70
|
|
|
throw new InvalidRruleExpressionException(sprintf('Missing [DtStart] or [DtStartTimezoned] option.')); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
if (empty($parameters['UntilTimezoned']) && empty($parameters['Until']) && empty($parameters['Count'])) { |
|
74
|
1 |
|
throw new InvalidRruleExpressionException(sprintf('Recurrence required [COUNT] or [UNTIL] option.')); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
if ((!empty($parameters['UntilTimezoned']) || !empty($parameters['Until'])) && !empty($parameters['Count'])) { |
|
78
|
1 |
|
throw new InvalidRruleExpressionException(sprintf('Recurrence cannot have [COUNT] and [UNTIL] option at the same time.')); |
|
79
|
|
|
} |
|
80
|
1 |
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|