Skip to content

Commit

Permalink
Code style normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Nov 25, 2023
1 parent 580d0c2 commit 45999a6
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 84 deletions.
58 changes: 31 additions & 27 deletions src/OpeningHoursSpecificationParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Spatie\OpeningHours;

use JsonException;
use Spatie\OpeningHours\Exceptions\InvalidOpeningHoursSpecification;

final class OpeningHoursSpecificationParser
Expand All @@ -14,22 +15,22 @@ public function __construct(array|string|null $openingHoursSpecification)
{
if (is_string($openingHoursSpecification)) {
try {
$openingHoursSpecification = \json_decode(
$openingHoursSpecification = json_decode(
$openingHoursSpecification,
true,
flags: JSON_THROW_ON_ERROR
flags: JSON_THROW_ON_ERROR,
);
} catch (\JsonException $e) {
} catch (JsonException $e) {
throw new InvalidOpeningHoursSpecification(
'Invalid https://schema.org/OpeningHoursSpecification JSON',
previous: $e
previous: $e,
);
}
}

if (! is_array($openingHoursSpecification) || empty($openingHoursSpecification)) {
if (! is_array($openingHoursSpecification) || $openingHoursSpecification === []) {
throw new InvalidOpeningHoursSpecification(
'Invalid https://schema.org/OpeningHoursSpecification structured data'
'Invalid https://schema.org/OpeningHoursSpecification structured data',
);
}

Expand All @@ -45,17 +46,19 @@ public function __construct(array|string|null $openingHoursSpecification)
$this->addDayOfWeekHours(
$dayOfWeekItem,
$openingHoursSpecificationItem['opens'] ?? null,
$openingHoursSpecificationItem['closes'] ?? null
$openingHoursSpecificationItem['closes'] ?? null,
);
}
} elseif (is_string($dayOfWeek)) {
$this->addDayOfWeekHours(
$dayOfWeek,
$openingHoursSpecificationItem['opens'] ?? null,
$openingHoursSpecificationItem['closes'] ?? null
$openingHoursSpecificationItem['closes'] ?? null,
);
} else {
throw new InvalidOpeningHoursSpecification('Invalid https://schema.org/OpeningHoursSpecification structured data');
throw new InvalidOpeningHoursSpecification(
'Invalid https://schema.org/OpeningHoursSpecification structured data',
);
}
} elseif (
isset($openingHoursSpecificationItem['validFrom']) &&
Expand All @@ -70,10 +73,12 @@ public function __construct(array|string|null $openingHoursSpecification)
$validFrom,
$validThrough,
$openingHoursSpecificationItem['opens'] ?? null,
$openingHoursSpecificationItem['closes'] ?? null
$openingHoursSpecificationItem['closes'] ?? null,
);
} else {
throw new InvalidOpeningHoursSpecification('Invalid https://schema.org/OpeningHoursSpecification structured data');
throw new InvalidOpeningHoursSpecification(
'Invalid https://schema.org/OpeningHoursSpecification structured data',
);
}
}
}
Expand Down Expand Up @@ -101,14 +106,16 @@ private function schemaOrgDayToString(string $schemaOrgDaySpec): string
private function addDayOfWeekHours(
string $dayOfWeek,
?string $opens,
?string $closes
?string $closes,
): void {
$dayOfWeek = self::schemaOrgDayToString($dayOfWeek);

$hours = $this->formatHours($opens, $closes);
if (null === $hours) {

if ($hours === null) {
return;
}

$this->openingHours[$dayOfWeek][] = $hours;
}

Expand All @@ -122,28 +129,25 @@ private function addExceptionsHours(
throw new InvalidOpeningHoursSpecification('Missing validFrom and validThrough dates');
}

if (! preg_match('/^\d{4}-\d{2}-\d{2}$/', $validFrom) || ! preg_match('/^\d{4}-\d{2}-\d{2}$/', $validThrough)) {
if (
! preg_match('/^\d{4}-\d{2}-\d{2}$/', $validFrom) ||
! preg_match('/^\d{4}-\d{2}-\d{2}$/', $validThrough)
) {
throw new InvalidOpeningHoursSpecification('Invalid validFrom and validThrough dates');
}

if ($validFrom === $validThrough) {
$exceptionKey = $validFrom;
} else {
$exceptionKey = $validFrom.' to '.$validThrough;
}
$exceptionKey = $validFrom === $validThrough ? $validFrom : $validFrom.' to '.$validThrough;

if (! isset($this->openingHours['exceptions'])) {
$this->openingHours['exceptions'] = [];
}
if (! isset($this->openingHours['exceptions'][$exceptionKey])) {
// Default to close all day
$this->openingHours['exceptions'][$exceptionKey] = [];
}
$this->openingHours['exceptions'] ??= [];
// Default to close all day
$this->openingHours['exceptions'][$exceptionKey] ??= [];

$hours = $this->formatHours($opens, $closes);
if (null === $hours) {

if ($hours === null) {
return;
}

$this->openingHours['exceptions'][$exceptionKey][] = $hours;
}

Expand Down
114 changes: 57 additions & 57 deletions tests/OpeningHoursSpecificationParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,61 @@ class OpeningHoursSpecificationParserTest extends TestCase
public function testCreateFromStructuredData(): void
{
$openingHoursSpecs = <<<'JSON'
[
{
"@type": "OpeningHoursSpecification",
"opens": "08:00",
"closes": "12:00",
"dayOfWeek": [
"https://schema.org/Monday",
"https://schema.org/Tuesday",
"https://schema.org/Wednesday",
"https://schema.org/Thursday",
"https://schema.org/Friday"
]
},
{
"@type": "OpeningHoursSpecification",
"opens": "14:00",
"closes": "18:00",
"dayOfWeek": [
"https://schema.org/Monday",
"https://schema.org/Tuesday",
"https://schema.org/Wednesday",
"https://schema.org/Thursday",
"https://schema.org/Friday"
]
},
{
"@type": "OpeningHoursSpecification",
"opens": "08:00:00",
"closes": "12:00:00",
"dayOfWeek": "https://schema.org/Saturday"
},
{
"@type": "OpeningHoursSpecification",
"opens": "00:00",
"closes": "00:00",
"dayOfWeek": [
"Sunday"
]
},
{
"@type": "OpeningHoursSpecification",
"opens": "00:00",
"closes": "00:00",
"validFrom": "2023-12-25",
"validThrough": "2023-12-25"
},
{
"@type": "OpeningHoursSpecification",
"opens": "09:00",
"closes": "18:00",
"validFrom": "2023-12-24",
"validThrough": "2023-12-24"
}
]
JSON;
[
{
"@type": "OpeningHoursSpecification",
"opens": "08:00",
"closes": "12:00",
"dayOfWeek": [
"https://schema.org/Monday",
"https://schema.org/Tuesday",
"https://schema.org/Wednesday",
"https://schema.org/Thursday",
"https://schema.org/Friday"
]
},
{
"@type": "OpeningHoursSpecification",
"opens": "14:00",
"closes": "18:00",
"dayOfWeek": [
"https://schema.org/Monday",
"https://schema.org/Tuesday",
"https://schema.org/Wednesday",
"https://schema.org/Thursday",
"https://schema.org/Friday"
]
},
{
"@type": "OpeningHoursSpecification",
"opens": "08:00:00",
"closes": "12:00:00",
"dayOfWeek": "https://schema.org/Saturday"
},
{
"@type": "OpeningHoursSpecification",
"opens": "00:00",
"closes": "00:00",
"dayOfWeek": [
"Sunday"
]
},
{
"@type": "OpeningHoursSpecification",
"opens": "00:00",
"closes": "00:00",
"validFrom": "2023-12-25",
"validThrough": "2023-12-25"
},
{
"@type": "OpeningHoursSpecification",
"opens": "09:00",
"closes": "18:00",
"validFrom": "2023-12-24",
"validThrough": "2023-12-24"
}
]
JSON;

$openingHours = OpeningHours::createFromStructuredData(json_decode($openingHoursSpecs, true));
$this->assertInstanceOf(OpeningHours::class, $openingHours);
Expand All @@ -92,12 +92,12 @@ public function testCreateFromStructuredData(): void
// Exception Closed on Christmas day
$this->assertTrue(
$openingHours->isClosedAt(new \DateTime('2023-12-25 08:00')),
'Closed on 2023 Monday Christmas day'
'Closed on 2023 Monday Christmas day',
);
// Exception Opened on Christmas Eve
$this->assertTrue(
$openingHours->isOpenAt(new \DateTime('2023-12-24 10:00')),
'Opened on 2023 Sunday before Christmas day'
'Opened on 2023 Sunday before Christmas day',
);
}
}

0 comments on commit 45999a6

Please sign in to comment.