1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Speicher210\BusinessHours; |
6
|
|
|
|
7
|
|
|
use Speicher210\BusinessHours\Day\Day; |
8
|
|
|
use Speicher210\BusinessHours\Day\DayBuilder; |
9
|
|
|
use Speicher210\BusinessHours\Day\DayInterface; |
10
|
|
|
use Speicher210\BusinessHours\Day\Time\TimeBuilder; |
11
|
|
|
use Speicher210\BusinessHours\Day\Time\TimeInterval; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Build a BusinessHours concrete implementation. |
15
|
|
|
*/ |
16
|
|
|
final class BusinessHoursBuilder |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Build a BusinessHours from an array. |
20
|
|
|
* |
21
|
|
|
* @param array $data The business hours data. |
22
|
12 |
|
* @return BusinessHours |
23
|
|
|
*/ |
24
|
12 |
|
public static function fromAssociativeArray(array $data): BusinessHours |
25
|
9 |
|
{ |
26
|
|
|
if (!isset($data['days'], $data['timezone']) || !\is_array($data['days'])) { |
27
|
|
|
throw new \InvalidArgumentException('Array is not valid.'); |
28
|
3 |
|
} |
29
|
3 |
|
|
30
|
3 |
|
$days = []; |
31
|
3 |
|
foreach ($data['days'] as $day) { |
32
|
|
|
$days[] = DayBuilder::fromAssociativeArray($day); |
33
|
3 |
|
} |
34
|
|
|
|
35
|
|
|
return new BusinessHours($days, new \DateTimeZone($data['timezone'])); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Create a new BusinessHours with a different timezone from an existing BusinessHours. |
40
|
|
|
* |
41
|
|
|
* @param BusinessHours $businessHours The original business hours. |
42
|
|
|
* @param \DateTimeZone $newTimezone The new timezone. |
43
|
9 |
|
* @return BusinessHours |
44
|
|
|
*/ |
45
|
9 |
|
public static function shiftToTimezone(BusinessHours $businessHours, \DateTimeZone $newTimezone): BusinessHours |
46
|
9 |
|
{ |
47
|
9 |
|
$now = new \DateTime('now'); |
48
|
|
|
$oldTimezone = $businessHours->getTimezone(); |
49
|
9 |
|
$offset = $newTimezone->getOffset($now) - $oldTimezone->getOffset($now); |
50
|
3 |
|
|
51
|
|
|
if ($offset === 0) { |
52
|
|
|
return clone $businessHours; |
53
|
6 |
|
} |
54
|
6 |
|
|
55
|
6 |
|
$tmpDays = \array_fill_keys(Day::getDaysOfWeek(), []); |
56
|
6 |
|
foreach ($businessHours->getDays() as $day) { |
57
|
6 |
|
foreach ($day->getOpeningHoursIntervals() as $interval) { |
58
|
|
|
$start = $interval->getStart()->toSeconds() + $offset; |
59
|
|
|
$end = $interval->getEnd()->toSeconds() + $offset; |
60
|
6 |
|
|
61
|
6 |
|
// Current day. |
62
|
6 |
View Code Duplication |
if ($start < 86400 && $end > 0) { |
|
|
|
|
63
|
|
|
$startForCurrentDay = \max($start, 0); |
64
|
9 |
|
$endForCurrentDay = \min($end, 86400); |
65
|
6 |
|
|
66
|
6 |
|
$dayOfWeek = $day->getDayOfWeek(); |
67
|
6 |
|
$interval = new TimeInterval( |
68
|
|
|
TimeBuilder::fromSeconds($startForCurrentDay), |
69
|
|
|
TimeBuilder::fromSeconds($endForCurrentDay) |
70
|
6 |
|
); |
71
|
3 |
|
$tmpDays[$dayOfWeek][] = $interval; |
72
|
3 |
|
} |
73
|
|
|
|
74
|
3 |
|
// Previous day. |
75
|
3 |
View Code Duplication |
if ($start < 0) { |
|
|
|
|
76
|
3 |
|
$startForPreviousDay = 86400 + $start; |
77
|
3 |
|
$endForPreviousDay = \min(86400, 86400 + $end); |
78
|
|
|
|
79
|
|
|
$dayOfWeek = self::getPreviousDayOfWeek($day->getDayOfWeek()); |
80
|
6 |
|
$interval = new TimeInterval( |
81
|
3 |
|
TimeBuilder::fromSeconds($startForPreviousDay), |
82
|
3 |
|
TimeBuilder::fromSeconds($endForPreviousDay) |
83
|
|
|
); |
84
|
3 |
|
$tmpDays[$dayOfWeek][] = $interval; |
85
|
3 |
|
} |
86
|
3 |
|
|
87
|
3 |
|
// Next day. |
88
|
6 |
View Code Duplication |
if ($end > 86400) { |
|
|
|
|
89
|
6 |
|
$startForNextDay = \max(0, $start - 86400); |
90
|
|
|
$endForNextDay = $end - 86400; |
91
|
6 |
|
|
92
|
6 |
|
$dayOfWeek = self::getNextDayOfWeek($day->getDayOfWeek()); |
93
|
|
|
$interval = new TimeInterval( |
94
|
6 |
|
TimeBuilder::fromSeconds($startForNextDay), |
95
|
|
|
TimeBuilder::fromSeconds($endForNextDay) |
96
|
|
|
); |
97
|
|
|
$tmpDays[$dayOfWeek][] = $interval; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$tmpDays = \array_filter($tmpDays); |
103
|
6 |
|
$days = self::flattenDaysIntervals($tmpDays); |
104
|
|
|
|
105
|
6 |
|
return new BusinessHours($days, $newTimezone); |
106
|
|
|
} |
107
|
6 |
|
|
108
|
6 |
|
/** |
109
|
6 |
|
* Flatten days intervals. |
110
|
6 |
|
* |
111
|
|
|
* @param array $days The days to flatten. |
112
|
6 |
|
* @return DayInterface[] |
113
|
|
|
*/ |
114
|
|
|
private static function flattenDaysIntervals(array $days): array |
115
|
|
|
{ |
116
|
|
|
\ksort($days); |
117
|
|
|
|
118
|
|
|
$flattenDays = []; |
119
|
|
|
foreach ($days as $dayOfWeek => $intervals) { |
120
|
|
|
$flattenDays[] = DayBuilder::fromArray($dayOfWeek, $intervals); |
121
|
3 |
|
} |
122
|
|
|
|
123
|
3 |
|
return $flattenDays; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get previous day of week for a given day of week. |
128
|
|
|
* |
129
|
|
|
* @param integer $dayOfWeek The day of week. |
130
|
|
|
* @return integer |
131
|
|
|
*/ |
132
|
3 |
|
private static function getPreviousDayOfWeek($dayOfWeek): int |
133
|
|
|
{ |
134
|
3 |
|
return DayInterface::WEEK_DAY_MONDAY === $dayOfWeek ? DayInterface::WEEK_DAY_SUNDAY : --$dayOfWeek; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get next day of week for a given day of week. |
139
|
|
|
* |
140
|
|
|
* @param integer $dayOfWeek The day of week. |
141
|
|
|
* @return integer |
142
|
|
|
*/ |
143
|
|
|
private static function getNextDayOfWeek($dayOfWeek): int |
144
|
|
|
{ |
145
|
|
|
return DayInterface::WEEK_DAY_SUNDAY === $dayOfWeek ? DayInterface::WEEK_DAY_MONDAY : ++$dayOfWeek; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.