Skip to content

Commit 1ce873d

Browse files
committed
refactor(expr): use reference time class, cleanup process()
1 parent 2ad504b commit 1ce873d

File tree

1 file changed

+13
-47
lines changed

1 file changed

+13
-47
lines changed

src/Expression.php

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -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+~', ' ', \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;
218184
}
219185
}

0 commit comments

Comments
 (0)