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
DayOfMonthField.php
95 lines
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | namespace Cron; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use DateTime; |
| 6 | use DateTimeInterface; |
| 7 | class DayOfMonthField extends AbstractField |
| 8 | { |
| 9 | protected $rangeStart = 1; |
| 10 | protected $rangeEnd = 31; |
| 11 | private static function getNearestWeekday(int $currentYear, int $currentMonth, int $targetDay): ?DateTime |
| 12 | { |
| 13 | $tday = str_pad((string) $targetDay, 2, '0', STR_PAD_LEFT); |
| 14 | $target = DateTime::createFromFormat('Y-m-d', "{$currentYear}-{$currentMonth}-{$tday}"); |
| 15 | if ($target === false) { |
| 16 | return null; |
| 17 | } |
| 18 | $currentWeekday = (int) $target->format('N'); |
| 19 | if ($currentWeekday < 6) { |
| 20 | return $target; |
| 21 | } |
| 22 | $lastDayOfMonth = $target->format('t'); |
| 23 | foreach ([-1, 1, -2, 2] as $i) { |
| 24 | $adjusted = $targetDay + $i; |
| 25 | if ($adjusted > 0 && $adjusted <= $lastDayOfMonth) { |
| 26 | $target->setDate($currentYear, $currentMonth, $adjusted); |
| 27 | if ((int) $target->format('N') < 6 && (int) $target->format('m') === $currentMonth) { |
| 28 | return $target; |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | return null; |
| 33 | } |
| 34 | public function isSatisfiedBy(DateTimeInterface $date, $value, bool $invert): bool |
| 35 | { |
| 36 | // ? states that the field value is to be skipped |
| 37 | if ('?' === $value) { |
| 38 | return true; |
| 39 | } |
| 40 | $fieldValue = $date->format('d'); |
| 41 | // Check to see if this is the last day of the month |
| 42 | if ('L' === $value) { |
| 43 | return $fieldValue === $date->format('t'); |
| 44 | } |
| 45 | // Check to see if this is the nearest weekday to a particular value |
| 46 | if ($wPosition = strpos($value, 'W')) { |
| 47 | // Parse the target day |
| 48 | $targetDay = (int) substr($value, 0, $wPosition); |
| 49 | // Find out if the current day is the nearest day of the week |
| 50 | $nearest = self::getNearestWeekday( |
| 51 | (int) $date->format('Y'), |
| 52 | (int) $date->format('m'), |
| 53 | $targetDay |
| 54 | ); |
| 55 | if ($nearest) { |
| 56 | return $date->format('j') === $nearest->format('j'); |
| 57 | } |
| 58 | throw new \RuntimeException('Unable to return nearest weekday'); |
| 59 | } |
| 60 | return $this->isSatisfied((int) $date->format('d'), $value); |
| 61 | } |
| 62 | public function increment(DateTimeInterface &$date, $invert = false, $parts = null): FieldInterface |
| 63 | { |
| 64 | if (! $invert) { |
| 65 | $date = $date->add(new \DateInterval('P1D')); |
| 66 | $date = $date->setTime(0, 0); |
| 67 | } else { |
| 68 | $date = $date->sub(new \DateInterval('P1D')); |
| 69 | $date = $date->setTime(23, 59); |
| 70 | } |
| 71 | return $this; |
| 72 | } |
| 73 | public function validate(string $value): bool |
| 74 | { |
| 75 | $basicChecks = parent::validate($value); |
| 76 | // Validate that a list don't have W or L |
| 77 | if (false !== strpos($value, ',') && (false !== strpos($value, 'W') || false !== strpos($value, 'L'))) { |
| 78 | return false; |
| 79 | } |
| 80 | if (!$basicChecks) { |
| 81 | if ('?' === $value) { |
| 82 | return true; |
| 83 | } |
| 84 | if ('L' === $value) { |
| 85 | return true; |
| 86 | } |
| 87 | if (preg_match('/^(.*)W$/', $value, $matches)) { |
| 88 | return $this->validate($matches[1]); |
| 89 | } |
| 90 | return false; |
| 91 | } |
| 92 | return $basicChecks; |
| 93 | } |
| 94 | } |
| 95 |