25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

174 satır
5.5 KiB

  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. /**
  25. * Get the nearest day of the week for a given day in a month
  26. *
  27. * @param int $currentYear Current year
  28. * @param int $currentMonth Current month
  29. * @param int $targetDay Target day of the month
  30. *
  31. * @return \DateTime Returns the nearest date
  32. */
  33. private static function getNearestWeekday($currentYear, $currentMonth, $targetDay)
  34. {
  35. $tday = str_pad($targetDay, 2, '0', STR_PAD_LEFT);
  36. $target = DateTime::createFromFormat('Y-m-d', "$currentYear-$currentMonth-$tday");
  37. $currentWeekday = (int) $target->format('N');
  38. if ($currentWeekday < 6) {
  39. return $target;
  40. }
  41. $lastDayOfMonth = $target->format('t');
  42. foreach (array(-1, 1, -2, 2) as $i) {
  43. $adjusted = $targetDay + $i;
  44. if ($adjusted > 0 && $adjusted <= $lastDayOfMonth) {
  45. $target->setDate($currentYear, $currentMonth, $adjusted);
  46. if ($target->format('N') < 6 && $target->format('m') == $currentMonth) {
  47. return $target;
  48. }
  49. }
  50. }
  51. }
  52. public function isSatisfiedBy(DateTime $date, $value)
  53. {
  54. // ? states that the field value is to be skipped
  55. if ($value == '?') {
  56. return true;
  57. }
  58. $fieldValue = $date->format('d');
  59. // Check to see if this is the last day of the month
  60. if ($value == 'L') {
  61. return $fieldValue == $date->format('t');
  62. }
  63. // Check to see if this is the nearest weekday to a particular value
  64. if (strpos($value, 'W')) {
  65. // Parse the target day
  66. $targetDay = substr($value, 0, strpos($value, 'W'));
  67. // Find out if the current day is the nearest day of the week
  68. return $date->format('j') == self::getNearestWeekday(
  69. $date->format('Y'),
  70. $date->format('m'),
  71. $targetDay
  72. )->format('j');
  73. }
  74. return $this->isSatisfied($date->format('d'), $value);
  75. }
  76. public function increment(DateTime $date, $invert = false)
  77. {
  78. if ($invert) {
  79. $date->modify('previous day');
  80. $date->setTime(23, 59);
  81. } else {
  82. $date->modify('next day');
  83. $date->setTime(0, 0);
  84. }
  85. return $this;
  86. }
  87. /**
  88. * Validates that the value is valid for the Day of the Month field
  89. * Days of the month can contain values of 1-31, *, L, or ? by default. This can be augmented with lists via a ',',
  90. * ranges via a '-', or with a '[0-9]W' to specify the closest weekday.
  91. *
  92. * @param string $value
  93. * @return bool
  94. */
  95. public function validate($value)
  96. {
  97. // Allow wildcards and a single L
  98. if ($value === '?' || $value === '*' || $value === 'L') {
  99. return true;
  100. }
  101. // If you only contain numbers and are within 1-31
  102. if ((bool) preg_match('/^\d{1,2}$/', $value) && ($value >= 1 && $value <= 31)) {
  103. return true;
  104. }
  105. // If you have a -, we will deal with each of your chunks
  106. if ((bool) preg_match('/-/', $value)) {
  107. // We cannot have a range within a list or vice versa
  108. if ((bool) preg_match('/,/', $value)) {
  109. return false;
  110. }
  111. $chunks = explode('-', $value);
  112. foreach ($chunks as $chunk) {
  113. if (!$this->validate($chunk)) {
  114. return false;
  115. }
  116. }
  117. return true;
  118. }
  119. // If you have a comma, we will deal with each value
  120. if ((bool) preg_match('/,/', $value)) {
  121. // We cannot have a range within a list or vice versa
  122. if ((bool) preg_match('/-/', $value)) {
  123. return false;
  124. }
  125. $chunks = explode(',', $value);
  126. foreach ($chunks as $chunk) {
  127. if (!$this->validate($chunk)) {
  128. return false;
  129. }
  130. }
  131. return true;
  132. }
  133. // If you contain a /, we'll deal with it
  134. if ((bool) preg_match('/\//', $value)) {
  135. $chunks = explode('/', $value);
  136. foreach ($chunks as $chunk) {
  137. if (!$this->validate($chunk)) {
  138. return false;
  139. }
  140. }
  141. return true;
  142. }
  143. // If you end in W, make sure that it has a numeric in front of it
  144. if ((bool) preg_match('/^\d{1,2}W$/', $value)) {
  145. return true;
  146. }
  147. return false;
  148. }
  149. }