Files
apimacro/vendor/dragonmantank/cron-expression/src/Cron/AbstractField.php

347 lines
9.7 KiB
PHP
Raw Normal View History

2024-05-07 12:17:25 +02:00
<?php
2024-08-13 13:44:16 +00:00
declare(strict_types=1);
2024-05-07 12:17:25 +02:00
namespace Cron;
2024-08-13 13:44:16 +00:00
use DateTimeInterface;
2024-05-07 12:17:25 +02:00
/**
2024-08-13 13:44:16 +00:00
* Abstract CRON expression field.
2024-05-07 12:17:25 +02:00
*/
abstract class AbstractField implements FieldInterface
{
/**
2024-08-13 13:44:16 +00:00
* Full range of values that are allowed for this field type.
*
2024-05-07 12:17:25 +02:00
* @var array
*/
protected $fullRange = [];
/**
2024-08-13 13:44:16 +00:00
* Literal values we need to convert to integers.
*
2024-05-07 12:17:25 +02:00
* @var array
*/
protected $literals = [];
/**
2024-08-13 13:44:16 +00:00
* Start value of the full range.
*
* @var int
2024-05-07 12:17:25 +02:00
*/
protected $rangeStart;
/**
2024-08-13 13:44:16 +00:00
* End value of the full range.
*
* @var int
2024-05-07 12:17:25 +02:00
*/
protected $rangeEnd;
/**
* Constructor
*/
public function __construct()
{
$this->fullRange = range($this->rangeStart, $this->rangeEnd);
}
/**
2024-08-13 13:44:16 +00:00
* Check to see if a field is satisfied by a value.
2024-05-07 12:17:25 +02:00
*
2024-08-13 13:44:16 +00:00
* @internal
* @param int $dateValue Date value to check
* @param string $value Value to test
2024-05-07 12:17:25 +02:00
*
* @return bool
*/
2024-08-13 13:44:16 +00:00
public function isSatisfied(int $dateValue, string $value): bool
2024-05-07 12:17:25 +02:00
{
if ($this->isIncrementsOfRanges($value)) {
return $this->isInIncrementsOfRanges($dateValue, $value);
2024-08-13 13:44:16 +00:00
}
if ($this->isRange($value)) {
2024-05-07 12:17:25 +02:00
return $this->isInRange($dateValue, $value);
}
2024-08-13 13:44:16 +00:00
return '*' === $value || $dateValue === (int) $value;
2024-05-07 12:17:25 +02:00
}
/**
2024-08-13 13:44:16 +00:00
* Check if a value is a range.
2024-05-07 12:17:25 +02:00
*
2024-08-13 13:44:16 +00:00
* @internal
2024-05-07 12:17:25 +02:00
* @param string $value Value to test
*
* @return bool
*/
2024-08-13 13:44:16 +00:00
public function isRange(string $value): bool
2024-05-07 12:17:25 +02:00
{
2024-08-13 13:44:16 +00:00
return false !== strpos($value, '-');
2024-05-07 12:17:25 +02:00
}
/**
2024-08-13 13:44:16 +00:00
* Check if a value is an increments of ranges.
2024-05-07 12:17:25 +02:00
*
2024-08-13 13:44:16 +00:00
* @internal
2024-05-07 12:17:25 +02:00
* @param string $value Value to test
*
* @return bool
*/
2024-08-13 13:44:16 +00:00
public function isIncrementsOfRanges(string $value): bool
2024-05-07 12:17:25 +02:00
{
2024-08-13 13:44:16 +00:00
return false !== strpos($value, '/');
2024-05-07 12:17:25 +02:00
}
/**
2024-08-13 13:44:16 +00:00
* Test if a value is within a range.
2024-05-07 12:17:25 +02:00
*
2024-08-13 13:44:16 +00:00
* @internal
* @param int $dateValue Set date value
* @param string $value Value to test
2024-05-07 12:17:25 +02:00
*
* @return bool
*/
2024-08-13 13:44:16 +00:00
public function isInRange(int $dateValue, $value): bool
2024-05-07 12:17:25 +02:00
{
2024-08-13 13:44:16 +00:00
$parts = array_map(
function ($value) {
2024-05-07 12:17:25 +02:00
$value = trim($value);
2024-08-13 13:44:16 +00:00
return $this->convertLiterals($value);
2024-05-07 12:17:25 +02:00
},
explode('-', $value, 2)
);
return $dateValue >= $parts[0] && $dateValue <= $parts[1];
}
/**
2024-08-13 13:44:16 +00:00
* Test if a value is within an increments of ranges (offset[-to]/step size).
2024-05-07 12:17:25 +02:00
*
2024-08-13 13:44:16 +00:00
* @internal
* @param int $dateValue Set date value
* @param string $value Value to test
2024-05-07 12:17:25 +02:00
*
* @return bool
*/
2024-08-13 13:44:16 +00:00
public function isInIncrementsOfRanges(int $dateValue, string $value): bool
2024-05-07 12:17:25 +02:00
{
$chunks = array_map('trim', explode('/', $value, 2));
$range = $chunks[0];
2024-08-13 13:44:16 +00:00
$step = $chunks[1] ?? 0;
2024-05-07 12:17:25 +02:00
// No step or 0 steps aren't cool
2024-08-13 13:44:16 +00:00
/** @phpstan-ignore-next-line */
if (null === $step || '0' === $step || 0 === $step) {
2024-05-07 12:17:25 +02:00
return false;
}
// Expand the * to a full range
2024-08-13 13:44:16 +00:00
if ('*' === $range) {
2024-05-07 12:17:25 +02:00
$range = $this->rangeStart . '-' . $this->rangeEnd;
}
// Generate the requested small range
$rangeChunks = explode('-', $range, 2);
2024-08-13 13:44:16 +00:00
$rangeStart = (int) $rangeChunks[0];
$rangeEnd = $rangeChunks[1] ?? $rangeStart;
$rangeEnd = (int) $rangeEnd;
2024-05-07 12:17:25 +02:00
if ($rangeStart < $this->rangeStart || $rangeStart > $this->rangeEnd || $rangeStart > $rangeEnd) {
throw new \OutOfRangeException('Invalid range start requested');
}
if ($rangeEnd < $this->rangeStart || $rangeEnd > $this->rangeEnd || $rangeEnd < $rangeStart) {
throw new \OutOfRangeException('Invalid range end requested');
}
2024-08-13 13:44:16 +00:00
// Steps larger than the range need to wrap around and be handled
// slightly differently than smaller steps
// UPDATE - This is actually false. The C implementation will allow a
// larger step as valid syntax, it never wraps around. It will stop
// once it hits the end. Unfortunately this means in future versions
// we will not wrap around. However, because the logic exists today
// per the above documentation, fixing the bug from #89
if ($step > $this->rangeEnd) {
$thisRange = [$this->fullRange[$step % \count($this->fullRange)]];
2024-05-07 12:17:25 +02:00
} else {
2024-08-13 13:44:16 +00:00
if ($step > ($rangeEnd - $rangeStart)) {
$thisRange[$rangeStart] = (int) $rangeStart;
} else {
$thisRange = range($rangeStart, $rangeEnd, (int) $step);
}
2024-05-07 12:17:25 +02:00
}
2024-08-13 13:44:16 +00:00
return \in_array($dateValue, $thisRange, true);
2024-05-07 12:17:25 +02:00
}
/**
2024-08-13 13:44:16 +00:00
* Returns a range of values for the given cron expression.
2024-05-07 12:17:25 +02:00
*
* @param string $expression The expression to evaluate
2024-08-13 13:44:16 +00:00
* @param int $max Maximum offset for range
2024-05-07 12:17:25 +02:00
*
* @return array
*/
2024-08-13 13:44:16 +00:00
public function getRangeForExpression(string $expression, int $max): array
2024-05-07 12:17:25 +02:00
{
2024-08-13 13:44:16 +00:00
$values = [];
2024-05-07 12:17:25 +02:00
$expression = $this->convertLiterals($expression);
2024-08-13 13:44:16 +00:00
if (false !== strpos($expression, ',')) {
2024-05-07 12:17:25 +02:00
$ranges = explode(',', $expression);
$values = [];
foreach ($ranges as $range) {
$expanded = $this->getRangeForExpression($range, $this->rangeEnd);
$values = array_merge($values, $expanded);
}
2024-08-13 13:44:16 +00:00
2024-05-07 12:17:25 +02:00
return $values;
}
if ($this->isRange($expression) || $this->isIncrementsOfRanges($expression)) {
if (!$this->isIncrementsOfRanges($expression)) {
2024-08-13 13:44:16 +00:00
[$offset, $to] = explode('-', $expression);
2024-05-07 12:17:25 +02:00
$offset = $this->convertLiterals($offset);
$to = $this->convertLiterals($to);
$stepSize = 1;
2024-08-13 13:44:16 +00:00
} else {
2024-05-07 12:17:25 +02:00
$range = array_map('trim', explode('/', $expression, 2));
2024-08-13 13:44:16 +00:00
$stepSize = $range[1] ?? 0;
2024-05-07 12:17:25 +02:00
$range = $range[0];
$range = explode('-', $range, 2);
$offset = $range[0];
2024-08-13 13:44:16 +00:00
$to = $range[1] ?? $max;
2024-05-07 12:17:25 +02:00
}
2024-08-13 13:44:16 +00:00
$offset = '*' === $offset ? $this->rangeStart : $offset;
2024-05-07 12:17:25 +02:00
if ($stepSize >= $this->rangeEnd) {
2024-08-13 13:44:16 +00:00
$values = [$this->fullRange[$stepSize % \count($this->fullRange)]];
2024-05-07 12:17:25 +02:00
} else {
for ($i = $offset; $i <= $to; $i += $stepSize) {
2024-08-13 13:44:16 +00:00
$values[] = (int) $i;
2024-05-07 12:17:25 +02:00
}
}
sort($values);
2024-08-13 13:44:16 +00:00
} else {
$values = [$expression];
2024-05-07 12:17:25 +02:00
}
return $values;
}
/**
2024-08-13 13:44:16 +00:00
* Convert literal.
2024-05-07 12:17:25 +02:00
*
* @param string $value
2024-08-13 13:44:16 +00:00
*
2024-05-07 12:17:25 +02:00
* @return string
*/
2024-08-13 13:44:16 +00:00
protected function convertLiterals(string $value): string
2024-05-07 12:17:25 +02:00
{
2024-08-13 13:44:16 +00:00
if (\count($this->literals)) {
$key = array_search(strtoupper($value), $this->literals, true);
if (false !== $key) {
2024-05-07 12:17:25 +02:00
return (string) $key;
}
}
return $value;
}
/**
2024-08-13 13:44:16 +00:00
* Checks to see if a value is valid for the field.
2024-05-07 12:17:25 +02:00
*
* @param string $value
2024-08-13 13:44:16 +00:00
*
2024-05-07 12:17:25 +02:00
* @return bool
*/
2024-08-13 13:44:16 +00:00
public function validate(string $value): bool
2024-05-07 12:17:25 +02:00
{
$value = $this->convertLiterals($value);
// All fields allow * as a valid value
if ('*' === $value) {
return true;
}
// Validate each chunk of a list individually
2024-08-13 13:44:16 +00:00
if (false !== strpos($value, ',')) {
2024-05-07 12:17:25 +02:00
foreach (explode(',', $value) as $listItem) {
if (!$this->validate($listItem)) {
return false;
}
}
2024-08-13 13:44:16 +00:00
2024-05-07 12:17:25 +02:00
return true;
}
2024-08-13 13:44:16 +00:00
if (false !== strpos($value, '/')) {
[$range, $step] = explode('/', $value);
// Don't allow numeric ranges
if (is_numeric($range)) {
return false;
}
return $this->validate($range) && filter_var($step, FILTER_VALIDATE_INT);
}
if (false !== strpos($value, '-')) {
2024-05-07 12:17:25 +02:00
if (substr_count($value, '-') > 1) {
return false;
}
$chunks = explode('-', $value);
$chunks[0] = $this->convertLiterals($chunks[0]);
$chunks[1] = $this->convertLiterals($chunks[1]);
2024-08-13 13:44:16 +00:00
if ('*' === $chunks[0] || '*' === $chunks[1]) {
2024-05-07 12:17:25 +02:00
return false;
}
return $this->validate($chunks[0]) && $this->validate($chunks[1]);
}
if (!is_numeric($value)) {
return false;
}
2024-08-13 13:44:16 +00:00
if (false !== strpos($value, '.')) {
2024-05-07 12:17:25 +02:00
return false;
}
// We should have a numeric by now, so coerce this into an integer
$value = (int) $value;
2024-08-13 13:44:16 +00:00
return \in_array($value, $this->fullRange, true);
}
protected function timezoneSafeModify(DateTimeInterface $dt, string $modification): DateTimeInterface
{
$timezone = $dt->getTimezone();
$dt = $dt->setTimezone(new \DateTimeZone("UTC"));
$dt = $dt->modify($modification);
$dt = $dt->setTimezone($timezone);
return $dt;
}
protected function setTimeHour(DateTimeInterface $date, bool $invert, int $originalTimestamp): DateTimeInterface
{
$date = $date->setTime((int)$date->format('H'), ($invert ? 59 : 0));
// setTime caused the offset to change, moving time in the wrong direction
$actualTimestamp = $date->format('U');
if ((! $invert) && ($actualTimestamp <= $originalTimestamp)) {
$date = $this->timezoneSafeModify($date, "+1 hour");
} elseif ($invert && ($actualTimestamp >= $originalTimestamp)) {
$date = $this->timezoneSafeModify($date, "-1 hour");
}
return $date;
2024-05-07 12:17:25 +02:00
}
}