From 45999a6ae91c8db973dce3ffe63605498322baad Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Sat, 25 Nov 2023 14:49:15 +0100 Subject: [PATCH] Code style normalization --- src/OpeningHoursSpecificationParser.php | 58 ++++----- tests/OpeningHoursSpecificationParserTest.php | 114 +++++++++--------- 2 files changed, 88 insertions(+), 84 deletions(-) diff --git a/src/OpeningHoursSpecificationParser.php b/src/OpeningHoursSpecificationParser.php index 4d33246..5e6fc78 100644 --- a/src/OpeningHoursSpecificationParser.php +++ b/src/OpeningHoursSpecificationParser.php @@ -4,6 +4,7 @@ namespace Spatie\OpeningHours; +use JsonException; use Spatie\OpeningHours\Exceptions\InvalidOpeningHoursSpecification; final class OpeningHoursSpecificationParser @@ -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', ); } @@ -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']) && @@ -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', + ); } } } @@ -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; } @@ -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; } diff --git a/tests/OpeningHoursSpecificationParserTest.php b/tests/OpeningHoursSpecificationParserTest.php index 3a1dbc4..10fb471 100644 --- a/tests/OpeningHoursSpecificationParserTest.php +++ b/tests/OpeningHoursSpecificationParserTest.php @@ -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); @@ -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', ); } }