From 0be810faccf4f3ea4f9b8c026e897f71f2c61503 Mon Sep 17 00:00:00 2001 From: kylekatarnls Date: Sun, 17 Nov 2024 12:39:06 +0100 Subject: [PATCH 1/2] Add tests for #258 --- tests/OpeningHoursOverflowTest.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/OpeningHoursOverflowTest.php b/tests/OpeningHoursOverflowTest.php index 1a9aebb..67ec7dd 100644 --- a/tests/OpeningHoursOverflowTest.php +++ b/tests/OpeningHoursOverflowTest.php @@ -177,4 +177,32 @@ 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')); + + $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-12 04:00:00')); + + $this->assertSame('2024-11-12 05:00', $nextClose->format('Y-m-d H:i')); + } } From 03a676bb9de41e2a76c746428fb518741571d180 Mon Sep 17 00:00:00 2001 From: kylekatarnls Date: Sun, 17 Nov 2024 13:58:13 +0100 Subject: [PATCH 2/2] Use current range end for next close if available --- src/OpeningHours.php | 15 ++++++++++++++- src/Time.php | 2 +- tests/OpeningHoursOverflowTest.php | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/OpeningHours.php b/src/OpeningHours.php index aa5ba51..c834936 100644 --- a/src/OpeningHours.php +++ b/src/OpeningHours.php @@ -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) { @@ -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'); } } diff --git a/src/Time.php b/src/Time.php index 8c2c903..d7f6496 100644 --- a/src/Time.php +++ b/src/Time.php @@ -45,7 +45,7 @@ public function minutes(): int return $this->minutes; } - public function date(): DateTimeInterface + public function date(): ?DateTimeInterface { return $this->date; } diff --git a/tests/OpeningHoursOverflowTest.php b/tests/OpeningHoursOverflowTest.php index 67ec7dd..530cb59 100644 --- a/tests/OpeningHoursOverflowTest.php +++ b/tests/OpeningHoursOverflowTest.php @@ -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'));