Skip to content

Commit

Permalink
doc: Added README entry, support JSON string input
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Nov 23, 2023
1 parent 4e59c42 commit aa8f3b9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,48 @@ if ($date) {
}
```

#### `OpeningHours::createFromStructuredData(array|string $data, $timezone = null, $outputTimezone = null): Spatie\OpeningHours\OpeningHours`

Static factory method to fill the set with a https://schema.org/OpeningHoursSpecification array or JSON string.

`dayOfWeek` supports array of day names (Google-flavored) or array of day URLs (official schema.org specification).

```php
$openingHours = OpeningHours::createFromStructuredData('[
{
"@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": [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday"
]
},
{
"@type": "OpeningHoursSpecification",
"opens": "00:00",
"closes": "00:00",
"validFrom": "2023-12-25",
"validThrough": "2023-12-25"
}
]');
```

#### `OpeningHours::asStructuredData(strinf $format = 'H:i', string|DateTimeZone $timezone) : array`

Returns a [OpeningHoursSpecification](https://schema.org/openingHoursSpecification) as an array.
Expand Down
2 changes: 1 addition & 1 deletion src/OpeningHours.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static function create(
}

public static function createFromStructuredData(
array $structuredData,
array|string $structuredData,
string|DateTimeZone|null $timezone = null,
string|DateTimeZone|null $outputTimezone = null,
): self {
Expand Down
4 changes: 1 addition & 3 deletions tests/OpeningHoursSpecificationParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ public function testCreateFromStructuredData(): void
"@type": "OpeningHoursSpecification",
"opens": "08:00:00",
"closes": "12:00:00",
"dayOfWeek": [
"https://schema.org/Saturday"
]
"dayOfWeek": "https://schema.org/Saturday"
},
{
"@type": "OpeningHoursSpecification",
Expand Down

0 comments on commit aa8f3b9

Please sign in to comment.