Skip to content

Commit

Permalink
Use current range end for next close if available
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Nov 17, 2024
1 parent 0be810f commit 95ff51e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/OpeningHours.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,12 @@ public function nextClose(
$outputTimezone = $this->getOutputTimezone($dateTime);
$dateTime = $this->applyTimezone($dateTime ?? new $this->dateTimeClass());
$dateTime = $this->copyDateTime($dateTime);
$openRangeEnd = $this->currentOpenRange($dateTime)?->end();

if ($openRangeEnd) {
return $openRangeEnd->date() ?? $openRangeEnd->toDateTime($dateTime);
}

$nextClose = null;

if ($this->overflow) {
Expand All @@ -541,7 +547,14 @@ public function nextClose(
if (! $nextClose) {
$nextClose = $openingHoursForDay->nextClose(PreciseTime::fromDateTime($dateTime));

if ($nextClose && $nextClose->hours() < 24 && $nextClose->format('Gi') < $dateTime->format('Gi')) {
if (
$nextClose
&& $nextClose->hours() < 24
&& (
$nextClose->format('Gi') < $dateTime->format('Gi')
|| ($this->isClosedAt($dateTime) && $this->nextOpen($dateTime)->format('Gi') > $nextClose->format('Gi'))
)
) {
$dateTime = $dateTime->modify('+1 day');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function minutes(): int
return $this->minutes;
}

public function date(): DateTimeInterface
public function date(): ?DateTimeInterface
{
return $this->date;
}
Expand Down
16 changes: 16 additions & 0 deletions tests/OpeningHoursOverflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,28 @@ public function overflow_next_close()

$this->assertSame('2024-11-13 06:00', $nextClose->format('Y-m-d H:i'));

$nextClose = $openingHours->nextClose(new DateTime('2024-11-12 05:30:00'));

$openingHours = OpeningHours::create([
'overflow' => true,
'monday' => ['18:00-05:00'], // 2024-11-11
'tuesday' => ['05:40-05:50', '17:00-06:00'], // 2024-11-12
]);

$nextClose = $openingHours->nextClose(new DateTime('2024-11-12 05:30:00'));

$this->assertSame('2024-11-12 05:50', $nextClose->format('Y-m-d H:i'));

$openingHours = OpeningHours::create([
'overflow' => true,
'monday' => ['18:00-22:00', '23:00-05:00'], // 2024-11-11
'tuesday' => ['17:00-06:00'], // 2024-11-12
]);

$nextClose = $openingHours->nextClose(new DateTime('2024-11-11 23:30:00'));

$this->assertSame('2024-11-12 05:00', $nextClose->format('Y-m-d H:i'));

$nextClose = $openingHours->nextClose(new DateTime('2024-11-12 04:00:00'));

$this->assertSame('2024-11-12 05:00', $nextClose->format('Y-m-d H:i'));
Expand Down

0 comments on commit 95ff51e

Please sign in to comment.