Skip to content

Commit 42700ab

Browse files
committed
refactor: use Normalizer instead
1 parent afb57c4 commit 42700ab

File tree

1 file changed

+8
-60
lines changed

1 file changed

+8
-60
lines changed

src/Expression.php

Lines changed: 8 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -29,45 +29,13 @@ class Expression
2929
/** @var SegmentChecker */
3030
protected $checker;
3131

32-
protected static $expressions = [
33-
'@yearly' => '0 0 1 1 *',
34-
'@annually' => '0 0 1 1 *',
35-
'@monthly' => '0 0 1 * *',
36-
'@weekly' => '0 0 * * 0',
37-
'@daily' => '0 0 * * *',
38-
'@hourly' => '0 * * * *',
39-
'@always' => '* * * * *',
40-
'@5minutes' => '*/5 * * * *',
41-
'@10minutes' => '*/10 * * * *',
42-
'@15minutes' => '*/15 * * * *',
43-
'@30minutes' => '0,30 * * * *',
44-
];
45-
46-
protected static $literals = [
47-
'sun' => 0,
48-
'mon' => 1,
49-
'tue' => 2,
50-
'wed' => 3,
51-
'thu' => 4,
52-
'fri' => 5,
53-
'sat' => 6,
54-
'jan' => 1,
55-
'feb' => 2,
56-
'mar' => 3,
57-
'apr' => 4,
58-
'may' => 5,
59-
'jun' => 6,
60-
'jul' => 7,
61-
'aug' => 8,
62-
'sep' => 9,
63-
'oct' => 10,
64-
'nov' => 11,
65-
'dec' => 12,
66-
];
67-
68-
public function __construct(SegmentChecker $checker = null)
32+
/** @var Normalizer */
33+
protected $normalizer;
34+
35+
public function __construct(SegmentChecker $checker = null, Normalizer $normalizer = null)
6936
{
70-
$this->checker = $checker ?: new SegmentChecker;
37+
$this->checker = $checker ?: new SegmentChecker;
38+
$this->normalizer = $normalizer ?: new Normalizer;
7139

7240
if (null === static::$instance) {
7341
static::$instance = $this;
@@ -123,7 +91,7 @@ public function isCronDue(string $expr, $time = null): bool
12391
{
12492
$this->checker->setReference(new ReferenceTime($time));
12593

126-
foreach (\explode(' ', $this->normalizeExpr($expr)) as $pos => $segment) {
94+
foreach (\explode(' ', $this->normalizer->normalizeExpr($expr)) as $pos => $segment) {
12795
if ($segment === '*' || $segment === '?') {
12896
continue;
12997
}
@@ -149,7 +117,7 @@ public function filter(array $jobs, $time = null): array
149117
$dues = $cache = [];
150118

151119
foreach ($jobs as $name => $expr) {
152-
$expr = $this->normalizeExpr($expr);
120+
$expr = $this->normalizer->normalizeExpr($expr);
153121

154122
if (!isset($cache[$expr])) {
155123
$cache[$expr] = $this->isCronDue($expr, $time);
@@ -162,24 +130,4 @@ public function filter(array $jobs, $time = null): array
162130

163131
return $dues;
164132
}
165-
166-
protected function normalizeExpr(string $expr): string
167-
{
168-
$expr = \trim($expr);
169-
170-
if (isset(static::$expressions[$expr])) {
171-
return static::$expressions[$expr];
172-
}
173-
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);
184-
}
185133
}

0 commit comments

Comments
 (0)