| 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 |
sort($parts); |
| 53 |
$minutes = []; |
| 54 |
foreach ($parts as $part) { |
| 55 |
$minutes = array_merge($minutes, $this->getRangeForExpression($part, 59)); |
| 56 |
} |
| 57 |
|
| 58 |
$position = $invert ? \count($minutes) - 1 : 0; |
| 59 |
if (\count($minutes) > 1) { |
| 60 |
for ($i = 0; $i < \count($minutes) - 1; ++$i) { |
| 61 |
if ((!$invert && $current_minute >= $minutes[$i] && $current_minute < $minutes[$i + 1]) || |
| 62 |
($invert && $current_minute > $minutes[$i] && $current_minute <= $minutes[$i + 1])) { |
| 63 |
$position = $invert ? $i : $i + 1; |
| 64 |
|
| 65 |
break; |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
$target = (int) $minutes[$position]; |
| 71 |
$originalMinute = (int) $date->format("i"); |
| 72 |
|
| 73 |
if (! $invert) { |
| 74 |
if ($originalMinute >= $target) { |
| 75 |
$distance = 60 - $originalMinute; |
| 76 |
$date = $this->timezoneSafeModify($date, "+{$distance} minutes"); |
| 77 |
|
| 78 |
$originalMinute = (int) $date->format("i"); |
| 79 |
} |
| 80 |
|
| 81 |
$distance = $target - $originalMinute; |
| 82 |
$date = $this->timezoneSafeModify($date, "+{$distance} minutes"); |
| 83 |
} else { |
| 84 |
if ($originalMinute <= $target) { |
| 85 |
$distance = ($originalMinute + 1); |
| 86 |
$date = $this->timezoneSafeModify($date, "-{$distance} minutes"); |
| 87 |
|
| 88 |
$originalMinute = (int) $date->format("i"); |
| 89 |
} |
| 90 |
|
| 91 |
$distance = $originalMinute - $target; |
| 92 |
$date = $this->timezoneSafeModify($date, "-{$distance} minutes"); |
| 93 |
} |
| 94 |
|
| 95 |
return $this; |
| 96 |
} |
| 97 |
} |