Skip to content

Commit 4a18823

Browse files
committed
refactor: move around
1 parent 0e3d455 commit 4a18823

File tree

1 file changed

+1
-164
lines changed

1 file changed

+1
-164
lines changed

src/Expression.php

Lines changed: 1 addition & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static function isDue($expr, $time = null)
6565
continue;
6666
}
6767

68-
if (!static::isSegmentDue($segment, $pos, $time)) {
68+
if (!SegmentChecker::isDue($segment, $pos, $time)) {
6969
return false;
7070
}
7171
}
@@ -116,169 +116,6 @@ protected static function normalizeTime($time)
116116
return $time;
117117
}
118118

119-
protected static function isSegmentDue($segment, $pos, $time)
120-
{
121-
$isDue = true;
122-
$offsets = explode(',', trim($segment));
123-
124-
foreach ($offsets as $offset) {
125-
if (null === $isDue = static::isOffsetDue($offset, $pos, $time)) {
126-
throw new \UnexpectedValueException(
127-
sprintf('Invalid offset value %s for segment #%d', $offset, $pos)
128-
);
129-
}
130-
131-
if ($isDue) {
132-
return true;
133-
}
134-
}
135-
136-
return false;
137-
}
138-
139-
/**
140-
* Check if a given offset at a position is due with respect to given time.
141-
*
142-
* @param string $offset
143-
* @param int $pos
144-
* @param array $time
145-
*
146-
* @return bool|null
147-
*/
148-
protected static function isOffsetDue($offset, $pos, $time)
149-
{
150-
if (strpos($offset, '/') !== false) {
151-
return static::inStep($time[$pos], $offset);
152-
}
153-
154-
if (strpos($offset, '-') !== false) {
155-
return static::inRange($time[$pos], $offset);
156-
}
157-
158-
if (is_numeric($offset)) {
159-
return $time[$pos] == $offset;
160-
}
161-
162-
$isModifier = strpbrk($offset, 'LCW#');
163-
164-
if ($pos === 2 && $isModifier) {
165-
return static::checkMonthDay($offset, $time);
166-
}
167-
168-
if ($pos === 4 && $isModifier) {
169-
return static::checkWeekDay($offset, $time);
170-
}
171-
}
172-
173-
protected static function inRange($value, $offset)
174-
{
175-
$parts = explode('-', $offset);
176-
177-
return $parts[0] <= $value && $value <= $parts[1];
178-
}
179-
180-
protected static function inStep($value, $offset)
181-
{
182-
if (strpos($offset, '*/') !== false || strpos($offset, '0/') !== false) {
183-
$parts = explode('/', $offset, 2);
184-
185-
return $value % $parts[1] === 0;
186-
}
187-
188-
$parts = explode('/', $offset, 2);
189-
$subparts = explode('-', $parts[0], 2) + [1 => $value];
190-
191-
return ($subparts[0] <= $value && $value <= $subparts[1] && $parts[1])
192-
? in_array($value, range($subparts[0], $subparts[1], $parts[1]))
193-
: false;
194-
}
195-
196-
/**
197-
* Check if modifiers [L C W #] are satisfied.
198-
*
199-
* @internal
200-
*
201-
* @param string $value
202-
* @param int $time
203-
*
204-
* @return bool|null
205-
*/
206-
protected static function checkMonthDay($value, $time)
207-
{
208-
$month = $time[8] < 10 ? '0' . $time[8] : $time[8];
209-
210-
if ($value == 'L') {
211-
return $time[2] == $time[6];
212-
}
213-
214-
if ($pos = strpos($value, 'W')) {
215-
$value = substr($value, 0, $pos);
216-
217-
return static::isClosestWeekDay($value, $month, $time);
218-
}
219-
}
220-
221-
protected static function isClosestWeekDay($value, $month, $time)
222-
{
223-
foreach ([0, -1, 1, -2, 2] as $i) {
224-
$incr = $value + $i;
225-
if ($incr > 0 && $incr <= $time[6]) {
226-
if ($incr < 10) {
227-
$incr = '0' . $incr;
228-
}
229-
230-
$parts = explode(' ', date('N m j', strtotime("$time[5]-$month-$incr")));
231-
if ($parts[0] < 6 && $parts[1] == $month) {
232-
return $time[2] == $parts[2];
233-
}
234-
}
235-
}
236-
}
237-
238-
/**
239-
* Check if modifiers [L C W #] are satisfied.
240-
*
241-
* @internal
242-
*
243-
* @param string $value
244-
* @param int $time
245-
*
246-
* @return bool|null
247-
*/
248-
protected static function checkWeekDay($value, $time)
249-
{
250-
$month = $time[8] < 10 ? '0' . $time[8] : $time[8];
251-
252-
if (strpos($value, 'L')) {
253-
return static::lasWeekDay($value, $month, $time);
254-
}
255-
256-
if (strpos($value, '#')) {
257-
$value = explode('#', str_replace('0#', '7#', $value));
258-
259-
if ($value[0] < 0 || $value[0] > 7 || $value[1] < 1 || $value[1] > 5 || $time[9] != $value[0]) {
260-
return false;
261-
}
262-
263-
return intval($time[7] / 7) == $value[1] - 1;
264-
}
265-
}
266-
267-
protected static function lasWeekDay($value, $month, $time)
268-
{
269-
$value = explode('L', str_replace('7L', '0L', $value));
270-
$decr = $time[6];
271-
272-
for ($i = 0; $i < 7; $i++) {
273-
$decr -= $i;
274-
if (date('w', strtotime("$time[5]-$month-$decr")) == $value[0]) {
275-
return $time[2] == $decr;
276-
}
277-
}
278-
279-
return false;
280-
}
281-
282119
/**
283120
* Instance call.
284121
*

0 commit comments

Comments
 (0)