DayOfMonthField.php 4.0 KB

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