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