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 array |
20
|
|
|
*/ |
21
|
|
|
private $transformers; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* RecurrenceProvider constructor. |
25
|
|
|
*/ |
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
1 |
|
$this->transformers = [ |
29
|
1 |
|
'frequency' => new FreqTransformer(), |
30
|
1 |
|
'periodStartAt' => new DtStartTransformer(), |
31
|
1 |
|
'periodEndAt' => new UntilTransformer(), |
32
|
1 |
|
'interval' => new IntervalTransformer(), |
33
|
1 |
|
'count' => new CountTransformer(), |
34
|
|
|
]; |
35
|
1 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $rRule |
39
|
|
|
* @return Recurrence |
40
|
|
|
* @throws \InvalidArgumentException |
41
|
|
|
*/ |
42
|
|
|
public function create($rRule) |
43
|
|
|
{ |
44
|
1 |
|
if (empty($rRule)) { |
45
|
1 |
|
throw new \InvalidArgumentException('Empty RRULE'); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
$recurrence = new Recurrence(); |
49
|
|
|
|
50
|
|
|
// Process each transformer and set value if available in RRULE expression |
51
|
1 |
|
foreach ($this->transformers as $attribute => $transformer) { |
52
|
1 |
|
if ($value = $transformer->transform($rRule)) { |
53
|
1 |
|
$recurrence = $this->setAttribute($recurrence, $attribute, $value); |
54
|
1 |
|
} |
55
|
1 |
|
} |
56
|
|
|
|
57
|
1 |
|
if (!$recurrence->getFrequency()) { |
58
|
1 |
|
throw new \InvalidArgumentException(sprintf('Recurrence [%s] option is required', FreqTransformer::RRULE_PARAMETER)); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
View Code Duplication |
if ($recurrence->hasCount() && $recurrence->getPeriodEndAt()) { |
|
|
|
|
62
|
1 |
|
throw new \InvalidArgumentException(sprintf('Recurrence cannot have [%s] and [%s] option at the same time', UntilTransformer::RRULE_PARAMETER, CountTransformer::RRULE_PARAMETER)); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
View Code Duplication |
if (!$recurrence->hasCount() && !$recurrence->getPeriodEndAt()) { |
|
|
|
|
66
|
1 |
|
throw new \InvalidArgumentException(sprintf('Recurrence required an [%s] or [%s] option', UntilTransformer::RRULE_PARAMETER, CountTransformer::RRULE_PARAMETER)); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
return $recurrence; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Recurrence $recurrence |
74
|
|
|
* @param string $attribute |
75
|
|
|
* @param mixed $value |
76
|
|
|
* @return Recurrence |
77
|
|
|
*/ |
78
|
|
|
private function setAttribute(Recurrence $recurrence, $attribute, $value) |
79
|
|
|
{ |
80
|
1 |
|
$method = sprintf('set%s', ucfirst($attribute)); |
81
|
|
|
|
82
|
1 |
|
if (method_exists($recurrence, $method)) { |
83
|
1 |
|
$recurrence->$method($value); |
84
|
1 |
|
} |
85
|
|
|
|
86
|
1 |
|
return $recurrence; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.