<?php
 namespace Eluceo\iCal\Presentation\Factory; use Eluceo\iCal\Domain\Entity\Calendar; use Eluceo\iCal\Presentation\Component; use Eluceo\iCal\Presentation\Component\Property; use Eluceo\iCal\Presentation\Component\Property\Value\DurationValue; use Eluceo\iCal\Presentation\Component\Property\Value\TextValue; use Generator; class CalendarFactory { private EventFactory $eventFactory; private TimeZoneFactory $timeZoneFactory; public function __construct(?EventFactory $eventFactory = null, ?TimeZoneFactory $timeZoneFactory = null) { $this->eventFactory = $eventFactory ?? new EventFactory(); $this->timeZoneFactory = $timeZoneFactory ?? new TimeZoneFactory(); } public function createCalendar(Calendar $calendar): Component { $components = $this->createCalendarComponents($calendar); $properties = iterator_to_array($this->getProperties($calendar), false); return new Component('VCALENDAR', $properties, $components); } protected function createCalendarComponents(Calendar $calendar): iterable { yield from $this->eventFactory->createComponents($calendar->getEvents()); yield from $this->timeZoneFactory->createComponents($calendar->getTimeZones()); } protected function getProperties(Calendar $calendar): Generator { yield new Property('PRODID', new TextValue($calendar->getProductIdentifier())); yield new Property('VERSION', new TextValue('2.0')); yield new Property('CALSCALE', new TextValue('GREGORIAN')); $publishedTTL = $calendar->getPublishedTTL(); if ($publishedTTL) { yield new Property('X-PUBLISHED-TTL', new DurationValue($publishedTTL)); } } } 