@@ -121,14 +121,14 @@ public static function getDues(array $jobs, $time = null): array
121121 */
122122 public function isCronDue (string $ expr , $ time = null ): bool
123123 {
124- list ( $ expr , $ times ) = $ this ->process ( $ expr , $ time );
124+ $ this ->checker -> setReference ( new ReferenceTime ( $ time) );
125125
126- foreach ($ expr as $ pos => $ segment ) {
126+ foreach (\explode ( ' ' , $ this -> normalizeExpr ( $ expr)) as $ pos => $ segment ) {
127127 if ($ segment === '* ' || $ segment === '? ' ) {
128128 continue ;
129129 }
130130
131- if (!$ this ->checker ->checkDue ($ segment , $ pos, $ times )) {
131+ if (!$ this ->checker ->checkDue ($ segment , $ pos )) {
132132 return false ;
133133 }
134134 }
@@ -147,7 +147,6 @@ public function isCronDue(string $expr, $time = null): bool
147147 public function filter (array $ jobs , $ time = null ): array
148148 {
149149 $ dues = $ cache = [];
150- $ time = $ this ->normalizeTime ($ time );
151150
152151 foreach ($ jobs as $ name => $ expr ) {
153152 $ expr = $ this ->normalizeExpr ($ expr );
@@ -164,44 +163,6 @@ public function filter(array $jobs, $time = null): array
164163 return $ dues ;
165164 }
166165
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-
205166 protected function normalizeExpr (string $ expr ): string
206167 {
207168 $ expr = \trim ($ expr );
@@ -210,10 +171,15 @@ protected function normalizeExpr(string $expr): string
210171 return static ::$ expressions [$ expr ];
211172 }
212173
213- return \str_ireplace (
214- \array_keys (static ::$ literals ),
215- \array_values (static ::$ literals ),
216- $ expr
217- );
174+ $ expr = \preg_replace ('~\s+~ ' , ' ' , $ expr );
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 \str_ireplace (\array_keys (static ::$ literals ), \array_values (static ::$ literals ), $ expr );
218184 }
219185}
0 commit comments