mailpoet
/
vendor
/
woocommerce
/
action-scheduler
/
lib
/
cron-expression
/
CronExpression_DayOfWeekField.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_DayOfWeekField.php
88 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | class CronExpression_DayOfWeekField extends CronExpression_AbstractField |
| 4 | { |
| 5 | public function isSatisfiedBy(DateTime $date, $value) |
| 6 | { |
| 7 | if ($value == '?') { |
| 8 | return true; |
| 9 | } |
| 10 | // Convert text day of the week values to integers |
| 11 | $value = str_ireplace( |
| 12 | array('SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'), |
| 13 | range(0, 6), |
| 14 | $value |
| 15 | ); |
| 16 | $currentYear = $date->format('Y'); |
| 17 | $currentMonth = $date->format('m'); |
| 18 | $lastDayOfMonth = $date->format('t'); |
| 19 | // Find out if this is the last specific weekday of the month |
| 20 | if (strpos($value, 'L')) { |
| 21 | $weekday = str_replace('7', '0', substr($value, 0, strpos($value, 'L'))); |
| 22 | $tdate = clone $date; |
| 23 | $tdate->setDate($currentYear, $currentMonth, $lastDayOfMonth); |
| 24 | while ($tdate->format('w') != $weekday) { |
| 25 | $tdate->setDate($currentYear, $currentMonth, --$lastDayOfMonth); |
| 26 | } |
| 27 | return $date->format('j') == $lastDayOfMonth; |
| 28 | } |
| 29 | // Handle # hash tokens |
| 30 | if (strpos($value, '#')) { |
| 31 | list($weekday, $nth) = explode('#', $value); |
| 32 | // Validate the hash fields |
| 33 | if ($weekday < 1 || $weekday > 5) { |
| 34 | throw new InvalidArgumentException("Weekday must be a value between 1 and 5. {$weekday} given"); |
| 35 | } |
| 36 | if ($nth > 5) { |
| 37 | throw new InvalidArgumentException('There are never more than 5 of a given weekday in a month'); |
| 38 | } |
| 39 | // The current weekday must match the targeted weekday to proceed |
| 40 | if ($date->format('N') != $weekday) { |
| 41 | return false; |
| 42 | } |
| 43 | $tdate = clone $date; |
| 44 | $tdate->setDate($currentYear, $currentMonth, 1); |
| 45 | $dayCount = 0; |
| 46 | $currentDay = 1; |
| 47 | while ($currentDay < $lastDayOfMonth + 1) { |
| 48 | if ($tdate->format('N') == $weekday) { |
| 49 | if (++$dayCount >= $nth) { |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | $tdate->setDate($currentYear, $currentMonth, ++$currentDay); |
| 54 | } |
| 55 | return $date->format('j') == $currentDay; |
| 56 | } |
| 57 | // Handle day of the week values |
| 58 | if (strpos($value, '-')) { |
| 59 | $parts = explode('-', $value); |
| 60 | if ($parts[0] == '7') { |
| 61 | $parts[0] = '0'; |
| 62 | } elseif ($parts[1] == '0') { |
| 63 | $parts[1] = '7'; |
| 64 | } |
| 65 | $value = implode('-', $parts); |
| 66 | } |
| 67 | // Test to see which Sunday to use -- 0 == 7 == Sunday |
| 68 | $format = in_array(7, str_split($value)) ? 'N' : 'w'; |
| 69 | $fieldValue = $date->format($format); |
| 70 | return $this->isSatisfied($fieldValue, $value); |
| 71 | } |
| 72 | public function increment(DateTime $date, $invert = false) |
| 73 | { |
| 74 | if ($invert) { |
| 75 | $date->modify('-1 day'); |
| 76 | $date->setTime(23, 59, 0); |
| 77 | } else { |
| 78 | $date->modify('+1 day'); |
| 79 | $date->setTime(0, 0, 0); |
| 80 | } |
| 81 | return $this; |
| 82 | } |
| 83 | public function validate($value) |
| 84 | { |
| 85 | return (bool) preg_match('/[\*,\/\-0-9A-Z]+/', $value); |
| 86 | } |
| 87 | } |
| 88 |