1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recurrence; |
4
|
|
|
|
5
|
|
|
use Recurrence\RruleTransformer\CountTransformer; |
6
|
|
|
use Recurrence\RruleTransformer\DtStartTransformer; |
7
|
|
|
use Recurrence\RruleTransformer\FreqTransformer; |
8
|
|
|
use Recurrence\RruleTransformer\IntervalTransformer; |
9
|
|
|
use Recurrence\RruleTransformer\UntilTransformer; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class RecurrenceProvider |
13
|
|
|
* @package Recurrence |
14
|
|
|
*/ |
15
|
|
|
class RecurrenceProvider |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var FreqTransformer |
20
|
|
|
*/ |
21
|
|
|
private $freqTransformer; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var DtStartTransformer |
25
|
|
|
*/ |
26
|
|
|
private $dtStartTransformer; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var UntilTransformer |
30
|
|
|
*/ |
31
|
|
|
private $untilTransformer; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var IntervalTransformer |
35
|
|
|
*/ |
36
|
|
|
private $intervalTransformer; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* RecurrenceProvider constructor. |
40
|
|
|
*/ |
41
|
|
|
public function __construct() |
42
|
|
|
{ |
43
|
1 |
|
$this->freqTransformer = new FreqTransformer(); |
44
|
1 |
|
$this->dtStartTransformer = new DtStartTransformer(); |
45
|
1 |
|
$this->untilTransformer = new UntilTransformer(); |
46
|
1 |
|
$this->intervalTransformer = new IntervalTransformer(); |
47
|
1 |
|
$this->countTransformer = new CountTransformer(); |
|
|
|
|
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $rRule |
52
|
|
|
* @return Recurrence |
53
|
|
|
* @throws \InvalidArgumentException |
54
|
|
|
*/ |
55
|
|
|
public function parse($rRule) |
56
|
|
|
{ |
57
|
1 |
|
if (empty($rRule)) { |
58
|
1 |
|
throw new \InvalidArgumentException('Empty RRULE'); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
$recurrence = new Recurrence(); |
62
|
|
|
|
63
|
1 |
|
$recurrence->setFrequency($this->freqTransformer->transform($rRule)); |
64
|
|
|
|
65
|
1 |
|
if ($periodStartAt = $this->dtStartTransformer->transform($rRule)) { |
66
|
1 |
|
$recurrence->setPeriodStartAt($periodStartAt); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
1 |
|
if ($periodStartAt = $this->untilTransformer->transform($rRule)) { |
70
|
1 |
|
$recurrence->setPeriodEndAt($periodStartAt); |
71
|
1 |
|
} |
72
|
|
|
|
73
|
1 |
|
if ($interval = $this->intervalTransformer->transform($rRule)) { |
74
|
1 |
|
$recurrence->setInterval($interval); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
1 |
|
if ($interval = $this->countTransformer->transform($rRule)) { |
78
|
1 |
|
$recurrence->setCount($interval); |
79
|
1 |
|
} |
80
|
|
|
|
81
|
1 |
|
if ($recurrence->hasCount() && $recurrence->getPeriodEndAt()) { |
82
|
1 |
|
throw new \InvalidArgumentException('Recurrence cannot have [UNTIL] and [COUNT] option at the same time'); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
if (!$recurrence->hasCount() && !$recurrence->getPeriodEndAt()) { |
86
|
1 |
|
throw new \InvalidArgumentException('Recurrence required an [UNTIL] or [COUNT] option'); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
return $recurrence; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: