-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathExpression.php
More file actions
218 lines (170 loc) · 4.97 KB
/
Expression.php
File metadata and controls
218 lines (170 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
declare(strict_types=1);
namespace SchedulerBundle\Expression;
use Stringable;
use SchedulerBundle\Exception\InvalidArgumentException;
use SchedulerBundle\Exception\InvalidExpressionException;
use function array_key_exists;
use function count;
use function explode;
use function implode;
use function sprintf;
/**
* @author Guillaume Loulier <[email protected]>
*/
final class Expression implements Stringable
{
/**
* @var string
*/
public const ANNUALLY_MACRO = '@annually';
/**
* @var string
*/
public const YEARLY_MACRO = '@yearly';
/**
* @var string
*/
public const REBOOT_MACRO = '@reboot';
/**
* @var string
*/
public const DAILY_MACRO = '@daily';
/**
* @var string
*/
public const WEEKLY_MACRO = '@weekly';
/**
* @var string
*/
public const MONTHLY_MACRO = '@monthly';
public const ALLOWED_MACROS = [
self::ANNUALLY_MACRO => '0 0 1 1 *',
self::YEARLY_MACRO => '0 0 1 1 *',
self::DAILY_MACRO => '0 0 * * *',
self::WEEKLY_MACRO => '0 0 * * 0',
self::MONTHLY_MACRO => '0 0 1 * *',
self::REBOOT_MACRO => '@reboot',
];
private string $expression = '* * * * *';
public function __toString(): string
{
return $this->expression;
}
public static function createFromString(string $expression): self
{
$self = new self();
$self->setExpression($expression);
return $self;
}
public function setExpression(string $expression): void
{
if (str_starts_with($expression, '@')) {
$this->setMacro($expression);
return;
}
$this->expression = $expression;
}
public function getExpression(): string
{
return $this->expression;
}
public function everySpecificMinutes(string $minutes): string
{
return $this->changeExpression(0, $minutes);
}
public function everySpecificHours(string $hours): string
{
return $this->changeExpression(1, $hours);
}
public function everySpecificDays(string $days): string
{
return $this->changeExpression(2, $days);
}
public function everySpecificDaysOfWeek(string $days): string
{
return $this->changeExpression(4, $days);
}
public function everySpecificMonths(string $months): string
{
return $this->changeExpression(3, $months);
}
public function every5Minutes(): string
{
return $this->changeExpression(0, '*/5');
}
public function every10Minutes(): string
{
return $this->changeExpression(0, '*/10');
}
public function every15Minutes(): string
{
return $this->changeExpression(0, '*/15');
}
public function every20Minutes(): string
{
return $this->changeExpression(0, '*/20');
}
public function every25Minutes(): string
{
return $this->changeExpression(0, '*/25');
}
public function every30Minutes(): string
{
return $this->changeExpression(0, '*/30');
}
public function everyHours(): string
{
return $this->changeExpression(0, '0');
}
public function everyDays(): string
{
$this->setExpression(self::ALLOWED_MACROS[self::DAILY_MACRO]);
return $this->expression;
}
public function everyWeeks(): string
{
$this->setExpression(self::ALLOWED_MACROS[self::WEEKLY_MACRO]);
return $this->expression;
}
public function everyMonths(): string
{
$this->setExpression(self::ALLOWED_MACROS[self::MONTHLY_MACRO]);
return $this->expression;
}
public function everyYears(): string
{
$this->setExpression(self::ALLOWED_MACROS[self::YEARLY_MACRO]);
return $this->expression;
}
public function at(string $time): string
{
$fields = explode(':', $time);
$this->changeExpression(0, 2 === count($fields) ? $fields[1] : '0');
$this->changeExpression(1, $fields[0]);
return $this->expression;
}
private function setMacro(string $macro): string
{
if (!array_key_exists($macro, self::ALLOWED_MACROS)) {
throw new InvalidExpressionException(sprintf('The desired macro "%s" is not supported!', $macro));
}
$this->expression = $macro;
return $this->expression;
}
/**
* @param int $position A valid position (refer to cron syntax if needed)
* @param string $value A valid value (typed to string to prevent type changes)
*
* @return string The updated expression
*/
private function changeExpression(int $position, string $value): string
{
$fields = explode(' ', $this->expression);
if (!array_key_exists($position, $fields)) {
throw new InvalidArgumentException('The desired position is not valid');
}
$fields[$position] = $value;
return $this->expression = implode(' ', $fields);
}
}