wp-mail-smtp
/
vendor
/
woocommerce
/
action-scheduler
/
lib
/
cron-expression
/
CronExpression_DayOfMonthField.php
CronExpression.php
5 years ago
CronExpression_AbstractField.php
5 years ago
CronExpression_DayOfMonthField.php
5 years ago
CronExpression_DayOfWeekField.php
5 years ago
CronExpression_FieldFactory.php
5 years ago
CronExpression_FieldInterface.php
5 years ago
CronExpression_HoursField.php
5 years ago
CronExpression_MinutesField.php
5 years ago
CronExpression_MonthField.php
5 years ago
CronExpression_YearField.php
5 years ago
LICENSE
5 years ago
CronExpression_DayOfMonthField.php
111 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Day of month field. Allows: * , / - ? L W |
| 5 | * |
| 6 | * 'L' stands for "last" and specifies the last day of the month. |
| 7 | * |
| 8 | * The 'W' character is used to specify the weekday (Monday-Friday) nearest the |
| 9 | * given day. As an example, if you were to specify "15W" as the value for the |
| 10 | * day-of-month field, the meaning is: "the nearest weekday to the 15th of the |
| 11 | * month". So if the 15th is a Saturday, the trigger will fire on Friday the |
| 12 | * 14th. If the 15th is a Sunday, the trigger will fire on Monday the 16th. If |
| 13 | * the 15th is a Tuesday, then it will fire on Tuesday the 15th. However if you |
| 14 | * specify "1W" as the value for day-of-month, and the 1st is a Saturday, the |
| 15 | * trigger will fire on Monday the 3rd, as it will not 'jump' over the boundary |
| 16 | * of a month's days. The 'W' character can only be specified when the |
| 17 | * day-of-month is a single day, not a range or list of days. |
| 18 | * |
| 19 | * @author Michael Dowling <mtdowling@gmail.com> |
| 20 | */ |
| 21 | class CronExpression_DayOfMonthField extends CronExpression_AbstractField |
| 22 | { |
| 23 | /** |
| 24 | * Get the nearest day of the week for a given day in a month |
| 25 | * |
| 26 | * @param int $currentYear Current year |
| 27 | * @param int $currentMonth Current month |
| 28 | * @param int $targetDay Target day of the month |
| 29 | * |
| 30 | * @return DateTime Returns the nearest date |
| 31 | */ |
| 32 | private static function getNearestWeekday($currentYear, $currentMonth, $targetDay) |
| 33 | { |
| 34 | $tday = str_pad($targetDay, 2, '0', STR_PAD_LEFT); |
| 35 | $target = new DateTime("$currentYear-$currentMonth-$tday"); |
| 36 | $currentWeekday = (int) $target->format('N'); |
| 37 | |
| 38 | if ($currentWeekday < 6) { |
| 39 | return $target; |
| 40 | } |
| 41 | |
| 42 | $lastDayOfMonth = $target->format('t'); |
| 43 | |
| 44 | foreach (array(-1, 1, -2, 2) as $i) { |
| 45 | $adjusted = $targetDay + $i; |
| 46 | if ($adjusted > 0 && $adjusted <= $lastDayOfMonth) { |
| 47 | $target->setDate($currentYear, $currentMonth, $adjusted); |
| 48 | if ($target->format('N') < 6 && $target->format('m') == $currentMonth) { |
| 49 | return $target; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * {@inheritdoc} |
| 57 | */ |
| 58 | public function isSatisfiedBy(DateTime $date, $value) |
| 59 | { |
| 60 | // ? states that the field value is to be skipped |
| 61 | if ($value == '?') { |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | $fieldValue = $date->format('d'); |
| 66 | |
| 67 | // Check to see if this is the last day of the month |
| 68 | if ($value == 'L') { |
| 69 | return $fieldValue == $date->format('t'); |
| 70 | } |
| 71 | |
| 72 | // Check to see if this is the nearest weekday to a particular value |
| 73 | if (strpos($value, 'W')) { |
| 74 | // Parse the target day |
| 75 | $targetDay = substr($value, 0, strpos($value, 'W')); |
| 76 | // Find out if the current day is the nearest day of the week |
| 77 | return $date->format('j') == self::getNearestWeekday( |
| 78 | $date->format('Y'), |
| 79 | $date->format('m'), |
| 80 | $targetDay |
| 81 | )->format('j'); |
| 82 | } |
| 83 | |
| 84 | return $this->isSatisfied($date->format('d'), $value); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * {@inheritdoc} |
| 89 | */ |
| 90 | public function increment(DateTime $date, $invert = false) |
| 91 | { |
| 92 | if ($invert) { |
| 93 | $date->modify('previous day'); |
| 94 | $date->setTime(23, 59); |
| 95 | } else { |
| 96 | $date->modify('next day'); |
| 97 | $date->setTime(0, 0); |
| 98 | } |
| 99 | |
| 100 | return $this; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * {@inheritdoc} |
| 105 | */ |
| 106 | public function validate($value) |
| 107 | { |
| 108 | return (bool) preg_match('/[\*,\/\-\?LW0-9A-Za-z]+/', $value); |
| 109 | } |
| 110 | } |
| 111 |