1<?php
2
3namespace Cron;
4
5use DateTime;
6
7/**
8 * Day of month field.  Allows: * , / - ? L W
9 *
10 * 'L' stands for "last" and specifies the last day of the month.
11 *
12 * The 'W' character is used to specify the weekday (Monday-Friday) nearest the
13 * given day. As an example, if you were to specify "15W" as the value for the
14 * day-of-month field, the meaning is: "the nearest weekday to the 15th of the
15 * month". So if the 15th is a Saturday, the trigger will fire on Friday the
16 * 14th. If the 15th is a Sunday, the trigger will fire on Monday the 16th. If
17 * the 15th is a Tuesday, then it will fire on Tuesday the 15th. However if you
18 * specify "1W" as the value for day-of-month, and the 1st is a Saturday, the
19 * trigger will fire on Monday the 3rd, as it will not 'jump' over the boundary
20 * of a month's days. The 'W' character can only be specified when the
21 * day-of-month is a single day, not a range or list of days.
22 *
23 * @author Michael Dowling <mtdowling@gmail.com>
24 */
25class DayOfMonthField extends AbstractField
26{
27    /**
28     * Get the nearest day of the week for a given day in a month
29     *
30     * @param int $currentYear  Current year
31     * @param int $currentMonth Current month
32     * @param int $targetDay    Target day of the month
33     *
34     * @return \DateTime Returns the nearest date
35     */
36    private static function getNearestWeekday($currentYear, $currentMonth, $targetDay)
37    {
38        $tday = str_pad($targetDay, 2, '0', STR_PAD_LEFT);
39        $target = DateTime::createFromFormat('Y-m-d', "$currentYear-$currentMonth-$tday");
40        $currentWeekday = (int) $target->format('N');
41
42        if ($currentWeekday < 6) {
43            return $target;
44        }
45
46        $lastDayOfMonth = $target->format('t');
47
48        foreach (array(-1, 1, -2, 2) as $i) {
49            $adjusted = $targetDay + $i;
50            if ($adjusted > 0 && $adjusted <= $lastDayOfMonth) {
51                $target->setDate($currentYear, $currentMonth, $adjusted);
52                if ($target->format('N') < 6 && $target->format('m') == $currentMonth) {
53                    return $target;
54                }
55            }
56        }
57    }
58
59    public function isSatisfiedBy(DateTime $date, $value)
60    {
61        // ? states that the field value is to be skipped
62        if ($value == '?') {
63            return true;
64        }
65
66        $fieldValue = $date->format('d');
67
68        // Check to see if this is the last day of the month
69        if ($value == 'L') {
70            return $fieldValue == $date->format('t');
71        }
72
73        // Check to see if this is the nearest weekday to a particular value
74        if (strpos($value, 'W')) {
75            // Parse the target day
76            $targetDay = substr($value, 0, strpos($value, 'W'));
77            // Find out if the current day is the nearest day of the week
78            return $date->format('j') == self::getNearestWeekday(
79                $date->format('Y'),
80                $date->format('m'),
81                $targetDay
82            )->format('j');
83        }
84
85        return $this->isSatisfied($date->format('d'), $value);
86    }
87
88    public function increment(DateTime $date, $invert = false)
89    {
90        if ($invert) {
91            $date->modify('previous day');
92            $date->setTime(23, 59);
93        } else {
94            $date->modify('next day');
95            $date->setTime(0, 0);
96        }
97
98        return $this;
99    }
100
101    /**
102     * Validates that the value is valid for the Day of the Month field
103     * Days of the month can contain values of 1-31, *, L, or ? by default. This can be augmented with lists via a ',',
104     * ranges via a '-', or with a '[0-9]W' to specify the closest weekday.
105     *
106     * @param string $value
107     * @return bool
108     */
109    public function validate($value)
110    {
111        // Allow wildcards and a single L
112        if ($value === '?' || $value === '*' || $value === 'L') {
113            return true;
114        }
115
116        // If you only contain numbers and are within 1-31
117        if ((bool) preg_match('/^\d{1,2}$/', $value) && ($value >= 1 && $value <= 31)) {
118            return true;
119        }
120
121        // If you have a -, we will deal with each of your chunks
122        if ((bool) preg_match('/-/', $value)) {
123            // We cannot have a range within a list or vice versa
124            if ((bool) preg_match('/,/', $value)) {
125                return false;
126            }
127
128            $chunks = explode('-', $value);
129            foreach ($chunks as $chunk) {
130                if (!$this->validate($chunk)) {
131                    return false;
132                }
133            }
134
135            return true;
136        }
137
138        // If you have a comma, we will deal with each value
139        if ((bool) preg_match('/,/', $value)) {
140            // We cannot have a range within a list or vice versa
141            if ((bool) preg_match('/-/', $value)) {
142                return false;
143            }
144
145            $chunks = explode(',', $value);
146            foreach ($chunks as $chunk) {
147                if (!$this->validate($chunk)) {
148                    return false;
149                }
150            }
151
152            return true;
153        }
154
155        // If you contain a /, we'll deal with it
156        if ((bool) preg_match('/\//', $value)) {
157            $chunks = explode('/', $value);
158            foreach ($chunks as $chunk) {
159                if (!$this->validate($chunk)) {
160                    return false;
161                }
162            }
163            return true;
164        }
165
166        // If you end in W, make sure that it has a numeric in front of it
167        if ((bool) preg_match('/^\d{1,2}W$/', $value)) {
168            return true;
169        }
170
171        return false;
172    }
173}
174