| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace Cron;
|
|
|
6 |
|
|
|
7 |
use DateTimeInterface;
|
|
|
8 |
|
|
|
9 |
/**
|
|
|
10 |
* Minutes field. Allows: * , / -.
|
|
|
11 |
*/
|
|
|
12 |
class MinutesField extends AbstractField
|
|
|
13 |
{
|
|
|
14 |
/**
|
|
|
15 |
* {@inheritdoc}
|
|
|
16 |
*/
|
|
|
17 |
protected $rangeStart = 0;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* {@inheritdoc}
|
|
|
21 |
*/
|
|
|
22 |
protected $rangeEnd = 59;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* {@inheritdoc}
|
|
|
26 |
*/
|
|
|
27 |
public function isSatisfiedBy(DateTimeInterface $date, $value, bool $invert):bool
|
|
|
28 |
{
|
|
|
29 |
if ($value === '?') {
|
|
|
30 |
return true;
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
return $this->isSatisfied((int)$date->format('i'), $value);
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* {@inheritdoc}
|
|
|
38 |
* {@inheritDoc}
|
|
|
39 |
*
|
|
|
40 |
* @param string|null $parts
|
|
|
41 |
*/
|
|
|
42 |
public function increment(DateTimeInterface &$date, $invert = false, $parts = null): FieldInterface
|
|
|
43 |
{
|
|
|
44 |
if (is_null($parts)) {
|
|
|
45 |
$date = $this->timezoneSafeModify($date, ($invert ? "-" : "+") ."1 minute");
|
|
|
46 |
return $this;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
$current_minute = (int) $date->format('i');
|
|
|
50 |
|
|
|
51 |
$parts = false !== strpos($parts, ',') ? explode(',', $parts) : [$parts];
|
|
|
52 |
$minutes = [];
|
|
|
53 |
foreach ($parts as $part) {
|
|
|
54 |
$minutes = array_merge($minutes, $this->getRangeForExpression($part, 59));
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
$position = $invert ? \count($minutes) - 1 : 0;
|
|
|
58 |
if (\count($minutes) > 1) {
|
|
|
59 |
for ($i = 0; $i < \count($minutes) - 1; ++$i) {
|
|
|
60 |
if ((!$invert && $current_minute >= $minutes[$i] && $current_minute < $minutes[$i + 1]) ||
|
|
|
61 |
($invert && $current_minute > $minutes[$i] && $current_minute <= $minutes[$i + 1])) {
|
|
|
62 |
$position = $invert ? $i : $i + 1;
|
|
|
63 |
|
|
|
64 |
break;
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
$target = (int) $minutes[$position];
|
|
|
70 |
$originalMinute = (int) $date->format("i");
|
|
|
71 |
|
|
|
72 |
if (! $invert) {
|
|
|
73 |
if ($originalMinute >= $target) {
|
|
|
74 |
$distance = 60 - $originalMinute;
|
|
|
75 |
$date = $this->timezoneSafeModify($date, "+{$distance} minutes");
|
|
|
76 |
|
|
|
77 |
$originalMinute = (int) $date->format("i");
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$distance = $target - $originalMinute;
|
|
|
81 |
$date = $this->timezoneSafeModify($date, "+{$distance} minutes");
|
|
|
82 |
} else {
|
|
|
83 |
if ($originalMinute <= $target) {
|
|
|
84 |
$distance = ($originalMinute + 1);
|
|
|
85 |
$date = $this->timezoneSafeModify($date, "-{$distance} minutes");
|
|
|
86 |
|
|
|
87 |
$originalMinute = (int) $date->format("i");
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
$distance = $originalMinute - $target;
|
|
|
91 |
$date = $this->timezoneSafeModify($date, "-{$distance} minutes");
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
return $this;
|
|
|
95 |
}
|
|
|
96 |
}
|