Skip to content

Commit

Permalink
php8
Browse files Browse the repository at this point in the history
- min php version updated to 8.0
- code refactor for php 8
- bug fix to avoid duplicate dates when calculated dates from different schedules match
  • Loading branch information
jupitern committed Dec 4, 2024
1 parent 61314e6 commit e5b404b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 39 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ output:

## ChangeLog

v1.4

- min php version updated to 8.0
- code refactor for php 8
- bug fix to avoid duplicate dates when calculated dates from different schedules match


v1.3

- Changed method add to allow relative one time events like "+1 hour" or "next day 17:00"
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"issues": "https://github.com/jupitern/scheduler/issues"
},
"require" :{
"php":">=5.4"
"php":">=8.0"
},
"autoload": {
"psr-4": {
Expand Down
76 changes: 38 additions & 38 deletions src/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@

namespace Jupitern\Scheduler;

/*
* Simple Scheduler class
*
* Author: Nuno Chaves
* */

class Scheduler {

private $schedules = [];
private $oneTimeEvents = [];
private $startTime = null;
private $endTime = null;
private array $schedules = [];
private array $oneTimeEvents = [];
private \DateTime|null $startTime = null;
private \DateTime|null $endTime = null;

/**
* @return static
Expand All @@ -27,15 +21,17 @@ public static function instance()
/**
* set a time frame in which events will occur
*
* @param string $startTime time string compatible with \Datetime object. example: '08:00'
* @param string $endTime time string compatible with \Datetime object. example: '17:00'
* @param string|null $startTime time string compatible with \Datetime object. example: '08:00'
* @param string|null $endTime time string compatible with \Datetime object. example: '17:00'
* @return Scheduler
* @throws \Exception
*/
public function setTimeFrame( $startTime = null, $endTime = null )
public function setTimeFrame(string $startTime = null, string $endTime = null): self
{
if ($startTime !== null && !empty($startTime)) {
if (!empty($startTime)) {
$this->startTime = new \DateTime($startTime);
}
if ($endTime !== null && !empty($endTime)) {
if (!empty($endTime)) {
$this->endTime = new \DateTime($endTime);
}

Expand All @@ -48,7 +44,7 @@ public function setTimeFrame( $startTime = null, $endTime = null )
*
* @param string $dateTimeStr date string compatible with \Datetime object
*/
public function add( $dateTimeStr )
public function add(string $dateTimeStr): self
{
$this->oneTimeEvents[] = $dateTimeStr;
return $this;
Expand All @@ -59,7 +55,7 @@ public function add( $dateTimeStr )
*
* @param string $dateTimeStr date string compatible with \Datetime object
*/
public function addRecurring( $dateTimeStr )
public function addRecurring(string $dateTimeStr): self
{
$this->schedules[] = $dateTimeStr;
return $this;
Expand All @@ -70,9 +66,9 @@ public function addRecurring( $dateTimeStr )
* get next schedule date
*
* @param string $fromDateStr date string compatible with \Datetime object
* @return \Datetime or null
* @return \Datetime|null or null
*/
public function getNextSchedule( $fromDateStr = 'now' )
public function getNextSchedule(string $fromDateStr = 'now'): ?\Datetime
{
$dates = $this->getNextSchedules($fromDateStr, 1);
return count($dates) ? $dates[0] : null;
Expand All @@ -86,16 +82,16 @@ public function getNextSchedule( $fromDateStr = 'now' )
* @param int $limit number of dates to return
* @return array
*/
public function getNextSchedules( $fromDateStr = 'now', $limit = 5 )
public function getNextSchedules(string $fromDateStr = 'now', int $limit = 5): array
{
$dates = [];

foreach ($this->oneTimeEvents as $schedule) {
$dt = new \DateTime($fromDateStr);
$dt->modify($schedule);
$dt = new \DateTime($fromDateStr);
$dt->modify($schedule);

if ($this->isInTimeFrame($dt, $fromDateStr)) {
$dates[] = $dt;
$dates[$dt->format('YmdHis')] = $dt;
}
}

Expand All @@ -112,11 +108,15 @@ public function getNextSchedules( $fromDateStr = 'now', $limit = 5 )
$d->modify('next day')->modify($this->startTime->format('H:i:s'));
}

$dates[] = clone $d;
if (!array_key_exists($d->format('YmdHis'), $dates)) {
$dates[$d->format('YmdHis')] = clone $d;
}
$d->modify($schedule);
}
elseif ($this->isInTimeFrame($d->modify($schedule), $fromDateStr)) {
$dates[] = clone $d;
if (!array_key_exists($d->format('YmdHis'), $dates)) {
$dates[$d->format('YmdHis')] = clone $d;
}
}
else {
--$i;
Expand All @@ -131,9 +131,9 @@ public function getNextSchedules( $fromDateStr = 'now', $limit = 5 )

/**
* @param array $dates
* @return array
* @return void
*/
private function orderDates( &$dates )
private function orderDates(array &$dates): void
{
uasort($dates, function($a, $b) {
return strtotime($a->format('Y-m-d H:i:s')) > strtotime($b->format('Y-m-d H:i:s')) ? 1 : -1;
Expand All @@ -142,9 +142,9 @@ private function orderDates( &$dates )

/**
* @param \DateTime $date
* @return bool
* @return bool
*/
private function isInTimeFrame(\DateTime $date, $fromDateStr = 'now')
private function isInTimeFrame(\DateTime $date, string $fromDateStr = 'now'): bool
{
if ($date < new \DateTime($fromDateStr)) {
return false;
Expand All @@ -160,13 +160,13 @@ private function isInTimeFrame(\DateTime $date, $fromDateStr = 'now')
return true;
}

/**
* @param string $dateStr \Datetime object valid date string
* @return bool
*/
private function isDateRelative($dateStr)
{
return strpos($dateStr, '+') !== false;
}
/**
* @param string $dateStr \Datetime object valid date string
* @return bool
*/
private function isDateRelative(string $dateStr): bool
{
return str_contains($dateStr, '+');
}

}

0 comments on commit e5b404b

Please sign in to comment.