systemVersion = $serverVersion->getVersionString(); } /** * Generates serialized content stream for a calendar and objects based in selected format * * @return Generator */ public function export(ICalendarExport $calendar, CalendarExportOptions $options): Generator { // output start of serialized content based on selected format yield $this->exportStart($options->getFormat()); // iterate through each returned vCalendar entry // extract each component except timezones, convert to appropriate format and output // extract any timezones and save them but do not output $timezones = []; foreach ($calendar->export($options) as $entry) { $consecutive = false; foreach ($entry->getComponents() as $vComponent) { if ($vComponent->name === 'VTIMEZONE') { if (isset($vComponent->TZID) && !isset($timezones[$vComponent->TZID->getValue()])) { $timezones[$vComponent->TZID->getValue()] = clone $vComponent; } } else { yield $this->exportObject($vComponent, $options->getFormat(), $consecutive); $consecutive = true; } } } // iterate through each saved vTimezone entry, convert to appropriate format and output foreach ($timezones as $vComponent) { yield $this->exportObject($vComponent, $options->getFormat(), $consecutive); $consecutive = true; } // output end of serialized content based on selected format yield $this->exportFinish($options->getFormat()); } /** * Generates serialized content start based on selected format */ private function exportStart(string $format): string { return match ($format) { 'jcal' => '["vcalendar",[["version",{},"text","2.0"],["prodid",{},"text","-\/\/IDN nextcloud.com\/\/Calendar Export v' . $this->systemVersion . '\/\/EN"]],[', 'xcal' => '2.0-//IDN nextcloud.com//Calendar Export v' . $this->systemVersion . '//EN', default => "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//IDN nextcloud.com//Calendar Export v" . $this->systemVersion . "//EN\n" }; } /** * Generates serialized content end based on selected format */ private function exportFinish(string $format): string { return match ($format) { 'jcal' => ']]', 'xcal' => '', default => "END:VCALENDAR\n" }; } /** * Generates serialized content for a component based on selected format */ private function exportObject(Component $vobject, string $format, bool $consecutive): string { return match ($format) { 'jcal' => $consecutive ? ',' . Writer::writeJson($vobject) : Writer::writeJson($vobject), 'xcal' => $this->exportObjectXml($vobject), default => Writer::write($vobject) }; } /** * Generates serialized content for a component in xml format */ private function exportObjectXml(Component $vobject): string { $writer = new \Sabre\Xml\Writer(); $writer->openMemory(); $writer->setIndent(false); $vobject->xmlSerialize($writer); return $writer->outputMemory(); } }