1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Calendar; |
4
|
|
|
|
5
|
|
|
use CultureFeed_Cdb_Data_Calendar_OpeningTime; |
6
|
|
|
use CultureFeed_Cdb_Data_Calendar_Period; |
7
|
|
|
use CultureFeed_Cdb_Data_Calendar_PeriodList; |
8
|
|
|
use CultureFeed_Cdb_Data_Calendar_Permanent; |
9
|
|
|
use CultureFeed_Cdb_Data_Calendar_SchemeDay; |
10
|
|
|
use CultureFeed_Cdb_Data_Calendar_Timestamp; |
11
|
|
|
use CultureFeed_Cdb_Data_Calendar_TimestampList; |
12
|
|
|
use CultureFeed_Cdb_Data_Calendar_Weekscheme; |
13
|
|
|
use CultuurNet\UDB3\CalendarInterface; |
14
|
|
|
use CultuurNet\UDB3\CalendarType; |
15
|
|
|
use DateTimeInterface; |
16
|
|
|
|
17
|
|
|
class CalendarConverter implements CalendarConverterInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @inheritdoc |
21
|
|
|
*/ |
22
|
|
|
public function toCdbCalendar(CalendarInterface $calendar) |
23
|
|
|
{ |
24
|
|
|
$weekScheme = $this->getWeekScheme($calendar); |
25
|
|
|
$calendarType = (string) $calendar->getType(); |
26
|
|
|
|
27
|
|
|
switch ($calendarType) { |
28
|
|
|
case CalendarType::MULTIPLE: |
29
|
|
|
$cdbCalendar = new CultureFeed_Cdb_Data_Calendar_TimestampList(); |
30
|
|
|
foreach ($calendar->getTimestamps() as $timestamp) { |
31
|
|
|
$this->timestampCalendar( |
32
|
|
|
$timestamp->getStartDate(), |
33
|
|
|
$timestamp->getEndDate(), |
34
|
|
|
$cdbCalendar |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
break; |
38
|
|
|
case CalendarType::SINGLE: |
39
|
|
|
$cdbCalendar = $this->timestampCalendar( |
40
|
|
|
$calendar->getStartDate(), |
41
|
|
|
$calendar->getEndDate(), |
42
|
|
|
new CultureFeed_Cdb_Data_Calendar_TimestampList() |
43
|
|
|
); |
44
|
|
|
break; |
45
|
|
|
case CalendarType::PERIODIC: |
46
|
|
|
$cdbCalendar = new CultureFeed_Cdb_Data_Calendar_PeriodList(); |
47
|
|
|
$startDate = $calendar->getStartDate()->format('Y-m-d'); |
48
|
|
|
$endDate = $calendar->getEndDate()->format('Y-m-d'); |
49
|
|
|
|
50
|
|
|
$period = new CultureFeed_Cdb_Data_Calendar_Period($startDate, $endDate); |
51
|
|
|
if (!empty($weekScheme) && !empty($weekScheme->getDays())) { |
52
|
|
|
$period->setWeekScheme($weekScheme); |
53
|
|
|
} |
54
|
|
|
$cdbCalendar->add($period); |
55
|
|
|
break; |
56
|
|
|
case CalendarType::PERMANENT: |
57
|
|
|
$cdbCalendar = new CultureFeed_Cdb_Data_Calendar_Permanent(); |
58
|
|
|
if (!empty($weekScheme)) { |
59
|
|
|
$cdbCalendar->setWeekScheme($weekScheme); |
60
|
|
|
} |
61
|
|
|
break; |
62
|
|
|
default: |
63
|
|
|
$cdbCalendar = new CultureFeed_Cdb_Data_Calendar_Permanent(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $cdbCalendar; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param \CultuurNet\UDB3\CalendarInterface $itemCalendar |
71
|
|
|
* @return CultureFeed_Cdb_Data_Calendar_Weekscheme|null |
72
|
|
|
* @throws \Exception |
73
|
|
|
*/ |
74
|
|
|
private function getWeekScheme(CalendarInterface $itemCalendar) |
75
|
|
|
{ |
76
|
|
|
// Store opening hours. |
77
|
|
|
$openingHours = $itemCalendar->getOpeningHours(); |
78
|
|
|
$weekScheme = null; |
79
|
|
|
|
80
|
|
|
if (!empty($openingHours)) { |
81
|
|
|
$weekScheme = new CultureFeed_Cdb_Data_Calendar_Weekscheme(); |
82
|
|
|
|
83
|
|
|
// Multiple opening times can happen on same day. Store them in array. |
84
|
|
|
$openingTimesPerDay = array( |
85
|
|
|
'monday' => array(), |
86
|
|
|
'tuesday' => array(), |
87
|
|
|
'wednesday' => array(), |
88
|
|
|
'thursday' => array(), |
89
|
|
|
'friday' => array(), |
90
|
|
|
'saturday' => array(), |
91
|
|
|
'sunday' => array(), |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
foreach ($openingHours as $openingHour) { |
95
|
|
|
// In CDB2 every day needs to be a seperate entry. |
96
|
|
|
if (is_array($openingHour)) { |
97
|
|
|
$openingHour = (object) $openingHour; |
98
|
|
|
} |
99
|
|
|
foreach ($openingHour->getDayOfWeekCollection()->getDaysOfWeek() as $day) { |
100
|
|
|
$openingTimesPerDay[$day->toNative()][] = new CultureFeed_Cdb_Data_Calendar_OpeningTime( |
101
|
|
|
$openingHour->getOpens()->toNativeString() . ':00', |
102
|
|
|
$openingHour->getCloses()->toNativeString() . ':00' |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// Create the opening times correctly |
108
|
|
|
foreach ($openingTimesPerDay as $day => $openingTimes) { |
109
|
|
|
// Empty == closed. |
110
|
|
|
if (empty($openingTimes)) { |
111
|
|
|
$openingInfo = new CultureFeed_Cdb_Data_Calendar_SchemeDay( |
112
|
|
|
$day, |
113
|
|
|
CultureFeed_Cdb_Data_Calendar_SchemeDay::SCHEMEDAY_OPEN_TYPE_CLOSED |
114
|
|
|
); |
115
|
|
|
} else { |
116
|
|
|
// Add all opening times. |
117
|
|
|
$openingInfo = new CultureFeed_Cdb_Data_Calendar_SchemeDay( |
118
|
|
|
$day, |
119
|
|
|
CultureFeed_Cdb_Data_Calendar_SchemeDay::SCHEMEDAY_OPEN_TYPE_OPEN |
120
|
|
|
); |
121
|
|
|
foreach ($openingTimes as $openingTime) { |
122
|
|
|
$openingInfo->addOpeningTime($openingTime); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
$weekScheme->setDay($day, $openingInfo); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $weekScheme; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param DateTimeInterface $startDate |
134
|
|
|
* @param DateTimeInterface $endDate |
135
|
|
|
* @param CultureFeed_Cdb_Data_Calendar_TimestampList $calendar |
136
|
|
|
* |
137
|
|
|
* @return CultureFeed_Cdb_Data_Calendar_TimestampList |
138
|
|
|
*/ |
139
|
|
|
private function timestampCalendar( |
140
|
|
|
DateTimeInterface $startDate, |
141
|
|
|
DateTimeInterface $endDate, |
142
|
|
|
CultureFeed_Cdb_Data_Calendar_TimestampList $calendar |
143
|
|
|
) { |
144
|
|
|
$startHour = $startDate->format('H:i:s'); |
145
|
|
|
if ($startHour == '00:00:00') { |
146
|
|
|
$startHour = null; |
147
|
|
|
} |
148
|
|
|
$endHour = $endDate->format('H:i:s'); |
149
|
|
|
if ($endHour == '00:00:00') { |
150
|
|
|
$endHour = null; |
151
|
|
|
} |
152
|
|
|
$calendar->add( |
153
|
|
|
new CultureFeed_Cdb_Data_Calendar_Timestamp( |
154
|
|
|
$startDate->format('Y-m-d'), |
155
|
|
|
$startHour, |
156
|
|
|
$endHour |
157
|
|
|
) |
158
|
|
|
); |
159
|
|
|
|
160
|
|
|
return $calendar; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|