1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the eluceo/iCal package. |
5
|
|
|
* |
6
|
|
|
* (c) 2019 Markus Poerschke <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Eluceo\iCal\Presentation\Factory; |
13
|
|
|
|
14
|
|
|
use DateInterval; |
15
|
|
|
use Eluceo\iCal\Domain\Entity\Event; |
16
|
|
|
use Eluceo\iCal\Domain\ValueObject\MultiDay; |
17
|
|
|
use Eluceo\iCal\Domain\ValueObject\Occurrence; |
18
|
|
|
use Eluceo\iCal\Domain\ValueObject\SingleDay; |
19
|
|
|
use Eluceo\iCal\Domain\ValueObject\TimeSpan; |
20
|
|
|
use Eluceo\iCal\Presentation\Component; |
21
|
|
|
use Eluceo\iCal\Presentation\Component\Property; |
22
|
|
|
use Eluceo\iCal\Presentation\Component\Property\Value\DateTimeValue; |
23
|
|
|
use Eluceo\iCal\Presentation\Component\Property\Value\DateValue; |
24
|
|
|
use Eluceo\iCal\Presentation\Component\Property\Value\TextValue; |
25
|
|
|
use Generator; |
26
|
|
|
|
27
|
|
|
class EventFactory |
28
|
|
|
{ |
29
|
|
|
public function createComponent(Event $event): Component |
30
|
|
|
{ |
31
|
|
|
return Component::create('VEVENT', iterator_to_array($this->getProperties($event))); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return Generator<Property> |
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
private function getProperties(Event $event): Generator |
38
|
|
|
{ |
39
|
|
|
yield Property::create('UID', TextValue::fromString((string) $event->getUniqueIdentifier())); |
40
|
|
|
yield Property::create('DTSTAMP', DateTimeValue::fromTimestamp($event->getTouchedAt())); |
41
|
|
|
|
42
|
|
|
if ($event->hasSummary()) { |
43
|
|
|
yield Property::create('SUMMARY', TextValue::fromString($event->getSummary())); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ($event->hasDescription()) { |
47
|
|
|
yield Property::create('DESCRIPTION', TextValue::fromString($event->getDescription())); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($event->hasOccurrence()) { |
51
|
|
|
yield from $this->getOccurrenceProperties($event->getOccurrence()); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return Generator<Property> |
|
|
|
|
57
|
|
|
*/ |
58
|
|
|
private function getOccurrenceProperties(Occurrence $occurrence): Generator |
59
|
|
|
{ |
60
|
|
|
if ($occurrence instanceof SingleDay) { |
|
|
|
|
61
|
|
|
yield Property::create('DTSTART', DateValue::fromDate($occurrence->getDate())); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($occurrence instanceof MultiDay) { |
|
|
|
|
65
|
|
|
yield Property::create('DTSTART', DateValue::fromDate($occurrence->getFirstDay())); |
66
|
|
|
yield Property::create('DTEND', DateValue::fromDate($occurrence->getLastDay()->add(new DateInterval('P1D')))); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($occurrence instanceof TimeSpan) { |
|
|
|
|
70
|
|
|
yield Property::create('DTSTART', DateTimeValue::fromDateTime($occurrence->getBegin())); |
71
|
|
|
yield Property::create('DTEND', DateTimeValue::fromDateTime($occurrence->getEnd())); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.