Skip to content

Commit 630883b

Browse files
fix: unable to correctly determine isAlwaysClosed and isAlwaysOpen with exceptions
- Introduced `everyExceptions` method for checking conditions on exceptions. - Updated `isAlwaysClosed` and `isAlwaysOpen` methods to respect exceptions. - Added tests for various scenarios of opening hours and exceptions.
1 parent 773ab4f commit 630883b

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed

src/OpeningHours.php

+20-6
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,14 @@ public function filterExceptions(callable $callback): array
935935
return Arr::filter($this->exceptions, $callback);
936936
}
937937

938+
/** Checks that all exceptions match a given condition */
939+
public function everyExceptions(callable $callback): bool
940+
{
941+
return $this->filterExceptions(
942+
static fn (OpeningHoursForDay $day) => ! $callback($day),
943+
) === [];
944+
}
945+
938946
public function mapExceptions(callable $callback): array
939947
{
940948
return Arr::map($this->exceptions, $callback);
@@ -999,16 +1007,22 @@ static function (OpeningHoursForDay $openingHoursForDay, string $date) use ($for
9991007

10001008
public function isAlwaysClosed(): bool
10011009
{
1002-
return $this->exceptions === [] && $this->filters === [] && $this->every(
1003-
static fn (OpeningHoursForDay $day) => $day->isEmpty(),
1004-
);
1010+
$isAlwaysClosedCallback = static fn (OpeningHoursForDay $day) => $day->isEmpty();
1011+
$allExceptionsAlwaysClosed = $this->everyExceptions($isAlwaysClosedCallback);
1012+
$allOpeningHoursAlwaysClosed = $this->every($isAlwaysClosedCallback);
1013+
$noFiltersApplied = $this->filters === [];
1014+
1015+
return $allExceptionsAlwaysClosed && $noFiltersApplied && $allOpeningHoursAlwaysClosed;
10051016
}
10061017

10071018
public function isAlwaysOpen(): bool
10081019
{
1009-
return $this->exceptions === [] && $this->filters === [] && $this->every(
1010-
static fn (OpeningHoursForDay $day) => ((string) $day) === '00:00-24:00',
1011-
);
1020+
$isAlwaysOpenCallback = static fn (OpeningHoursForDay $day) => ((string) $day) === '00:00-24:00';
1021+
$allExceptionsAlwaysOpen = $this->everyExceptions($isAlwaysOpenCallback);
1022+
$allOpeningHoursAlwaysOpen = $this->every($isAlwaysOpenCallback);
1023+
$noFiltersApplied = $this->filters === [];
1024+
1025+
return $allExceptionsAlwaysOpen && $noFiltersApplied && $allOpeningHoursAlwaysOpen;
10121026
}
10131027

10141028
private static function filterHours(array $data, array $excludedKeys): Generator

tests/OpeningHoursSpecificationParserTest.php

+59-1
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,71 @@ public function testCreateFromStructuredData(): void
104104
);
105105
}
106106

107-
public function testEmptySpecs(): void
107+
public function testIsAlwaysClosedWithUndefinedOpeningHoursAndUndefinedExceptions(): void
108108
{
109109
$openingHours = OpeningHours::createFromStructuredData([]);
110110

111111
$this->assertTrue($openingHours->isAlwaysClosed());
112112
}
113113

114+
public function testIsAlwaysClosedWithEmptyOpeningHoursAndEmptyExceptions(): void
115+
{
116+
$openingHours = OpeningHours::create([
117+
'Monday' => [],
118+
'Tuesday' => [],
119+
'Wednesday' => [],
120+
'Thursday' => [],
121+
'Friday' => [],
122+
'Saturday' => [],
123+
'Sunday' => [],
124+
'exceptions' => [
125+
'2023-12-24' => [],
126+
],
127+
]);
128+
129+
$this->assertTrue($openingHours->isAlwaysClosed());
130+
}
131+
132+
public function testIsAlwaysOpenWithUndefinedOpeningHoursAndUndefinedExceptions(): void
133+
{
134+
$openingHours = OpeningHours::createFromStructuredData([]);
135+
136+
$this->assertFalse($openingHours->isAlwaysOpen());
137+
}
138+
139+
public function testIsAlwaysOpenWithFilledOpeningHoursAndFilledExceptions(): void
140+
{
141+
$openingHours = OpeningHours::create([
142+
'Monday' => ['00:00-24:00'],
143+
'Tuesday' => ['00:00-24:00'],
144+
'Wednesday' => ['00:00-24:00'],
145+
'Thursday' => ['00:00-24:00'],
146+
'Friday' => ['00:00-24:00'],
147+
'Saturday' => ['00:00-24:00'],
148+
'Sunday' => ['00:00-24:00'],
149+
'exceptions' => [
150+
'2023-12-24' => ['00:00-24:00'],
151+
],
152+
]);
153+
154+
$this->assertTrue($openingHours->isAlwaysOpen());
155+
}
156+
157+
public function testIsAlwaysOpenWithFilledOpeningHoursAndUndefinedExceptions()
158+
{
159+
$openingHours = OpeningHours::create([
160+
'Monday' => ['00:00-24:00'],
161+
'Tuesday' => ['00:00-24:00'],
162+
'Wednesday' => ['00:00-24:00'],
163+
'Thursday' => ['00:00-24:00'],
164+
'Friday' => ['00:00-24:00'],
165+
'Saturday' => ['00:00-24:00'],
166+
'Sunday' => ['00:00-24:00'],
167+
]);
168+
169+
$this->assertTrue($openingHours->isAlwaysOpen());
170+
}
171+
114172
public function testRangeOverNight(): void
115173
{
116174
$openingHours = OpeningHours::createFromStructuredData([

0 commit comments

Comments
 (0)