Skip to content

Commit

Permalink
Merge pull request #262 from esign/fix/always-open-closed
Browse files Browse the repository at this point in the history
fix: unable to correctly determine isAlwaysClosed and isAlwaysOpen
  • Loading branch information
kylekatarnls authored Nov 27, 2024
2 parents 773ab4f + 630883b commit e7b4a12
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
26 changes: 20 additions & 6 deletions src/OpeningHours.php
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,14 @@ public function filterExceptions(callable $callback): array
return Arr::filter($this->exceptions, $callback);
}

/** Checks that all exceptions match a given condition */
public function everyExceptions(callable $callback): bool
{
return $this->filterExceptions(
static fn (OpeningHoursForDay $day) => ! $callback($day),
) === [];
}

public function mapExceptions(callable $callback): array
{
return Arr::map($this->exceptions, $callback);
Expand Down Expand Up @@ -999,16 +1007,22 @@ static function (OpeningHoursForDay $openingHoursForDay, string $date) use ($for

public function isAlwaysClosed(): bool
{
return $this->exceptions === [] && $this->filters === [] && $this->every(
static fn (OpeningHoursForDay $day) => $day->isEmpty(),
);
$isAlwaysClosedCallback = static fn (OpeningHoursForDay $day) => $day->isEmpty();
$allExceptionsAlwaysClosed = $this->everyExceptions($isAlwaysClosedCallback);
$allOpeningHoursAlwaysClosed = $this->every($isAlwaysClosedCallback);
$noFiltersApplied = $this->filters === [];

return $allExceptionsAlwaysClosed && $noFiltersApplied && $allOpeningHoursAlwaysClosed;
}

public function isAlwaysOpen(): bool
{
return $this->exceptions === [] && $this->filters === [] && $this->every(
static fn (OpeningHoursForDay $day) => ((string) $day) === '00:00-24:00',
);
$isAlwaysOpenCallback = static fn (OpeningHoursForDay $day) => ((string) $day) === '00:00-24:00';
$allExceptionsAlwaysOpen = $this->everyExceptions($isAlwaysOpenCallback);
$allOpeningHoursAlwaysOpen = $this->every($isAlwaysOpenCallback);
$noFiltersApplied = $this->filters === [];

return $allExceptionsAlwaysOpen && $noFiltersApplied && $allOpeningHoursAlwaysOpen;
}

private static function filterHours(array $data, array $excludedKeys): Generator
Expand Down
60 changes: 59 additions & 1 deletion tests/OpeningHoursSpecificationParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,71 @@ public function testCreateFromStructuredData(): void
);
}

public function testEmptySpecs(): void
public function testIsAlwaysClosedWithUndefinedOpeningHoursAndUndefinedExceptions(): void
{
$openingHours = OpeningHours::createFromStructuredData([]);

$this->assertTrue($openingHours->isAlwaysClosed());
}

public function testIsAlwaysClosedWithEmptyOpeningHoursAndEmptyExceptions(): void
{
$openingHours = OpeningHours::create([
'Monday' => [],
'Tuesday' => [],
'Wednesday' => [],
'Thursday' => [],
'Friday' => [],
'Saturday' => [],
'Sunday' => [],
'exceptions' => [
'2023-12-24' => [],
],
]);

$this->assertTrue($openingHours->isAlwaysClosed());
}

public function testIsAlwaysOpenWithUndefinedOpeningHoursAndUndefinedExceptions(): void
{
$openingHours = OpeningHours::createFromStructuredData([]);

$this->assertFalse($openingHours->isAlwaysOpen());
}

public function testIsAlwaysOpenWithFilledOpeningHoursAndFilledExceptions(): void
{
$openingHours = OpeningHours::create([
'Monday' => ['00:00-24:00'],
'Tuesday' => ['00:00-24:00'],
'Wednesday' => ['00:00-24:00'],
'Thursday' => ['00:00-24:00'],
'Friday' => ['00:00-24:00'],
'Saturday' => ['00:00-24:00'],
'Sunday' => ['00:00-24:00'],
'exceptions' => [
'2023-12-24' => ['00:00-24:00'],
],
]);

$this->assertTrue($openingHours->isAlwaysOpen());
}

public function testIsAlwaysOpenWithFilledOpeningHoursAndUndefinedExceptions()
{
$openingHours = OpeningHours::create([
'Monday' => ['00:00-24:00'],
'Tuesday' => ['00:00-24:00'],
'Wednesday' => ['00:00-24:00'],
'Thursday' => ['00:00-24:00'],
'Friday' => ['00:00-24:00'],
'Saturday' => ['00:00-24:00'],
'Sunday' => ['00:00-24:00'],
]);

$this->assertTrue($openingHours->isAlwaysOpen());
}

public function testRangeOverNight(): void
{
$openingHours = OpeningHours::createFromStructuredData([
Expand Down

0 comments on commit e7b4a12

Please sign in to comment.