Skip to content

Commit

Permalink
- Allow overflow for structured-data
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Nov 25, 2023
1 parent d3f3ab7 commit 793b060
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/OpeningHours.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ public static function createFromStructuredData(
string|DateTimeZone|null $outputTimezone = null,
): self {
return new static(
OpeningHoursSpecificationParser::create($structuredData)->getOpeningHours(),
array_merge(
// https://schema.org/OpeningHoursSpecification allows overflow by default
['overflow' => true],
OpeningHoursSpecificationParser::create($structuredData)->getOpeningHours(),
),
$timezone,
$outputTimezone,
);
Expand Down Expand Up @@ -936,7 +940,7 @@ public function flatMapExceptions(callable $callback): array
public function every(callable $callback): bool
{
return $this->filter(
static fn (OpeningHoursForDay $day) => !$callback($day),
static fn (OpeningHoursForDay $day) => ! $callback($day),
) === [];
}

Expand Down
2 changes: 1 addition & 1 deletion src/OpeningHoursSpecificationParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getOpeningHours(): array
}

/**
* Regular opening hours
* Regular opening hours.
*/
private function addDaysOfWeek(
array $dayOfWeek,
Expand Down
17 changes: 17 additions & 0 deletions tests/OpeningHoursSpecificationParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ public function testEmptySpecs(): void
$this->assertTrue($openingHours->isAlwaysClosed());
}

public function testRangeOverNight(): void
{
$openingHours = OpeningHours::createFromStructuredData([
[
'dayOfWeek' => 'Monday',
'opens' => '18:00',
'closes' => '02:00',
],
]);

$this->assertTrue($openingHours->isClosedAt(new DateTimeImmutable('2023-11-27 17:50')));
$this->assertTrue($openingHours->isOpenAt(new DateTimeImmutable('2023-11-27 23:55')));
$this->assertTrue($openingHours->isOpenAt(new DateTimeImmutable('2023-11-27 23:59:59.99')));
$this->assertTrue($openingHours->isOpenAt(new DateTimeImmutable('2023-11-28 01:50')));
$this->assertTrue($openingHours->isClosedAt(new DateTimeImmutable('2023-11-28 19:00')));
}

public function testH24Specs(): void
{
$openingHours = OpeningHours::createFromStructuredData([
Expand Down
2 changes: 1 addition & 1 deletion tests/OpeningHoursTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ public function it_can_determine_that_its_open_now()
/** @test */
public function it_can_use_day_enum()
{
$openingHours = new class () extends OpeningHours
$openingHours = new class extends OpeningHours
{
public readonly array $days;

Expand Down

0 comments on commit 793b060

Please sign in to comment.