|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Scheduler\Job; |
|
4
|
|
|
|
|
5
|
|
|
use DateTimeInterface; |
|
6
|
|
|
use Recurr\Rule as RecurrRule; |
|
7
|
|
|
use Recurr\Recurrence; |
|
8
|
|
|
use Recurr\Transformer\ArrayTransformer; |
|
9
|
|
|
use Recurr\Transformer\Constraint\AfterConstraint; |
|
10
|
|
|
use Recurr\Transformer\Constraint\BetweenConstraint; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class RRule |
|
14
|
|
|
* @package Scheduler\Job |
|
15
|
|
|
* @author Aleh Hutnikau, <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class RRule extends AbstractRule |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param DateTimeInterface $from |
|
22
|
|
|
* @param DateTimeInterface $to |
|
23
|
|
|
* @param boolean $inc |
|
24
|
|
|
* @throws |
|
25
|
|
|
* @return DateTimeInterface[] |
|
26
|
|
|
*/ |
|
27
|
14 |
|
public function getRecurrences(DateTimeInterface $from, DateTimeInterface $to, $inc = true) |
|
28
|
|
|
{ |
|
29
|
14 |
|
$result = []; |
|
30
|
14 |
|
$recurrenceCollection = $this->getCollection(new BetweenConstraint($from, $to, $inc)); |
|
31
|
|
|
/** @var Recurrence $recurrence */ |
|
32
|
14 |
|
foreach ($recurrenceCollection as $recurrence) { |
|
33
|
13 |
|
$result[] = $recurrence->getStart(); |
|
34
|
14 |
|
} |
|
35
|
14 |
|
return $result; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param DateTimeInterface $from |
|
40
|
|
|
* @param boolean $inc including $from and $to dates |
|
41
|
|
|
* @return DateTimeInterface|null date of the next recurrence or null of no more recurrences scheduled. |
|
42
|
|
|
* @throws |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public function getNextRecurrence(DateTimeInterface $from, $inc = true) |
|
45
|
|
|
{ |
|
46
|
1 |
|
$result = null; |
|
47
|
1 |
|
$recurrenceCollection = $this->getCollection(new AfterConstraint($from, $inc)); |
|
48
|
1 |
|
if ($first = $recurrenceCollection->first()) { |
|
49
|
1 |
|
$result = $first->getStart(); |
|
50
|
1 |
|
} |
|
51
|
1 |
|
return $result; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get recurrence collection by given constraint |
|
56
|
|
|
* |
|
57
|
|
|
* @param $constraint |
|
58
|
|
|
* @return Recurrence[]|\Recurr\RecurrenceCollection |
|
59
|
|
|
* @throws \Recurr\Exception\InvalidRRule |
|
60
|
|
|
* @throws \Recurr\Exception\InvalidWeekday |
|
61
|
|
|
*/ |
|
62
|
15 |
|
private function getCollection($constraint) |
|
63
|
1 |
|
{ |
|
64
|
15 |
|
$rRule = new RecurrRule($this->getRrule(), $this->getStartDate()); |
|
65
|
15 |
|
$rRuleTransformer = new ArrayTransformer(); |
|
66
|
15 |
|
return $rRuleTransformer->transform($rRule, $constraint); |
|
67
|
|
|
} |
|
68
|
|
|
} |