20
20
use Spatie \OpeningHours \Helpers \DataTrait ;
21
21
use Spatie \OpeningHours \Helpers \DateTimeCopier ;
22
22
use Spatie \OpeningHours \Helpers \DiffTrait ;
23
- use ValueError ;
24
23
25
24
class OpeningHours
26
25
{
@@ -128,9 +127,15 @@ public static function createFromStructuredData(
128
127
string |DateTimeZone |null $ timezone = null ,
129
128
string |DateTimeZone |null $ outputTimezone = null ,
130
129
): self {
131
- $ parser = new OpeningHoursSpecificationParser ($ structuredData );
132
-
133
- return new static ($ parser ->getOpeningHours (), $ timezone , $ outputTimezone );
130
+ return new static (
131
+ array_merge (
132
+ // https://schema.org/OpeningHoursSpecification allows overflow by default
133
+ ['overflow ' => true ],
134
+ OpeningHoursSpecificationParser::create ($ structuredData )->getOpeningHours (),
135
+ ),
136
+ $ timezone ,
137
+ $ outputTimezone ,
138
+ );
134
139
}
135
140
136
141
/**
@@ -213,7 +218,7 @@ public static function isValid(array $data): bool
213
218
static ::create ($ data );
214
219
215
220
return true ;
216
- } catch (Exception | ValueError ) {
221
+ } catch (Exception ) {
217
222
return false ;
218
223
}
219
224
}
@@ -891,6 +896,11 @@ protected function getDateWithTimezone(DateTimeInterface $date, ?DateTimeZone $t
891
896
return $ date ;
892
897
}
893
898
899
+ /**
900
+ * Returns opening hours for the days that match a given condition as an array.
901
+ *
902
+ * @return OpeningHoursForDay[]
903
+ */
894
904
public function filter (callable $ callback ): array
895
905
{
896
906
return Arr::filter ($ this ->openingHours , $ callback );
@@ -906,6 +916,11 @@ public function flatMap(callable $callback): array
906
916
return Arr::flatMap ($ this ->openingHours , $ callback );
907
917
}
908
918
919
+ /**
920
+ * Returns opening hours for the exceptions that match a given condition as an array.
921
+ *
922
+ * @return OpeningHoursForDay[]
923
+ */
909
924
public function filterExceptions (callable $ callback ): array
910
925
{
911
926
return Arr::filter ($ this ->exceptions , $ callback );
@@ -921,6 +936,14 @@ public function flatMapExceptions(callable $callback): array
921
936
return Arr::flatMap ($ this ->exceptions , $ callback );
922
937
}
923
938
939
+ /** Checks that opening hours for every day of the week matches a given condition */
940
+ public function every (callable $ callback ): bool
941
+ {
942
+ return $ this ->filter (
943
+ static fn (OpeningHoursForDay $ day ) => ! $ callback ($ day ),
944
+ ) === [];
945
+ }
946
+
924
947
public function asStructuredData (
925
948
string $ format = TimeDataContainer::TIME_FORMAT ,
926
949
DateTimeZone |string |null $ timezone = null ,
@@ -965,6 +988,20 @@ static function (OpeningHoursForDay $openingHoursForDay, string $date) use ($for
965
988
return array_merge ($ regularHours , $ exceptions );
966
989
}
967
990
991
+ public function isAlwaysClosed (): bool
992
+ {
993
+ return $ this ->exceptions === [] && $ this ->filters === [] && $ this ->every (
994
+ static fn (OpeningHoursForDay $ day ) => $ day ->isEmpty (),
995
+ );
996
+ }
997
+
998
+ public function isAlwaysOpen (): bool
999
+ {
1000
+ return $ this ->exceptions === [] && $ this ->filters === [] && $ this ->every (
1001
+ static fn (OpeningHoursForDay $ day ) => ((string ) $ day ) === '00:00-24:00 ' ,
1002
+ );
1003
+ }
1004
+
968
1005
private static function filterHours (array $ data , array $ excludedKeys ): Generator
969
1006
{
970
1007
foreach ($ data as $ key => $ value ) {
0 commit comments