|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Scheduler\Job; |
|
4
|
|
|
|
|
5
|
|
|
use DateTimeInterface; |
|
6
|
|
|
use DateTimeZone; |
|
7
|
|
|
use DateTime; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Job |
|
11
|
|
|
* @package Job |
|
12
|
|
|
* @author Aleh Hutnikau, <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class Job implements JobInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var RRule */ |
|
17
|
|
|
private $rRule; |
|
18
|
|
|
|
|
19
|
|
|
/** @var callable */ |
|
20
|
|
|
private $callable; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Job constructor. |
|
24
|
|
|
* @param RRule $rRule - recurrence rules (@see https://github.com/simshaun/recurr) |
|
25
|
|
|
* @param callable $callable |
|
26
|
|
|
*/ |
|
27
|
8 |
|
public function __construct(RRule $rRule, callable $callable) |
|
28
|
|
|
{ |
|
29
|
8 |
|
$this->rRule = $rRule; |
|
30
|
8 |
|
$this->callable = $callable; |
|
31
|
8 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return RRule |
|
35
|
|
|
*/ |
|
36
|
15 |
|
public function getRRule() |
|
37
|
|
|
{ |
|
38
|
15 |
|
return $this->rRule; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return callable |
|
43
|
|
|
*/ |
|
44
|
2 |
|
public function getCallable() |
|
45
|
|
|
{ |
|
46
|
2 |
|
return $this->callable; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $rRule RRULE string |
|
51
|
|
|
* @param string|DateTimeInterface $startDate - @see DateTime supported formats |
|
52
|
|
|
* @param callable $callback |
|
53
|
|
|
* @param string|DateTimeZone $timezone - If $timezone is omitted, the current timezone will be used. |
|
54
|
|
|
* If $startDate instance of `DateTimeInterface` and $timezone was given then $startDate's timezone will be overwritten. |
|
55
|
|
|
* @return Job |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public static function createFromString($rRule, $startDate, callable $callback, $timezone = null) |
|
58
|
|
|
{ |
|
59
|
1 |
|
if ($timezone === null) { |
|
60
|
1 |
|
$timezone = $startDate instanceof DateTimeInterface? |
|
61
|
1 |
|
$startDate->getTimezone() : |
|
62
|
1 |
|
new \DateTimeZone(date_default_timezone_get()); |
|
63
|
|
|
} |
|
64
|
1 |
|
if (is_string($timezone)) { |
|
65
|
1 |
|
$timezone = new \DateTimeZone($timezone); |
|
66
|
|
|
} |
|
67
|
1 |
|
if (!$startDate instanceof DateTimeInterface) { |
|
68
|
1 |
|
$startDate = new DateTime($startDate, $timezone); |
|
69
|
|
|
} |
|
70
|
1 |
|
$startDate->setTimezone($timezone); |
|
71
|
1 |
|
$rRule = new RRule($rRule, $startDate); |
|
72
|
1 |
|
return new self($rRule, $callback); |
|
73
|
|
|
} |
|
74
|
|
|
} |