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( |
||
223 | |||
224 | /** |
||
225 | * @param DateTimeInterface $timestamp |
||
226 | * @param integer|null $index |
||
227 | * @return null|string |
||
228 | */ |
||
229 | private function formatDateTimeAsCdbTime(DateTimeInterface $timestamp, $index = null) |
||
241 | |||
242 | /** |
||
243 | * @param int $index |
||
244 | * @return string |
||
245 | */ |
||
246 | private function createIndexedTimeString($index) |
||
250 | } |
||
251 |