@@ -121,14 +121,14 @@ public static function getDues(array $jobs, $time = null): array
121
121
*/
122
122
public function isCronDue (string $ expr , $ time = null ): bool
123
123
{
124
- list ( $ expr , $ times ) = $ this ->process ( $ expr , $ time );
124
+ $ this ->checker -> setReference ( new ReferenceTime ( $ time) );
125
125
126
- foreach ($ expr as $ pos => $ segment ) {
126
+ foreach (\explode ( ' ' , $ this -> normalizeExpr ( $ expr)) as $ pos => $ segment ) {
127
127
if ($ segment === '* ' || $ segment === '? ' ) {
128
128
continue ;
129
129
}
130
130
131
- if (!$ this ->checker ->checkDue ($ segment , $ pos, $ times )) {
131
+ if (!$ this ->checker ->checkDue ($ segment , $ pos )) {
132
132
return false ;
133
133
}
134
134
}
@@ -147,7 +147,6 @@ public function isCronDue(string $expr, $time = null): bool
147
147
public function filter (array $ jobs , $ time = null ): array
148
148
{
149
149
$ dues = $ cache = [];
150
- $ time = $ this ->normalizeTime ($ time );
151
150
152
151
foreach ($ jobs as $ name => $ expr ) {
153
152
$ expr = $ this ->normalizeExpr ($ expr );
@@ -164,44 +163,6 @@ public function filter(array $jobs, $time = null): array
164
163
return $ dues ;
165
164
}
166
165
167
- /**
168
- * Process and prepare input.
169
- *
170
- * @param string $expr
171
- * @param mixed $time
172
- *
173
- * @return array [expr, time]
174
- */
175
- protected function process (string $ expr , $ time ): array
176
- {
177
- $ expr = $ this ->normalizeExpr ($ expr );
178
- $ expr = \preg_split ('~\s+~ ' , $ expr ); // 14
179
-
180
- if (\count ($ expr ) < 5 || \count ($ expr ) > 6 ) {
181
- throw new \UnexpectedValueException (
182
- 'Cron $expr should have 5 or 6 segments delimited by space '
183
- );
184
- }
185
-
186
- $ time = static ::normalizeTime ($ time );
187
- $ times = \array_map ('intval ' , \explode (' ' , \date ('i G j n w Y t d m N ' , $ time )));
188
-
189
- return [$ expr , $ times ];
190
- }
191
-
192
- protected function normalizeTime ($ time ): int
193
- {
194
- if (empty ($ time )) {
195
- $ time = \time ();
196
- } elseif (\is_string ($ time )) {
197
- $ time = \strtotime ($ time );
198
- } elseif ($ time instanceof \DateTime) {
199
- $ time = $ time ->getTimestamp ();
200
- }
201
-
202
- return $ time ;
203
- }
204
-
205
166
protected function normalizeExpr (string $ expr ): string
206
167
{
207
168
$ expr = \trim ($ expr );
@@ -210,10 +171,15 @@ protected function normalizeExpr(string $expr): string
210
171
return static ::$ expressions [$ expr ];
211
172
}
212
173
213
- return \str_ireplace (
214
- \array_keys (static ::$ literals ),
215
- \array_values (static ::$ literals ),
216
- $ expr
217
- );
174
+ $ expr = \preg_replace ('~\s+~ ' , ' ' , \strtr ($ expr , static ::$ literals ));
175
+ $ count = \substr_count ($ expr , ' ' );
176
+
177
+ if ($ count < 4 || $ count > 5 ) {
178
+ throw new \UnexpectedValueException (
179
+ 'Cron $expr should have 5 or 6 segments delimited by space '
180
+ );
181
+ }
182
+
183
+ return $ expr ;
218
184
}
219
185
}
0 commit comments