AbstractField.php
1 year ago
CronExpression.php
8 months ago
DayOfMonthField.php
1 year ago
DayOfWeekField.php
1 year ago
FieldFactory.php
1 year ago
FieldFactoryInterface.php
1 year ago
FieldInterface.php
1 year ago
HoursField.php
1 year ago
MinutesField.php
1 year ago
MonthField.php
1 year ago
index.php
1 year ago
AbstractField.php
205 lines
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | namespace Cron; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use DateTimeInterface; |
| 6 | abstract class AbstractField implements FieldInterface |
| 7 | { |
| 8 | protected $fullRange = []; |
| 9 | protected $literals = []; |
| 10 | protected $rangeStart; |
| 11 | protected $rangeEnd; |
| 12 | public function __construct() |
| 13 | { |
| 14 | $this->fullRange = range($this->rangeStart, $this->rangeEnd); |
| 15 | } |
| 16 | public function isSatisfied(int $dateValue, string $value): bool |
| 17 | { |
| 18 | if ($this->isIncrementsOfRanges($value)) { |
| 19 | return $this->isInIncrementsOfRanges($dateValue, $value); |
| 20 | } |
| 21 | if ($this->isRange($value)) { |
| 22 | return $this->isInRange($dateValue, $value); |
| 23 | } |
| 24 | return '*' === $value || $dateValue === (int) $value; |
| 25 | } |
| 26 | public function isRange(string $value): bool |
| 27 | { |
| 28 | return false !== strpos($value, '-'); |
| 29 | } |
| 30 | public function isIncrementsOfRanges(string $value): bool |
| 31 | { |
| 32 | return false !== strpos($value, '/'); |
| 33 | } |
| 34 | public function isInRange(int $dateValue, $value): bool |
| 35 | { |
| 36 | $parts = array_map( |
| 37 | function ($value) { |
| 38 | $value = trim($value); |
| 39 | return $this->convertLiterals($value); |
| 40 | }, |
| 41 | explode('-', $value, 2) |
| 42 | ); |
| 43 | return $dateValue >= $parts[0] && $dateValue <= $parts[1]; |
| 44 | } |
| 45 | public function isInIncrementsOfRanges(int $dateValue, string $value): bool |
| 46 | { |
| 47 | $chunks = array_map('trim', explode('/', $value, 2)); |
| 48 | $range = $chunks[0]; |
| 49 | $step = $chunks[1] ?? 0; |
| 50 | // No step or 0 steps aren't cool |
| 51 | if (null === $step || '0' === $step || 0 === $step) { |
| 52 | return false; |
| 53 | } |
| 54 | // Expand the * to a full range |
| 55 | if ('*' === $range) { |
| 56 | $range = $this->rangeStart . '-' . $this->rangeEnd; |
| 57 | } |
| 58 | // Generate the requested small range |
| 59 | $rangeChunks = explode('-', $range, 2); |
| 60 | $rangeStart = (int) $rangeChunks[0]; |
| 61 | $rangeEnd = $rangeChunks[1] ?? $rangeStart; |
| 62 | $rangeEnd = (int) $rangeEnd; |
| 63 | if ($rangeStart < $this->rangeStart || $rangeStart > $this->rangeEnd || $rangeStart > $rangeEnd) { |
| 64 | throw new \OutOfRangeException('Invalid range start requested'); |
| 65 | } |
| 66 | if ($rangeEnd < $this->rangeStart || $rangeEnd > $this->rangeEnd || $rangeEnd < $rangeStart) { |
| 67 | throw new \OutOfRangeException('Invalid range end requested'); |
| 68 | } |
| 69 | // Steps larger than the range need to wrap around and be handled |
| 70 | // slightly differently than smaller steps |
| 71 | // UPDATE - This is actually false. The C implementation will allow a |
| 72 | // larger step as valid syntax, it never wraps around. It will stop |
| 73 | // once it hits the end. Unfortunately this means in future versions |
| 74 | // we will not wrap around. However, because the logic exists today |
| 75 | // per the above documentation, fixing the bug from #89 |
| 76 | if ($step > $this->rangeEnd) { |
| 77 | $thisRange = [$this->fullRange[$step % \count($this->fullRange)]]; |
| 78 | } else { |
| 79 | if ($step > ($rangeEnd - $rangeStart)) { |
| 80 | $thisRange[$rangeStart] = (int) $rangeStart; |
| 81 | } else { |
| 82 | $thisRange = range($rangeStart, $rangeEnd, (int) $step); |
| 83 | } |
| 84 | } |
| 85 | return \in_array($dateValue, $thisRange, true); |
| 86 | } |
| 87 | public function getRangeForExpression(string $expression, int $max): array |
| 88 | { |
| 89 | $values = []; |
| 90 | $expression = $this->convertLiterals($expression); |
| 91 | if (false !== strpos($expression, ',')) { |
| 92 | $ranges = explode(',', $expression); |
| 93 | $values = []; |
| 94 | foreach ($ranges as $range) { |
| 95 | $expanded = $this->getRangeForExpression($range, $this->rangeEnd); |
| 96 | $values = array_merge($values, $expanded); |
| 97 | } |
| 98 | return $values; |
| 99 | } |
| 100 | if ($this->isRange($expression) || $this->isIncrementsOfRanges($expression)) { |
| 101 | if (!$this->isIncrementsOfRanges($expression)) { |
| 102 | [$offset, $to] = explode('-', $expression); |
| 103 | $offset = $this->convertLiterals($offset); |
| 104 | $to = $this->convertLiterals($to); |
| 105 | $stepSize = 1; |
| 106 | } else { |
| 107 | $range = array_map('trim', explode('/', $expression, 2)); |
| 108 | $stepSize = $range[1] ?? 0; |
| 109 | $range = $range[0]; |
| 110 | $range = explode('-', $range, 2); |
| 111 | $offset = $range[0]; |
| 112 | $to = $range[1] ?? $max; |
| 113 | } |
| 114 | $offset = '*' === $offset ? $this->rangeStart : $offset; |
| 115 | if ($stepSize >= $this->rangeEnd) { |
| 116 | $values = [$this->fullRange[$stepSize % \count($this->fullRange)]]; |
| 117 | } else { |
| 118 | for ($i = $offset; $i <= $to; $i += $stepSize) { |
| 119 | $values[] = (int) $i; |
| 120 | } |
| 121 | } |
| 122 | sort($values); |
| 123 | } else { |
| 124 | $values = [$expression]; |
| 125 | } |
| 126 | return $values; |
| 127 | } |
| 128 | protected function convertLiterals(string $value): string |
| 129 | { |
| 130 | if (\count($this->literals)) { |
| 131 | $key = array_search(strtoupper($value), $this->literals, true); |
| 132 | if (false !== $key) { |
| 133 | return (string) $key; |
| 134 | } |
| 135 | } |
| 136 | return $value; |
| 137 | } |
| 138 | public function validate(string $value): bool |
| 139 | { |
| 140 | $value = $this->convertLiterals($value); |
| 141 | // All fields allow * as a valid value |
| 142 | if ('*' === $value) { |
| 143 | return true; |
| 144 | } |
| 145 | // Validate each chunk of a list individually |
| 146 | if (false !== strpos($value, ',')) { |
| 147 | foreach (explode(',', $value) as $listItem) { |
| 148 | if (!$this->validate($listItem)) { |
| 149 | return false; |
| 150 | } |
| 151 | } |
| 152 | return true; |
| 153 | } |
| 154 | if (false !== strpos($value, '/')) { |
| 155 | [$range, $step] = explode('/', $value); |
| 156 | // Don't allow numeric ranges |
| 157 | if (is_numeric($range)) { |
| 158 | return false; |
| 159 | } |
| 160 | return $this->validate($range) && filter_var($step, FILTER_VALIDATE_INT); |
| 161 | } |
| 162 | if (false !== strpos($value, '-')) { |
| 163 | if (substr_count($value, '-') > 1) { |
| 164 | return false; |
| 165 | } |
| 166 | $chunks = explode('-', $value); |
| 167 | $chunks[0] = $this->convertLiterals($chunks[0]); |
| 168 | $chunks[1] = $this->convertLiterals($chunks[1]); |
| 169 | if ('*' === $chunks[0] || '*' === $chunks[1]) { |
| 170 | return false; |
| 171 | } |
| 172 | return $this->validate($chunks[0]) && $this->validate($chunks[1]); |
| 173 | } |
| 174 | if (!is_numeric($value)) { |
| 175 | return false; |
| 176 | } |
| 177 | if (false !== strpos($value, '.')) { |
| 178 | return false; |
| 179 | } |
| 180 | // We should have a numeric by now, so coerce this into an integer |
| 181 | $value = (int) $value; |
| 182 | return \in_array($value, $this->fullRange, true); |
| 183 | } |
| 184 | protected function timezoneSafeModify(DateTimeInterface $dt, string $modification): DateTimeInterface |
| 185 | { |
| 186 | $timezone = $dt->getTimezone(); |
| 187 | $dt = $dt->setTimezone(new \DateTimeZone("UTC")); |
| 188 | $dt = $dt->modify($modification); |
| 189 | $dt = $dt->setTimezone($timezone); |
| 190 | return $dt; |
| 191 | } |
| 192 | protected function setTimeHour(DateTimeInterface $date, bool $invert, int $originalTimestamp): DateTimeInterface |
| 193 | { |
| 194 | $date = $date->setTime((int)$date->format('H'), ($invert ? 59 : 0)); |
| 195 | // setTime caused the offset to change, moving time in the wrong direction |
| 196 | $actualTimestamp = $date->format('U'); |
| 197 | if ((! $invert) && ($actualTimestamp <= $originalTimestamp)) { |
| 198 | $date = $this->timezoneSafeModify($date, "+1 hour"); |
| 199 | } elseif ($invert && ($actualTimestamp >= $originalTimestamp)) { |
| 200 | $date = $this->timezoneSafeModify($date, "-1 hour"); |
| 201 | } |
| 202 | return $date; |
| 203 | } |
| 204 | } |
| 205 |