1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scheduler\Job; |
4
|
|
|
|
5
|
|
|
use DateTimeInterface; |
6
|
|
|
use Cron\CronExpression; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class CronRule |
10
|
|
|
* @package Scheduler\Job |
11
|
|
|
* @author Aleh Hutnikau, <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class CronRule implements RRuleInterface |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
private $startDate; |
17
|
|
|
private $rRule; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* RRule constructor. |
21
|
|
|
* @param string $rRule |
22
|
|
|
* @param string|DateTimeInterface $startDate |
23
|
|
|
*/ |
24
|
3 |
|
public function __construct($rRule, DateTimeInterface $startDate) |
25
|
|
|
{ |
26
|
3 |
|
$this->rRule = $rRule; |
27
|
3 |
|
$this->startDate = $startDate; |
28
|
3 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return DateTimeInterface |
32
|
|
|
*/ |
33
|
2 |
|
public function getStartDate() |
34
|
|
|
{ |
35
|
2 |
|
return $this->startDate; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return string RRULE string |
40
|
|
|
*/ |
41
|
2 |
|
public function getRrule() |
42
|
|
|
{ |
43
|
2 |
|
return $this->rRule; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param DateTimeInterface $from |
48
|
|
|
* @param DateTimeInterface $to |
49
|
|
|
* @param boolean $inc |
50
|
|
|
* @throws |
51
|
|
|
* @return DateTimeInterface[] |
52
|
|
|
*/ |
53
|
1 |
|
public function getRecurrences(DateTimeInterface $from, DateTimeInterface $to, $inc = true) |
54
|
|
|
{ |
55
|
1 |
|
$rRule = CronExpression::factory($this->getRrule()); |
56
|
1 |
|
$result = []; |
57
|
|
|
|
58
|
1 |
|
if ($to->getTimestamp() < $this->getStartDate()->getTimestamp()) { |
59
|
1 |
|
return $result; |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
if ($from->getTimestamp() < $this->getStartDate()->getTimestamp()) { |
63
|
1 |
|
$from = clone $this->getStartDate(); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$firstIteration = true; |
67
|
|
|
|
68
|
|
|
do { |
69
|
1 |
|
$nextRunDate = $rRule->getNextRunDate($from, 0, $firstIteration && $inc); |
70
|
1 |
|
if ($nextRunDate->getTimestamp() < ($to->getTimestamp() + (int) $inc) && $from->getTimestamp() <= $nextRunDate->getTimestamp()) { |
71
|
1 |
|
$result[] = $nextRunDate; |
72
|
|
|
} |
73
|
1 |
|
$firstIteration = false; |
74
|
1 |
|
$from = $nextRunDate; |
75
|
1 |
|
} while ($nextRunDate->getTimestamp() < ($to->getTimestamp() + (int) $inc)); |
76
|
|
|
|
77
|
1 |
|
return $result; |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
// while(($nextRunDate = $rRule->getNextRunDate($from, 0, $inc)->getTimestamp()) < ($to->getTimestamp() + (int) $inc)) { |
81
|
|
|
// $nextRunDate = $rRule->getNextRunDate($from, 0, $inc); |
82
|
|
|
// $result[] = $recurrence->getStart() |
83
|
|
|
// } |
84
|
|
|
|
85
|
|
|
// $rRule = new RecurrRule($this->getRrule(), $this->getStartDate()); |
86
|
|
|
// $rRuleTransformer = new ArrayTransformer(); |
87
|
|
|
// $constraint = new BetweenConstraint($from, $to, $inc); |
88
|
|
|
// $recurrenceCollection = $rRuleTransformer->transform($rRule, $constraint); |
89
|
|
|
// $result = []; |
90
|
|
|
// /** @var Recurrence $recurrence */ |
91
|
|
|
// foreach ($recurrenceCollection as $recurrence) { |
92
|
|
|
// $result[] = $recurrence->getStart(); |
93
|
|
|
// } |
94
|
|
|
// return $result; |
95
|
|
|
} |
96
|
|
|
} |