1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\IcalendarGenerator\Components; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use DateInterval; |
7
|
|
|
use Spatie\IcalendarGenerator\ComponentPayload; |
8
|
|
|
use Spatie\IcalendarGenerator\PropertyTypes\DurationPropertyType; |
9
|
|
|
use Spatie\IcalendarGenerator\PropertyTypes\Parameter; |
10
|
|
|
|
11
|
|
|
final class Calendar extends Component |
12
|
|
|
{ |
13
|
|
|
/** @var array */ |
14
|
|
|
private $events = []; |
15
|
|
|
|
16
|
|
|
/** @var string|null */ |
17
|
|
|
private $name; |
18
|
|
|
|
19
|
|
|
/** @var string|null */ |
20
|
|
|
private $description; |
21
|
|
|
|
22
|
|
|
/** @var bool */ |
23
|
|
|
private $withTimezone = false; |
24
|
|
|
|
25
|
|
|
/** @var \DateInterval|null */ |
26
|
|
|
private $refreshInterval; |
27
|
|
|
|
28
|
|
|
/** @var string|null */ |
29
|
|
|
private $productIdentifier; |
30
|
|
|
|
31
|
|
|
public static function create(string $name = null): Calendar |
32
|
|
|
{ |
33
|
|
|
return new self($name); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function __construct(string $name = null) |
37
|
|
|
{ |
38
|
|
|
$this->name = $name; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getComponentType(): string |
42
|
|
|
{ |
43
|
|
|
return 'CALENDAR'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getRequiredProperties(): array |
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
|
|
'VERSION', |
50
|
|
|
'PRODID', |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function name(string $name): Calendar |
55
|
|
|
{ |
56
|
|
|
$this->name = $name; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function description(string $description): Calendar |
62
|
|
|
{ |
63
|
|
|
$this->description = $description; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function productIdentifier(string $identifier): Calendar |
69
|
|
|
{ |
70
|
|
|
$this->productIdentifier = $identifier; |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $event \Spatie\IcalendarGenerator\Components\Event|array|Closure |
77
|
|
|
* |
78
|
|
|
* @return \Spatie\IcalendarGenerator\Components\Calendar |
79
|
|
|
*/ |
80
|
|
|
public function event($event): Calendar |
81
|
|
|
{ |
82
|
|
|
if (is_null($event)) { |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$events = array_map(function ($eventToResolve) { |
87
|
|
|
if (! is_callable($eventToResolve)) { |
88
|
|
|
return $eventToResolve; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$newEvent = new Event(); |
92
|
|
|
|
93
|
|
|
$eventToResolve($newEvent); |
94
|
|
|
|
95
|
|
|
return $newEvent; |
96
|
|
|
}, is_array($event) ? $event : [$event]); |
97
|
|
|
|
98
|
|
|
$this->events = array_merge($this->events, $events); |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function withTimezone(): Calendar |
104
|
|
|
{ |
105
|
|
|
$this->withTimezone = true; |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function refreshInterval(int $minutes): Calendar |
111
|
|
|
{ |
112
|
|
|
$this->refreshInterval = new DateInterval("PT{$minutes}M"); |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function get(): string |
118
|
|
|
{ |
119
|
|
|
return $this->toString(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
protected function payload(): ComponentPayload |
123
|
|
|
{ |
124
|
|
|
$events = $this->events; |
125
|
|
|
|
126
|
|
|
if ($this->withTimezone) { |
127
|
|
|
array_walk($events, function (Event $event) { |
128
|
|
|
$event->withTimezone(); |
129
|
|
|
}); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$payload = ComponentPayload::create($this->getComponentType()) |
133
|
|
|
->textProperty('VERSION', '2.0') |
134
|
|
|
->textProperty('PRODID', $this->productIdentifier ?? 'spatie/icalendar-generator') |
135
|
|
|
->textProperty(['NAME', 'X-WR-CALNAME'], $this->name) |
136
|
|
|
->textProperty(['DESCRIPTION', 'X-WR-CALDESC'], $this->description) |
137
|
|
|
->subComponent(...$events); |
138
|
|
|
|
139
|
|
|
if ($this->refreshInterval) { |
140
|
|
|
$payload |
141
|
|
|
->property( |
142
|
|
|
DurationPropertyType::create('REFRESH-INTERVAL', $this->refreshInterval) |
143
|
|
|
->addParameter(new Parameter('VALUE', 'DURATION')) |
144
|
|
|
) |
145
|
|
|
->property(DurationPropertyType::create('X-PUBLISHED-TTL', $this->refreshInterval)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $payload; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|