Skip to content

Commit ab89f30

Browse files
authored
Merge pull request #20 from adhocore/fix-step-and-weekday-check
Fix step and weekday check
2 parents 34250c8 + e264210 commit ab89f30

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

src/Normalizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public function normalizeExpr(string $expr): string
6868
{
6969
$expr = \trim($expr);
7070

71-
if (isset(static::$expressions[$expr])) {
72-
return static::$expressions[$expr];
71+
if (isset(static::$expressions[$exp = \strtolower($expr)])) {
72+
return static::$expressions[$exp];
7373
}
7474

7575
$expr = \preg_replace('~\s+~', ' ', $expr);

src/SegmentChecker.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,5 @@ protected function checkModifier(string $offset, int $pos): bool
9999
$this->validator->unexpectedValue($pos, $offset);
100100
// @codeCoverageIgnoreStart
101101
}
102-
103102
// @codeCoverageIgnoreEnd
104103
}

src/Validator.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function inStep(int $value, string $offset): bool
5353
return false;
5454
}
5555

56-
if (\strpos($offset, '*/') !== false || \strpos($offset, '0/') !== false) {
56+
if (\strpos($offset, '*/') === 0 || \strpos($offset, '0/') === 0) {
5757
return $value % $parts[1] === 0;
5858
}
5959

@@ -91,7 +91,7 @@ public function inStepRange(int $value, int $start, int $end, int $step): bool
9191
* @internal
9292
*
9393
* @param string $value
94-
* @param ReferenceTime $time
94+
* @param ReferenceTime $reference
9595
*
9696
* @return bool
9797
*/
@@ -140,7 +140,7 @@ protected function isClosestWeekDay(int $value, string $month, ReferenceTime $re
140140
* @internal
141141
*
142142
* @param string $value
143-
* @param ReferenceTime $time
143+
* @param ReferenceTime $reference
144144
*
145145
* @return bool
146146
*/
@@ -156,16 +156,18 @@ public function isValidWeekDay(string $value, ReferenceTime $reference): bool
156156
$this->unexpectedValue(4, $value);
157157
}
158158

159-
list($day, $nth) = \explode('#', \str_replace('0#', '7#', $value));
159+
list($day, $nth) = \explode('#', \str_replace('7#', '0#', $value));
160160

161-
if (!$this->isNthWeekDay((int) $day, (int) $nth) || $reference->weekDay1() != $day) {
161+
if (!$this->isNthWeekDay((int) $day, (int) $nth) || $reference->weekDay() != $day) {
162162
return false;
163163
}
164164

165165
return \intval($reference->day() / 7) == $nth - 1;
166166
}
167167

168168
/**
169+
* Throws UnexpectedValueException.
170+
*
169171
* @param int $pos
170172
* @param string $value
171173
*

tests/ExpressionTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,19 @@ public function scheduleProvider()
175175
['5/20 * * * *', '2018-08-13 00:24:00', '2011-07-27 00:00:00', false], // issue #12
176176
['5/20 * * * *', strtotime('2018-08-13 00:45:00'), '2011-07-27 00:00:00', true], // issue #12
177177
['5-11/4 * * * *', strtotime('2018-08-13 00:03:00'), '2011-07-27 00:00:00', false],
178+
179+
// New from https://github.com/dragonmantank/cron-expression
180+
['0 0 L * 0', strtotime('2011-06-15 23:09:00'), '2011-06-19 00:00:00', false],
181+
['3-59/15 6-12 */15 1 2-5', strtotime('2017-01-08 00:00:00'), '2017-01-10 06:03:00', false],
182+
['* * * * MON-FRI', strtotime('2017-01-08 00:00:00'), strtotime('2017-01-09 00:00:00'), false],
183+
['* * * * TUE', strtotime('2017-01-08 00:00:00'), strtotime('2017-01-10 00:00:00'), false],
184+
['0 1 15 JUL mon,Wed,FRi', strtotime('2019-11-14 00:00:00'), strtotime('2020-07-01 01:00:00'), false],
185+
['0 1 15 jul mon,Wed,FRi', strtotime('2019-11-14 00:00:00'), strtotime('2020-07-01 01:00:00'), false],
186+
['@weekly', strtotime('2019-11-14 00:00:00'), strtotime('2019-11-17 00:00:00'), false],
187+
['@weekly', strtotime('2019-11-14 00:00:00'), strtotime('2019-11-17 00:00:00'), false],
188+
['@weekly', strtotime('2019-11-14 00:00:00'), strtotime('2019-11-17 00:00:00'), false],
189+
['0 12 * * ?', strtotime('2020-08-20 00:00:00'), strtotime('2020-08-20 12:00:00'), false],
190+
['0 12 ? * *', strtotime('2020-08-20 00:00:00'), strtotime('2020-08-20 12:00:00'), false],
178191
];
179192
}
180193
}

0 commit comments

Comments
 (0)