-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathValidator.php
More file actions
209 lines (175 loc) · 5.25 KB
/
Validator.php
File metadata and controls
209 lines (175 loc) · 5.25 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?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;
use UnexpectedValueException;
/**
* Cron segment validator.
*
* This class checks if a cron segment is valid.
*
* @author Jitendra Adhikari <jiten.adhikary@gmail.com>
*/
class Validator
{
/**
* Check if the value is in range of given offset.
*
* @param int $value
* @param string $offset
*
* @return bool
*/
public function inRange(int $value, string $offset): bool
{
$parts = \explode('-', $offset);
return $parts[0] <= $value && $value <= $parts[1];
}
/**
* Check if the value is in step of given offset.
*
* @param int $value
* @param string $offset
*
* @return bool
*/
public function inStep(int $value, string $offset): bool
{
$parts = \explode('/', $offset, 2);
if (empty($parts[1])) {
return false;
}
if (\strpos($offset, '*/') === 0 || \strpos($offset, '0/') === 0) {
return $value % $parts[1] === 0;
}
$subparts = \explode('-', $parts[0], 2) + [1 => $value];
return $this->inStepRange((int) $value, (int) $subparts[0], (int) $subparts[1], (int) $parts[1]);
}
/**
* Check if the value falls between start and end when advanved by step.
*
* @param int $value
* @param int $start
* @param int $end
* @param int $step
*
* @return bool
*/
public function inStepRange(int $value, int $start, int $end, int $step): bool
{
if (($start + $step) > $end) {
return false;
}
if ($start <= $value && $value <= $end) {
return \in_array($value, \range($start, $end, $step));
}
return false;
}
/**
* Check if month modifiers [L C W #] are satisfied.
*
* @internal
*
* @param string $value
* @param ReferenceTime $reference
*
* @return bool
*/
public function isValidMonthDay(string $value, ReferenceTime $reference): bool
{
if ($value == 'L') {
return $reference->monthDay() == $reference->numDays();
}
if ($pos = \strpos($value, 'W')) {
$value = \substr($value, 0, $pos);
$month = $this->zeroPad($reference->month());
return $this->isClosestWeekDay((int) $value, $month, $reference);
}
throw $this->unexpectedValue(2, $value);
// @codeCoverageIgnoreStart
}
// @codeCoverageIgnoreEnd
protected function isClosestWeekDay(int $value, string $month, ReferenceTime $reference): bool
{
foreach ([0, -1, 1, -2, 2] as $i) {
$incr = $value + $i;
if ($incr < 1 || $incr > $reference->numDays()) {
continue;
}
$incr = $this->zeroPad($incr);
$parts = \explode(' ', \date('N m j', \strtotime("{$reference->year()}-$month-$incr")));
if ($parts[0] < 6 && $parts[1] == $month) {
return $reference->monthDay() == $parts[2];
}
}
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
}
/**
* Check if week modifiers [L C W #] are satisfied.
*
* @internal
*
* @param string $value
* @param ReferenceTime $reference
*
* @return bool
*/
public function isValidWeekDay(string $value, ReferenceTime $reference): bool
{
$month = $this->zeroPad($reference->month());
if (\strpos($value, 'L')) {
return $this->isLastWeekDay($value, $month, $reference);
}
if (!\strpos($value, '#')) {
throw $this->unexpectedValue(4, $value);
}
list($day, $nth) = \explode('#', \str_replace('7#', '0#', $value));
if (!$this->isNthWeekDay((int) $day, (int) $nth) || $reference->weekDay() != $day) {
return false;
}
return \intval($reference->day() / 7) == $nth - 1;
}
/**
* Throws UnexpectedValueException.
*
* @param int $pos
* @param string $value
*
* @return \UnexpectedValueException
*/
public function unexpectedValue(int $pos, string $value): UnexpectedValueException
{
return new UnexpectedValueException(
\sprintf('Invalid offset value at segment #%d: %s', $pos, $value)
);
}
protected function isLastWeekDay(string $value, string $month, ReferenceTime $reference): bool
{
$decr = $reference->numDays();
$value = \explode('L', \str_replace('7L', '0L', $value));
for ($i = 0; $i < 7; $i++) {
$decr -= $i;
if (\date('w', \strtotime("{$reference->year()}-$month-$decr")) == $value[0]) {
return $reference->monthDay() == $decr;
}
}
return false;
}
protected function isNthWeekDay(int $day, int $nth): bool
{
return !($day < 0 || $day > 7 || $nth < 1 || $nth > 5);
}
protected function zeroPad($value): string
{
return \str_pad((string) $value, 2, '0', \STR_PAD_LEFT);
}
}