| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Cron; |
| 6 |
|
| 7 |
use DateTimeInterface; |
| 8 |
|
| 9 |
/** |
| 10 |
* Abstract CRON expression field. |
| 11 |
*/ |
| 12 |
abstract class AbstractField implements FieldInterface |
| 13 |
{ |
| 14 |
/** |
| 15 |
* Full range of values that are allowed for this field type. |
| 16 |
* |
| 17 |
* @var array<int, int> |
| 18 |
*/ |
| 19 |
protected $fullRange = []; |
| 20 |
|
| 21 |
/** |
| 22 |
* Literal values we need to convert to integers. |
| 23 |
* |
| 24 |
* @var array<int, string> |
| 25 |
*/ |
| 26 |
protected $literals = []; |
| 27 |
|
| 28 |
/** |
| 29 |
* Start value of the full range. |
| 30 |
* |
| 31 |
* @var int |
| 32 |
*/ |
| 33 |
protected $rangeStart; |
| 34 |
|
| 35 |
/** |
| 36 |
* End value of the full range. |
| 37 |
* |
| 38 |
* @var int |
| 39 |
*/ |
| 40 |
protected $rangeEnd; |
| 41 |
|
| 42 |
/** |
| 43 |
* Constructor |
| 44 |
*/ |
| 45 |
public function __construct() |
| 46 |
{ |
| 47 |
$this->fullRange = range($this->rangeStart, $this->rangeEnd); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Check to see if a field is satisfied by a value. |
| 52 |
* |
| 53 |
* @internal |
| 54 |
* @param int $dateValue Date value to check |
| 55 |
* @param string $value Value to test |
| 56 |
* |
| 57 |
* @return bool |
| 58 |
*/ |
| 59 |
public function isSatisfied(int $dateValue, string $value): bool |
| 60 |
{ |
| 61 |
if ($this->isIncrementsOfRanges($value)) { |
| 62 |
return $this->isInIncrementsOfRanges($dateValue, $value); |
| 63 |
} |
| 64 |
|
| 65 |
if ($this->isRange($value)) { |
| 66 |
return $this->isInRange($dateValue, $value); |
| 67 |
} |
| 68 |
|
| 69 |
return '*' === $value || $dateValue === (int) $value; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Check if a value is a range. |
| 74 |
* |
| 75 |
* @internal |
| 76 |
* @param string $value Value to test |
| 77 |
* |
| 78 |
* @return bool |
| 79 |
*/ |
| 80 |
public function isRange(string $value): bool |
| 81 |
{ |
| 82 |
return false !== strpos($value, '-'); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Check if a value is an increments of ranges. |
| 87 |
* |
| 88 |
* @internal |
| 89 |
* @param string $value Value to test |
| 90 |
* |
| 91 |
* @return bool |
| 92 |
*/ |
| 93 |
public function isIncrementsOfRanges(string $value): bool |
| 94 |
{ |
| 95 |
return false !== strpos($value, '/'); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Test if a value is within a range. |
| 100 |
* |
| 101 |
* @internal |
| 102 |
* @param int $dateValue Set date value |
| 103 |
* @param string $value Value to test |
| 104 |
* |
| 105 |
* @return bool |
| 106 |
*/ |
| 107 |
public function isInRange(int $dateValue, $value): bool |
| 108 |
{ |
| 109 |
$parts = array_map( |
| 110 |
function ($value) { |
| 111 |
$value = trim($value); |
| 112 |
|
| 113 |
return $this->convertLiterals($value); |
| 114 |
}, |
| 115 |
explode('-', $value, 2) |
| 116 |
); |
| 117 |
|
| 118 |
return $dateValue >= $parts[0] && $dateValue <= $parts[1]; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Test if a value is within an increments of ranges (offset[-to]/step size). |
| 123 |
* |
| 124 |
* @internal |
| 125 |
* @param int $dateValue Set date value |
| 126 |
* @param string $value Value to test |
| 127 |
* |
| 128 |
* @return bool |
| 129 |
*/ |
| 130 |
public function isInIncrementsOfRanges(int $dateValue, string $value): bool |
| 131 |
{ |
| 132 |
$chunks = array_map('trim', explode('/', $value, 2)); |
| 133 |
$range = $chunks[0]; |
| 134 |
$step = $chunks[1] ?? 0; |
| 135 |
|
| 136 |
// No step or 0 steps aren't cool |
| 137 |
if (null === $step || '0' === $step || 0 === $step) { |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
// Expand the * to a full range |
| 142 |
if ('*' === $range) { |
| 143 |
$range = $this->rangeStart . '-' . $this->rangeEnd; |
| 144 |
} |
| 145 |
|
| 146 |
// Generate the requested small range |
| 147 |
$rangeChunks = explode('-', $range, 2); |
| 148 |
$rangeStart = (int) $rangeChunks[0]; |
| 149 |
$rangeEnd = $rangeChunks[1] ?? $rangeStart; |
| 150 |
$rangeEnd = (int) $rangeEnd; |
| 151 |
|
| 152 |
if ($rangeStart < $this->rangeStart || $rangeStart > $this->rangeEnd || $rangeStart > $rangeEnd) { |
| 153 |
throw new \OutOfRangeException('Invalid range start requested'); |
| 154 |
} |
| 155 |
|
| 156 |
if ($rangeEnd < $this->rangeStart || $rangeEnd > $this->rangeEnd || $rangeEnd < $rangeStart) { |
| 157 |
throw new \OutOfRangeException('Invalid range end requested'); |
| 158 |
} |
| 159 |
|
| 160 |
// Steps larger than the range need to wrap around and be handled |
| 161 |
// slightly differently than smaller steps |
| 162 |
|
| 163 |
// UPDATE - This is actually false. The C implementation will allow a |
| 164 |
// larger step as valid syntax, it never wraps around. It will stop |
| 165 |
// once it hits the end. Unfortunately this means in future versions |
| 166 |
// we will not wrap around. However, because the logic exists today |
| 167 |
// per the above documentation, fixing the bug from #89 |
| 168 |
if ($step > $this->rangeEnd) { |
| 169 |
$thisRange = [$this->fullRange[(int) $step % \count($this->fullRange)]]; |
| 170 |
} else { |
| 171 |
if ($step > ($rangeEnd - $rangeStart)) { |
| 172 |
$thisRange[$rangeStart] = (int) $rangeStart; |
| 173 |
} else { |
| 174 |
$thisRange = range($rangeStart, $rangeEnd, (int) $step); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return \in_array($dateValue, $thisRange, true); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Returns a range of values for the given cron expression. |
| 183 |
* |
| 184 |
* @param string $expression The expression to evaluate |
| 185 |
* @param int $max Maximum offset for range |
| 186 |
* |
| 187 |
* @return array<int, int> |
| 188 |
*/ |
| 189 |
public function getRangeForExpression(string $expression, int $max): array |
| 190 |
{ |
| 191 |
$values = []; |
| 192 |
$expression = $this->convertLiterals($expression); |
| 193 |
|
| 194 |
if (false !== strpos($expression, ',')) { |
| 195 |
$ranges = explode(',', $expression); |
| 196 |
$values = []; |
| 197 |
foreach ($ranges as $range) { |
| 198 |
$expanded = $this->getRangeForExpression($range, $this->rangeEnd); |
| 199 |
$values = array_merge($values, $expanded); |
| 200 |
} |
| 201 |
|
| 202 |
return $values; |
| 203 |
} |
| 204 |
|
| 205 |
if ($this->isRange($expression) || $this->isIncrementsOfRanges($expression)) { |
| 206 |
if (!$this->isIncrementsOfRanges($expression)) { |
| 207 |
[$offset, $to] = explode('-', $expression); |
| 208 |
$offset = $this->convertLiterals($offset); |
| 209 |
$to = $this->convertLiterals($to); |
| 210 |
$stepSize = 1; |
| 211 |
} else { |
| 212 |
$range = array_map('trim', explode('/', $expression, 2)); |
| 213 |
$stepSize = $range[1] ?? 0; |
| 214 |
$range = $range[0]; |
| 215 |
$range = explode('-', $range, 2); |
| 216 |
$offset = $range[0]; |
| 217 |
$to = $range[1] ?? $max; |
| 218 |
} |
| 219 |
$offset = '*' === $offset ? $this->rangeStart : $offset; |
| 220 |
if ($stepSize >= $this->rangeEnd) { |
| 221 |
$values = [$this->fullRange[(int) $stepSize % \count($this->fullRange)]]; |
| 222 |
} else { |
| 223 |
for ($i = $offset; $i <= $to; $i += $stepSize) { |
| 224 |
$values[] = (int) $i; |
| 225 |
} |
| 226 |
} |
| 227 |
sort($values); |
| 228 |
} else { |
| 229 |
$values = [$expression]; |
| 230 |
} |
| 231 |
|
| 232 |
return $values; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Convert literal. |
| 237 |
* |
| 238 |
* @param string $value |
| 239 |
* |
| 240 |
* @return string |
| 241 |
*/ |
| 242 |
protected function convertLiterals(string $value): string |
| 243 |
{ |
| 244 |
if (\count($this->literals)) { |
| 245 |
$key = array_search(strtoupper($value), $this->literals, true); |
| 246 |
if (false !== $key) { |
| 247 |
return (string) $key; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
return $value; |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Checks to see if a value is valid for the field. |
| 256 |
* |
| 257 |
* @param string $value |
| 258 |
* |
| 259 |
* @return bool |
| 260 |
*/ |
| 261 |
public function validate(string $value): bool |
| 262 |
{ |
| 263 |
$value = $this->convertLiterals($value); |
| 264 |
|
| 265 |
// All fields allow * as a valid value |
| 266 |
if ('*' === $value) { |
| 267 |
return true; |
| 268 |
} |
| 269 |
|
| 270 |
// Validate each chunk of a list individually |
| 271 |
if (false !== strpos($value, ',')) { |
| 272 |
foreach (explode(',', $value) as $listItem) { |
| 273 |
if (!$this->validate($listItem)) { |
| 274 |
return false; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
return true; |
| 279 |
} |
| 280 |
|
| 281 |
if (false !== strpos($value, '/')) { |
| 282 |
[$range, $step] = explode('/', $value); |
| 283 |
|
| 284 |
// Don't allow numeric ranges |
| 285 |
if (is_numeric($range)) { |
| 286 |
return false; |
| 287 |
} |
| 288 |
|
| 289 |
return $this->validate($range) && filter_var($step, FILTER_VALIDATE_INT); |
| 290 |
} |
| 291 |
|
| 292 |
if (false !== strpos($value, '-')) { |
| 293 |
if (substr_count($value, '-') > 1) { |
| 294 |
return false; |
| 295 |
} |
| 296 |
|
| 297 |
$chunks = explode('-', $value); |
| 298 |
$chunks[0] = $this->convertLiterals($chunks[0]); |
| 299 |
$chunks[1] = $this->convertLiterals($chunks[1]); |
| 300 |
|
| 301 |
if ('*' === $chunks[0] || '*' === $chunks[1]) { |
| 302 |
return false; |
| 303 |
} |
| 304 |
|
| 305 |
return $this->validate($chunks[0]) && $this->validate($chunks[1]); |
| 306 |
} |
| 307 |
|
| 308 |
if (!is_numeric($value)) { |
| 309 |
return false; |
| 310 |
} |
| 311 |
|
| 312 |
if (false !== strpos($value, '.')) { |
| 313 |
return false; |
| 314 |
} |
| 315 |
|
| 316 |
// We should have a numeric by now, so coerce this into an integer |
| 317 |
$value = (int) $value; |
| 318 |
|
| 319 |
return \in_array($value, $this->fullRange, true); |
| 320 |
} |
| 321 |
|
| 322 |
protected function timezoneSafeModify(DateTimeInterface $dt, string $modification): DateTimeInterface |
| 323 |
{ |
| 324 |
$timezone = $dt->getTimezone(); |
| 325 |
$dt = $dt->setTimezone(new \DateTimeZone("UTC")); |
| 326 |
$dt = $dt->modify($modification); |
| 327 |
$dt = $dt->setTimezone($timezone); |
| 328 |
return $dt; |
| 329 |
} |
| 330 |
|
| 331 |
protected function setTimeHour(DateTimeInterface $date, bool $invert, int $originalTimestamp): DateTimeInterface |
| 332 |
{ |
| 333 |
$date = $date->setTime((int)$date->format('H'), ($invert ? 59 : 0)); |
| 334 |
|
| 335 |
// setTime caused the offset to change, moving time in the wrong direction |
| 336 |
$actualTimestamp = $date->format('U'); |
| 337 |
if ((! $invert) && ($actualTimestamp <= $originalTimestamp)) { |
| 338 |
$date = $this->timezoneSafeModify($date, "+1 hour"); |
| 339 |
} elseif ($invert && ($actualTimestamp >= $originalTimestamp)) { |
| 340 |
$date = $this->timezoneSafeModify($date, "-1 hour"); |
| 341 |
} |
| 342 |
|
| 343 |
return $date; |
| 344 |
} |
| 345 |
} |