|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Scheduler\Job; |
|
4
|
|
|
|
|
5
|
|
|
use DateTimeInterface; |
|
6
|
|
|
use DateTime; |
|
7
|
|
|
use Cron\CronExpression; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class CronRule |
|
11
|
|
|
* @package Scheduler\Job |
|
12
|
|
|
* @author Aleh Hutnikau, <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class CronRule extends AbstractRule |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param DateTimeInterface $from |
|
19
|
|
|
* @param DateTimeInterface $to |
|
20
|
|
|
* @param boolean $inc |
|
21
|
|
|
* @throws |
|
22
|
|
|
* @return DateTimeInterface[] |
|
23
|
|
|
*/ |
|
24
|
1 |
|
public function getRecurrences(DateTimeInterface $from, DateTimeInterface $to, $inc = true) |
|
25
|
|
|
{ |
|
26
|
1 |
|
$rRule = CronExpression::factory($this->getRrule()); |
|
27
|
1 |
|
$result = []; |
|
28
|
|
|
|
|
29
|
1 |
|
if ($from->getTimestamp() < $this->getStartDate()->getTimestamp()) { |
|
30
|
1 |
|
$from = clone $this->getStartDate(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
1 |
|
if (($to->getTimestamp() + (int) $inc) > $this->getStartDate()->getTimestamp()) { |
|
34
|
|
|
//make sure that $from is DateTime instance |
|
35
|
1 |
|
$from = new DateTime('@'.$from->getTimestamp()); |
|
36
|
1 |
|
$result = $this->getDates($rRule, $from, $to, $inc); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
1 |
|
return $result; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param DateTimeInterface $from |
|
44
|
|
|
* @param boolean $inc including $from and $to dates |
|
45
|
|
|
* @return DateTimeInterface|null date of the next recurrence or null of no more recurrences scheduled. |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function getNextRecurrence(DateTimeInterface $from, $inc = true) |
|
48
|
|
|
{ |
|
49
|
1 |
|
if ($from->getTimestamp() < $this->getStartDate()->getTimestamp()) { |
|
50
|
1 |
|
$from = clone $this->getStartDate(); |
|
51
|
|
|
} |
|
52
|
|
|
//make sure that $from is DateTime instance |
|
53
|
1 |
|
$from = new DateTime('@'.$from->getTimestamp()); |
|
54
|
1 |
|
if ($from->format('s') !== '00') { |
|
55
|
1 |
|
$from->setTime($from->format('H'), $from->format('i') + 1, 0); |
|
56
|
1 |
|
$inc = true; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
$rRule = CronExpression::factory($this->getRrule()); |
|
60
|
1 |
|
return $rRule->getNextRunDate($from, 0, $inc); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param CronExpression $rRule |
|
65
|
|
|
* @param DateTime $from |
|
66
|
|
|
* @param DateTimeInterface $to |
|
67
|
|
|
* @param $inc |
|
68
|
|
|
* @return DateTimeInterface[] |
|
69
|
|
|
*/ |
|
70
|
1 |
|
private function getDates(CronExpression $rRule, DateTime $from, DateTimeInterface $to, $inc) |
|
71
|
|
|
{ |
|
72
|
1 |
|
$result = []; |
|
73
|
1 |
|
$toTimestamp = $to->getTimestamp() + (int) $inc; |
|
74
|
|
|
do { |
|
75
|
1 |
|
$nextRunDate = $rRule->getNextRunDate($from, 0, $inc); |
|
76
|
1 |
|
$nextRunTimestamp = $nextRunDate->getTimestamp(); |
|
77
|
1 |
|
if ($nextRunTimestamp < $toTimestamp && $from->getTimestamp() <= $nextRunTimestamp) { |
|
78
|
1 |
|
$result[] = $nextRunDate; |
|
79
|
|
|
} |
|
80
|
1 |
|
$inc = false; |
|
81
|
1 |
|
$from = $nextRunDate; |
|
82
|
1 |
|
} while ($nextRunTimestamp < $toTimestamp); |
|
83
|
1 |
|
return $result; |
|
84
|
|
|
} |
|
85
|
|
|
} |