1 | <?php |
||
19 | class CalendarConverter implements CalendarConverterInterface |
||
20 | { |
||
21 | /** |
||
22 | * @inheritdoc |
||
23 | */ |
||
24 | public function toCdbCalendar(CalendarInterface $calendar) |
||
25 | { |
||
26 | $weekScheme = $this->getWeekScheme($calendar); |
||
27 | $calendarType = (string) $calendar->getType(); |
||
28 | |||
29 | switch ($calendarType) { |
||
30 | case CalendarType::MULTIPLE: |
||
31 | $cdbCalendar = new CultureFeed_Cdb_Data_Calendar_TimestampList(); |
||
32 | $index = 1; |
||
33 | foreach ($calendar->getTimestamps() as $timestamp) { |
||
34 | $currentCount = $this->countTimestamps($cdbCalendar); |
||
35 | $cdbCalendar = $this->createTimestampCalendar( |
||
36 | $timestamp->getStartDate(), |
||
37 | $timestamp->getEndDate(), |
||
38 | $cdbCalendar, |
||
39 | $index |
||
40 | ); |
||
41 | $newCount = $this->countTimestamps($cdbCalendar); |
||
42 | if ($currentCount - $newCount !== -1) { |
||
43 | $index++; |
||
44 | } |
||
45 | } |
||
46 | break; |
||
47 | case CalendarType::SINGLE: |
||
48 | $cdbCalendar = $this->createTimestampCalendar( |
||
49 | $calendar->getStartDate(), |
||
50 | $calendar->getEndDate(), |
||
51 | new CultureFeed_Cdb_Data_Calendar_TimestampList(), |
||
52 | 1 |
||
53 | ); |
||
54 | break; |
||
55 | case CalendarType::PERIODIC: |
||
56 | $cdbCalendar = new CultureFeed_Cdb_Data_Calendar_PeriodList(); |
||
57 | $startDate = $calendar->getStartDate()->format('Y-m-d'); |
||
58 | $endDate = $calendar->getEndDate()->format('Y-m-d'); |
||
59 | |||
60 | $period = new CultureFeed_Cdb_Data_Calendar_Period($startDate, $endDate); |
||
61 | if (!empty($weekScheme) && !empty($weekScheme->getDays())) { |
||
62 | $period->setWeekScheme($weekScheme); |
||
63 | } |
||
64 | $cdbCalendar->add($period); |
||
65 | break; |
||
66 | case CalendarType::PERMANENT: |
||
67 | $cdbCalendar = new CultureFeed_Cdb_Data_Calendar_Permanent(); |
||
68 | if (!empty($weekScheme)) { |
||
69 | $cdbCalendar->setWeekScheme($weekScheme); |
||
70 | } |
||
71 | break; |
||
72 | default: |
||
73 | $cdbCalendar = new CultureFeed_Cdb_Data_Calendar_Permanent(); |
||
74 | } |
||
75 | |||
76 | return $cdbCalendar; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param CultureFeed_Cdb_Data_Calendar_TimestampList $timestamps |
||
81 | * @return int |
||
82 | */ |
||
83 | private function countTimestamps(CultureFeed_Cdb_Data_Calendar_TimestampList $timestamps) |
||
90 | |||
91 | /** |
||
92 | * @param \CultuurNet\UDB3\CalendarInterface $itemCalendar |
||
93 | * @return CultureFeed_Cdb_Data_Calendar_Weekscheme|null |
||
94 | * @throws \Exception |
||
95 | */ |
||
96 | private function getWeekScheme(CalendarInterface $itemCalendar) |
||
153 | |||
154 | /** |
||
155 | * @param DateTimeInterface $startDate |
||
156 | * @param DateTimeInterface $endDate |
||
157 | * @param CultureFeed_Cdb_Data_Calendar_TimestampList $calendar |
||
158 | * @param Integer|null $index |
||
159 | * |
||
160 | * @return CultureFeed_Cdb_Data_Calendar_TimestampList |
||
161 | */ |
||
162 | private function createTimestampCalendar( |
||
163 | DateTimeInterface $startDate, |
||
164 | DateTimeInterface $endDate, |
||
165 | CultureFeed_Cdb_Data_Calendar_TimestampList $calendar, |
||
166 | $index = null |
||
167 | ) { |
||
168 | // Make a clone of the original calendar to avoid updating input param. |
||
169 | $newCalendar = clone $calendar; |
||
170 | |||
171 | $startDay = Period::createFromDay($startDate); |
||
172 | $untilEndOfNextDay = $startDay |
||
173 | ->withDuration('2 DAYS') |
||
174 | ->moveEndDate('-1 SECOND'); |
||
175 | |||
176 | // Easy case an no seconds needed for indexing. |
||
177 | if ($untilEndOfNextDay->contains($endDate)) { |
||
178 | $newCalendar->add( |
||
179 | new CultureFeed_Cdb_Data_Calendar_Timestamp( |
||
180 | $startDate->format('Y-m-d'), |
||
181 | $this->formatDateTimeAsCdbTime($startDate), |
||
182 | $this->formatDateTimeAsCdbTime($endDate) |
||
183 | ) |
||
184 | ); |
||
185 | } else if (is_int($index)) { |
||
186 | // Complex case and seconds needed for indexing. |
||
187 | $period = new Period($startDate, $endDate); |
||
188 | |||
189 | $startTimestamp = new CultureFeed_Cdb_Data_Calendar_Timestamp( |
||
190 | $startDate->format('Y-m-d'), |
||
191 | $this->formatDateTimeAsCdbTime($startDate, $index) |
||
192 | ); |
||
193 | |||
194 | $endTimestamp = new CultureFeed_Cdb_Data_Calendar_Timestamp( |
||
195 | $endDate->format('Y-m-d'), |
||
196 | $this->createIndexedTimeString($index), |
||
197 | $this->formatDateTimeAsCdbTime($endDate) |
||
198 | ); |
||
199 | |||
200 | $days = iterator_to_array($period->getDatePeriod('1 DAY')); |
||
201 | $fillerTimestamps = array_map( |
||
202 | function (DateTimeInterface $dateTime) use ($index) { |
||
203 | return new CultureFeed_Cdb_Data_Calendar_Timestamp( |
||
204 | $dateTime->format('Y-m-d'), |
||
205 | $this->createIndexedTimeString($index) |
||
206 | ); |
||
207 | }, |
||
208 | array_slice($days, 1, count($days) === 2 ? 2 : -1) |
||
209 | ); |
||
210 | |||
211 | $newCalendar = array_reduce( |
||
212 | array_merge([$startTimestamp], $fillerTimestamps, [$endTimestamp]), |
||
213 | function (CultureFeed_Cdb_Data_Calendar_TimestampList $calendar, $timestamp) { |
||
214 | $calendar->add($timestamp); |
||
215 | return $calendar; |
||
216 | }, |
||
217 | $calendar |
||
218 | ); |
||
219 | } |
||
220 | |||
221 | return $newCalendar; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * @param DateTimeInterface $timestamp |
||
226 | * @param integer|null $index |
||
227 | * @return null|string |
||
228 | */ |
||
229 | private function formatDateTimeAsCdbTime(DateTimeInterface $timestamp, $index = null) |
||
245 | |||
246 | /** |
||
247 | * @param int $index |
||
248 | * @return string |
||
249 | */ |
||
250 | private function createIndexedTimeString($index) |
||
254 | } |
||
255 |