MinutesField.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Cron;
  3. use DateTime;
  4. /**
  5. * Minutes field. Allows: * , / -
  6. */
  7. class MinutesField extends AbstractField
  8. {
  9. protected $rangeStart = 0;
  10. protected $rangeEnd = 59;
  11. public function isSatisfiedBy(DateTime $date, $value)
  12. {
  13. return $this->isSatisfied($date->format('i'), $value);
  14. }
  15. public function increment(DateTime $date, $invert = false, $parts = null)
  16. {
  17. if (is_null($parts)) {
  18. if ($invert) {
  19. $date->modify('-1 minute');
  20. } else {
  21. $date->modify('+1 minute');
  22. }
  23. return $this;
  24. }
  25. $parts = strpos($parts, ',') !== false ? explode(',', $parts) : array($parts);
  26. $minutes = array();
  27. foreach ($parts as $part) {
  28. $minutes = array_merge($minutes, $this->getRangeForExpression($part, 59));
  29. }
  30. $current_minute = $date->format('i');
  31. $position = $invert ? count($minutes) - 1 : 0;
  32. if (count($minutes) > 1) {
  33. for ($i = 0; $i < count($minutes) - 1; $i++) {
  34. if ((!$invert && $current_minute >= $minutes[$i] && $current_minute < $minutes[$i + 1]) ||
  35. ($invert && $current_minute > $minutes[$i] && $current_minute <= $minutes[$i + 1])) {
  36. $position = $invert ? $i : $i + 1;
  37. break;
  38. }
  39. }
  40. }
  41. if ((!$invert && $current_minute >= $minutes[$position]) || ($invert && $current_minute <= $minutes[$position])) {
  42. $date->modify(($invert ? '-' : '+') . '1 hour');
  43. $date->setTime($date->format('H'), $invert ? 59 : 0);
  44. }
  45. else {
  46. $date->setTime($date->format('H'), $minutes[$position]);
  47. }
  48. return $this;
  49. }
  50. }