1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MyBuilder\Cronos\Formatter; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Rather than using * /5 for 5 minutes you can simply use /5 |
7
|
|
|
* |
8
|
|
|
* |
9
|
|
|
* field allowed values |
10
|
|
|
* ----- -------------- |
11
|
|
|
* minute 0-59 |
12
|
|
|
* hour 0-23 |
13
|
|
|
* day of month 1-31 |
14
|
|
|
* month 1-12 (or names, see below) |
15
|
|
|
* day of week 0-7 (0 or 7 is Sun, or use names) |
16
|
|
|
* |
17
|
|
|
* Ranges of numbers are allowed. Ranges are two numbers separated with a |
18
|
|
|
* hyphen. The specified range is inclusive. For example, 8-11 for an |
19
|
|
|
* ``hours'' entry specifies execution at hours 8, 9, 10 and 11. |
20
|
|
|
* |
21
|
|
|
* Lists are allowed. A list is a set of numbers (or ranges) separated by |
22
|
|
|
* commas. Examples: ``1,2,5,9'', ``0-4,8-12''. |
23
|
|
|
*/ |
24
|
|
|
class Time |
25
|
|
|
{ |
26
|
|
|
public const FORMAT = '%-4s %-4s %-4s %-4s %-4s '; |
27
|
|
|
public const WILDCARD_TIME = '*'; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
private $minute = self::WILDCARD_TIME; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
private $hour = self::WILDCARD_TIME; |
34
|
|
|
|
35
|
|
|
/** @var string */ |
36
|
|
|
private $dayOfMonth = self::WILDCARD_TIME; |
37
|
|
|
|
38
|
|
|
/** @var string */ |
39
|
|
|
private $month = self::WILDCARD_TIME; |
40
|
|
|
|
41
|
|
|
/** @var string */ |
42
|
|
|
private $dayOfWeek = self::WILDCARD_TIME; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $value 0-59 or a list or range |
46
|
|
|
*/ |
47
|
|
|
public function setMinute(string $value): self |
48
|
|
|
{ |
49
|
|
|
$this->minute = $this->parse($value); |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $value 0-23 or a list or range |
56
|
|
|
*/ |
57
|
|
|
public function setHour(string $value): self |
58
|
|
|
{ |
59
|
|
|
$this->hour = $this->parse($value); |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $value 0-31 or a list or range |
66
|
|
|
*/ |
67
|
|
|
public function setDayOfMonth(string $value): self |
68
|
|
|
{ |
69
|
|
|
$this->dayOfMonth = $this->parse($value); |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $value 0-12 or a list or range |
76
|
|
|
*/ |
77
|
|
|
public function setMonth(string $value): self |
78
|
|
|
{ |
79
|
|
|
$this->month = $this->parse($value); |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $value 0-7 (0 or 7 is Sun, or use names) or a list or range |
86
|
|
|
*/ |
87
|
|
|
public function setDayOfWeek(string $value): self |
88
|
|
|
{ |
89
|
|
|
$this->dayOfWeek = $this->parse($value); |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function format(): string |
95
|
|
|
{ |
96
|
|
|
return \sprintf(self::FORMAT, $this->minute, $this->hour, $this->dayOfMonth, $this->month, $this->dayOfWeek); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function parse(string $value): string |
100
|
|
|
{ |
101
|
|
|
if (0 === \strpos($value, '/')) { |
102
|
|
|
return self::WILDCARD_TIME . $value; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $value; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|