-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSegmentChecker.php
More file actions
105 lines (86 loc) · 2.48 KB
/
Copy pathSegmentChecker.php
File metadata and controls
105 lines (86 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
declare(strict_types=1);
/*
* This file is part of the PHP-CRON-EXPR package.
*
* (c) Jitendra Adhikari <jiten.adhikary@gmail.com>
* <https://github.com/adhocore>
*
* Licensed under MIT license.
*/
namespace Ahc\Cron;
/**
* Cron Expression segment checker.
*
* This class checks if a cron segment satisfies given time.
*
* @author Jitendra Adhikari <jiten.adhikary@gmail.com>
*/
class SegmentChecker
{
/** @var ReferenceTime */
protected $reference;
/** @var Validator */
protected $validator;
public function __construct(Validator $validator = null)
{
$this->validator = $validator ?: new Validator;
}
public function setReference(ReferenceTime $reference): self
{
$this->reference = $reference;
return $this;
}
/**
* Checks if a cron segment satisfies given time.
*
* @param string $segment
* @param int $pos
*
* @return bool
*/
public function checkDue(string $segment, int $pos): bool
{
$offsets = \explode(',', \trim($segment));
foreach ($offsets as $offset) {
if ($this->isOffsetDue($offset, $pos)) {
return true;
}
}
return false;
}
/**
* Check if a given offset at a position is due with respect to given time.
*
* @param string $offset
* @param int $pos
*
* @return bool
*/
protected function isOffsetDue(string $offset, int $pos): bool
{
if (\strpos($offset, '/') !== false) {
return $this->validator->inStep($this->reference->get($pos), $offset);
}
if (\strpos($offset, '-') !== false) {
return $this->validator->inRange($this->reference->get($pos), $offset);
}
if (\is_numeric($offset)) {
return $this->reference->isAt($offset, $pos);
}
return $this->checkModifier($offset, $pos);
}
protected function checkModifier(string $offset, int $pos): bool
{
$isModifier = \strpbrk($offset, 'LCW#');
if ($pos === ReferenceTime::MONTHDAY && $isModifier) {
return $this->validator->isValidMonthDay($offset, $this->reference);
}
if ($pos === ReferenceTime::WEEKDAY && $isModifier) {
return $this->validator->isValidWeekDay($offset, $this->reference);
}
throw $this->validator->unexpectedValue($pos, $offset);
// @codeCoverageIgnoreStart
}
// @codeCoverageIgnoreEnd
}