MonthField.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. declare(strict_types=1);
  3. namespace Cron;
  4. use DateTimeInterface;
  5. /**
  6. * Month field. Allows: * , / -.
  7. */
  8. class MonthField extends AbstractField
  9. {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. protected $rangeStart = 1;
  14. /**
  15. * {@inheritdoc}
  16. */
  17. protected $rangeEnd = 12;
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected $literals = [1 => 'JAN', 2 => 'FEB', 3 => 'MAR', 4 => 'APR', 5 => 'MAY', 6 => 'JUN', 7 => 'JUL',
  22. 8 => 'AUG', 9 => 'SEP', 10 => 'OCT', 11 => 'NOV', 12 => 'DEC', ];
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function isSatisfiedBy(DateTimeInterface $date, $value): bool
  27. {
  28. if ($value == '?') {
  29. return true;
  30. }
  31. $value = $this->convertLiterals($value);
  32. return $this->isSatisfied((int) $date->format('m'), $value);
  33. }
  34. /**
  35. * @inheritDoc
  36. *
  37. * @param \DateTime|\DateTimeImmutable $date
  38. */
  39. public function increment(DateTimeInterface &$date, $invert = false, $parts = null): FieldInterface
  40. {
  41. if ($invert) {
  42. $date = $date->modify('last day of previous month')->setTime(23, 59);
  43. } else {
  44. $date = $date->modify('first day of next month')->setTime(0, 0);
  45. }
  46. return $this;
  47. }
  48. }