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