Skip to content

Commit

Permalink
Merge pull request #259 from spatie/fix/issue-258-overflow-next-close
Browse files Browse the repository at this point in the history
Fix nextClose() result with overflow over night
  • Loading branch information
kylekatarnls authored Nov 17, 2024
2 parents bfa3956 + 03a676b commit 773ab4f
Show file tree
Hide file tree
Showing 3 changed files with 59 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 && $openRangeEnd->hours() < 24) {
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
44 changes: 44 additions & 0 deletions tests/OpeningHoursOverflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,48 @@ public function overflow_on_simple_ranges()
$this->assertFalse($openWithOverflow->isOpenAt($time));
$this->assertFalse($openWithoutOverflow->isOpenAt($time));
}

#[Test]
public function overflow_next_close()
{
$openingHours = OpeningHours::create([
'overflow' => true,
'monday' => ['18:00-05:00'], // 2024-11-11
'tuesday' => ['17:00-06:00'], // 2024-11-12
]);

$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'));

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

$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'));
}
}

0 comments on commit 773ab4f

Please sign in to comment.