mailpoet
/
vendor
/
woocommerce
/
action-scheduler
/
lib
/
cron-expression
/
CronExpression_DayOfMonthField.php
CronExpression.php
1 year ago
CronExpression_AbstractField.php
4 years ago
CronExpression_DayOfMonthField.php
4 years ago
CronExpression_DayOfWeekField.php
4 years ago
CronExpression_FieldFactory.php
4 years ago
CronExpression_FieldInterface.php
4 years ago
CronExpression_HoursField.php
4 years ago
CronExpression_MinutesField.php
4 years ago
CronExpression_MonthField.php
4 years ago
CronExpression_YearField.php
4 years ago
index.php
3 years ago
CronExpression_DayOfMonthField.php
64 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | class CronExpression_DayOfMonthField extends CronExpression_AbstractField |
| 4 | { |
| 5 | private static function getNearestWeekday($currentYear, $currentMonth, $targetDay) |
| 6 | { |
| 7 | $tday = str_pad($targetDay, 2, '0', STR_PAD_LEFT); |
| 8 | $target = new DateTime("$currentYear-$currentMonth-$tday"); |
| 9 | $currentWeekday = (int) $target->format('N'); |
| 10 | if ($currentWeekday < 6) { |
| 11 | return $target; |
| 12 | } |
| 13 | $lastDayOfMonth = $target->format('t'); |
| 14 | foreach (array(-1, 1, -2, 2) as $i) { |
| 15 | $adjusted = $targetDay + $i; |
| 16 | if ($adjusted > 0 && $adjusted <= $lastDayOfMonth) { |
| 17 | $target->setDate($currentYear, $currentMonth, $adjusted); |
| 18 | if ($target->format('N') < 6 && $target->format('m') == $currentMonth) { |
| 19 | return $target; |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | public function isSatisfiedBy(DateTime $date, $value) |
| 25 | { |
| 26 | // ? states that the field value is to be skipped |
| 27 | if ($value == '?') { |
| 28 | return true; |
| 29 | } |
| 30 | $fieldValue = $date->format('d'); |
| 31 | // Check to see if this is the last day of the month |
| 32 | if ($value == 'L') { |
| 33 | return $fieldValue == $date->format('t'); |
| 34 | } |
| 35 | // Check to see if this is the nearest weekday to a particular value |
| 36 | if (strpos($value, 'W')) { |
| 37 | // Parse the target day |
| 38 | $targetDay = substr($value, 0, strpos($value, 'W')); |
| 39 | // Find out if the current day is the nearest day of the week |
| 40 | return $date->format('j') == self::getNearestWeekday( |
| 41 | $date->format('Y'), |
| 42 | $date->format('m'), |
| 43 | $targetDay |
| 44 | )->format('j'); |
| 45 | } |
| 46 | return $this->isSatisfied($date->format('d'), $value); |
| 47 | } |
| 48 | public function increment(DateTime $date, $invert = false) |
| 49 | { |
| 50 | if ($invert) { |
| 51 | $date->modify('previous day'); |
| 52 | $date->setTime(23, 59); |
| 53 | } else { |
| 54 | $date->modify('next day'); |
| 55 | $date->setTime(0, 0); |
| 56 | } |
| 57 | return $this; |
| 58 | } |
| 59 | public function validate($value) |
| 60 | { |
| 61 | return (bool) preg_match('/[\*,\/\-\?LW0-9A-Za-z]+/', $value); |
| 62 | } |
| 63 | } |
| 64 |