|
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\Calendar; |
|
14
|
|
|
use CultuurNet\UDB3\CalendarInterface; |
|
15
|
|
|
use CultuurNet\UDB3\CalendarType; |
|
16
|
|
|
use DateTimeInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class CdbEncoder |
|
20
|
|
|
* |
|
21
|
|
|
* Encodes an UDB3 calendar or serialized calendar data as a cdb calendar. |
|
22
|
|
|
* This encoder is set up to be compatible with the Symfony serializer component. |
|
23
|
|
|
* |
|
24
|
|
|
* @package CultuurNet\UDB3\Calendar |
|
25
|
|
|
*/ |
|
26
|
|
|
class CdbEncoder |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @param CalendarInterface|array $data |
|
30
|
|
|
* @param $format |
|
31
|
|
|
* @param array $context |
|
32
|
|
|
* |
|
33
|
|
|
* @return \CultureFeed_Cdb_Data_Calendar |
|
34
|
|
|
*/ |
|
35
|
|
|
public function encode($data, $format, array $context = array()) |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
$calendar = $data instanceof CalendarInterface ? $data : Calendar::deserialize($data); |
|
38
|
|
|
|
|
39
|
|
|
$weekscheme = $this->getWeekscheme($calendar); |
|
40
|
|
|
|
|
41
|
|
|
switch ($calendar->getType()->toNative()) { |
|
42
|
|
|
case CalendarType::MULTIPLE: |
|
43
|
|
|
$cdbCalendar = new CultureFeed_Cdb_Data_Calendar_TimestampList(); |
|
44
|
|
|
foreach ($calendar->getTimestamps() as $timestamp) { |
|
45
|
|
|
$this->timestampCalendar( |
|
46
|
|
|
$timestamp->getStartDate(), |
|
47
|
|
|
$timestamp->getEndDate(), |
|
48
|
|
|
$cdbCalendar |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
break; |
|
52
|
|
|
case CalendarType::SINGLE: |
|
53
|
|
|
$cdbCalendar = $this->timestampCalendar( |
|
54
|
|
|
$calendar->getStartDate(), |
|
55
|
|
|
$calendar->getEndDate(), |
|
56
|
|
|
new CultureFeed_Cdb_Data_Calendar_TimestampList() |
|
57
|
|
|
); |
|
58
|
|
|
break; |
|
59
|
|
|
case CalendarType::PERIODIC: |
|
60
|
|
|
$cdbCalendar = new CultureFeed_Cdb_Data_Calendar_PeriodList(); |
|
61
|
|
|
$startDate = $calendar->getStartDate()->format('Y-m-d'); |
|
62
|
|
|
$endDate = $calendar->getEndDate()->format('Y-m-d'); |
|
63
|
|
|
|
|
64
|
|
|
$period = new CultureFeed_Cdb_Data_Calendar_Period($startDate, $endDate); |
|
65
|
|
|
if (!empty($weekscheme) && !empty($weekscheme->getDays())) { |
|
66
|
|
|
$period->setWeekScheme($weekscheme); |
|
67
|
|
|
} |
|
68
|
|
|
$cdbCalendar->add($period); |
|
69
|
|
|
break; |
|
70
|
|
|
case CalendarType::PERMANENT: |
|
71
|
|
|
$cdbCalendar = new CultureFeed_Cdb_Data_Calendar_Permanent(); |
|
72
|
|
|
if (!empty($weekscheme)) { |
|
73
|
|
|
$cdbCalendar->setWeekScheme($weekscheme); |
|
74
|
|
|
} |
|
75
|
|
|
break; |
|
76
|
|
|
default: |
|
77
|
|
|
$cdbCalendar = new CultureFeed_Cdb_Data_Calendar_Permanent(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $cdbCalendar; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function supportsEncoding($format) |
|
84
|
|
|
{ |
|
85
|
|
|
return 'cbd' === $format; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param \CultuurNet\UDB3\CalendarInterface $itemCalendar |
|
90
|
|
|
* @return CultureFeed_Cdb_Data_Calendar_Weekscheme|null |
|
91
|
|
|
* @throws \Exception |
|
92
|
|
|
*/ |
|
93
|
|
|
private function getWeekscheme(CalendarInterface $itemCalendar) |
|
94
|
|
|
{ |
|
95
|
|
|
// Store opening hours. |
|
96
|
|
|
$openingHours = $itemCalendar->getOpeningHours(); |
|
97
|
|
|
$weekscheme = null; |
|
98
|
|
|
|
|
99
|
|
|
if (!empty($openingHours)) { |
|
100
|
|
|
$weekscheme = new CultureFeed_Cdb_Data_Calendar_Weekscheme(); |
|
101
|
|
|
|
|
102
|
|
|
// Multiple opening times can happen on same day. Store them in array. |
|
103
|
|
|
$openingTimesPerDay = array( |
|
104
|
|
|
'monday' => array(), |
|
105
|
|
|
'tuesday' => array(), |
|
106
|
|
|
'wednesday' => array(), |
|
107
|
|
|
'thursday' => array(), |
|
108
|
|
|
'friday' => array(), |
|
109
|
|
|
'saturday' => array(), |
|
110
|
|
|
'sunday' => array(), |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
|
|
foreach ($openingHours as $openingHour) { |
|
114
|
|
|
// In CDB2 every day needs to be a seperate entry. |
|
115
|
|
|
if (is_array($openingHour)) { |
|
116
|
|
|
$openingHour = (object) $openingHour; |
|
117
|
|
|
} |
|
118
|
|
|
foreach ($openingHour->getDayOfWeekCollection()->getDaysOfWeek() as $day) { |
|
119
|
|
|
$openingTimesPerDay[$day->toNative()][] = new CultureFeed_Cdb_Data_Calendar_OpeningTime( |
|
120
|
|
|
$openingHour->getOpens()->toNativeString() . ':00', |
|
121
|
|
|
$openingHour->getCloses()->toNativeString() . ':00' |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// Create the opening times correctly |
|
127
|
|
|
foreach ($openingTimesPerDay as $day => $openingTimes) { |
|
128
|
|
|
// Empty == closed. |
|
129
|
|
|
if (empty($openingTimes)) { |
|
130
|
|
|
$openingInfo = new CultureFeed_Cdb_Data_Calendar_SchemeDay( |
|
131
|
|
|
$day, |
|
132
|
|
|
CultureFeed_Cdb_Data_Calendar_SchemeDay::SCHEMEDAY_OPEN_TYPE_CLOSED |
|
133
|
|
|
); |
|
134
|
|
|
} else { |
|
135
|
|
|
// Add all opening times. |
|
136
|
|
|
$openingInfo = new CultureFeed_Cdb_Data_Calendar_SchemeDay( |
|
137
|
|
|
$day, |
|
138
|
|
|
CultureFeed_Cdb_Data_Calendar_SchemeDay::SCHEMEDAY_OPEN_TYPE_OPEN |
|
139
|
|
|
); |
|
140
|
|
|
foreach ($openingTimes as $openingTime) { |
|
141
|
|
|
$openingInfo->addOpeningTime($openingTime); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
$weekscheme->setDay($day, $openingInfo); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $weekscheme; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param DateTimeInterface $startDate |
|
153
|
|
|
* @param DateTimeInterface $endDate |
|
154
|
|
|
* @param CultureFeed_Cdb_Data_Calendar_TimestampList $calendar |
|
155
|
|
|
* |
|
156
|
|
|
* @return CultureFeed_Cdb_Data_Calendar_TimestampList |
|
157
|
|
|
*/ |
|
158
|
|
|
private function timestampCalendar( |
|
159
|
|
|
DateTimeInterface $startDate, |
|
160
|
|
|
DateTimeInterface $endDate, |
|
161
|
|
|
CultureFeed_Cdb_Data_Calendar_TimestampList $calendar |
|
162
|
|
|
) { |
|
163
|
|
|
$startHour = $startDate->format('H:i:s'); |
|
164
|
|
|
if ($startHour == '00:00:00') { |
|
165
|
|
|
$startHour = null; |
|
166
|
|
|
} |
|
167
|
|
|
$endHour = $endDate->format('H:i:s'); |
|
168
|
|
|
if ($endHour == '00:00:00') { |
|
169
|
|
|
$endHour = null; |
|
170
|
|
|
} |
|
171
|
|
|
$calendar->add( |
|
172
|
|
|
new CultureFeed_Cdb_Data_Calendar_Timestamp( |
|
173
|
|
|
$startDate->format('Y-m-d'), |
|
174
|
|
|
$startHour, |
|
175
|
|
|
$endHour |
|
176
|
|
|
) |
|
177
|
|
|
); |
|
178
|
|
|
|
|
179
|
|
|
return $calendar; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.