|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\IcalendarGenerator\Components; |
|
4
|
|
|
|
|
5
|
|
|
use DateInterval; |
|
6
|
|
|
use DateTime; |
|
7
|
|
|
use DateTimeInterface; |
|
8
|
|
|
use Spatie\IcalendarGenerator\ComponentPayload; |
|
9
|
|
|
use Spatie\IcalendarGenerator\PropertyTypes\DateTimePropertyType; |
|
10
|
|
|
use Spatie\IcalendarGenerator\PropertyTypes\DurationPropertyType; |
|
11
|
|
|
use Spatie\IcalendarGenerator\PropertyTypes\Parameter; |
|
12
|
|
|
|
|
13
|
|
|
final class Alert extends Component |
|
14
|
|
|
{ |
|
15
|
|
|
private const TRIGGER_START = 'trigger_start'; |
|
16
|
|
|
private const TRIGGER_END = 'trigger_end'; |
|
17
|
|
|
private const TRIGGER_DATE = 'trigger_date'; |
|
18
|
|
|
|
|
19
|
|
|
/** @var \DateTimeInterface */ |
|
20
|
|
|
private $triggerDate; |
|
21
|
|
|
|
|
22
|
|
|
/** @var DateInterval */ |
|
23
|
|
|
private $triggerInterval; |
|
24
|
|
|
|
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
private $triggerMode = self::TRIGGER_DATE; |
|
27
|
|
|
|
|
28
|
|
|
/** @var null|string */ |
|
29
|
|
|
private $message; |
|
30
|
|
|
|
|
31
|
|
|
public static function date(DateTimeInterface $date, string $description = null): Alert |
|
32
|
|
|
{ |
|
33
|
|
|
return static::create($description)->triggerDate($date); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public static function minutesBeforeStart(int $minutes, string $description = null): Alert |
|
37
|
|
|
{ |
|
38
|
|
|
$interval = new DateInterval("PT{$minutes}M"); |
|
39
|
|
|
$interval->invert = 1; |
|
40
|
|
|
|
|
41
|
|
|
return static::create($description)->triggerAtStart($interval); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public static function minutesAfterStart(int $minutes, string $description = null): Alert |
|
45
|
|
|
{ |
|
46
|
|
|
return static::create($description)->triggerAtStart(new DateInterval("PT{$minutes}M")); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public static function minutesBeforeEnd(int $minutes, string $description = null): Alert |
|
50
|
|
|
{ |
|
51
|
|
|
$interval = new DateInterval("PT{$minutes}M"); |
|
52
|
|
|
$interval->invert = 1; |
|
53
|
|
|
|
|
54
|
|
|
return static::create($description)->triggerAtEnd($interval); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public static function minutesAfterEnd(int $minutes, string $description = null): Alert |
|
58
|
|
|
{ |
|
59
|
|
|
return static::create($description)->triggerAtEnd(new DateInterval("PT{$minutes}M")); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private static function create(string $description = null): Alert |
|
63
|
|
|
{ |
|
64
|
|
|
return new self($description); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function __construct(string $description = null) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->message = $description; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getComponentType(): string |
|
73
|
|
|
{ |
|
74
|
|
|
return 'ALARM'; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getRequiredProperties(): array |
|
78
|
|
|
{ |
|
79
|
|
|
return [ |
|
80
|
|
|
'ACTION', |
|
81
|
|
|
'TRIGGER', |
|
82
|
|
|
'DESCRIPTION', |
|
83
|
|
|
]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function message(string $message): Alert |
|
87
|
|
|
{ |
|
88
|
|
|
$this->message = $message; |
|
89
|
|
|
|
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function triggerDate(DateTimeInterface $triggerAt): Alert |
|
94
|
|
|
{ |
|
95
|
|
|
$this->triggerMode = self::TRIGGER_DATE; |
|
96
|
|
|
$this->triggerDate = $triggerAt; |
|
97
|
|
|
|
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function triggerAtStart(DateInterval $interval): Alert |
|
102
|
|
|
{ |
|
103
|
|
|
$this->triggerMode = self::TRIGGER_START; |
|
104
|
|
|
$this->triggerInterval = $interval; |
|
105
|
|
|
|
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function triggerAtEnd(DateInterval $interval): Alert |
|
110
|
|
|
{ |
|
111
|
|
|
$this->triggerMode = self::TRIGGER_END; |
|
112
|
|
|
$this->triggerInterval = $interval; |
|
113
|
|
|
|
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
protected function payload(): ComponentPayload |
|
118
|
|
|
{ |
|
119
|
|
|
return ComponentPayload::create($this->getComponentType()) |
|
120
|
|
|
->textProperty('ACTION', 'DISPLAY') |
|
121
|
|
|
->textProperty('DESCRIPTION', $this->message) |
|
122
|
|
|
->property($this->resolveTriggerProperty()); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
private function resolveTriggerProperty() |
|
126
|
|
|
{ |
|
127
|
|
|
if ($this->triggerMode === self::TRIGGER_DATE) { |
|
128
|
|
|
return DateTimePropertyType::create( |
|
129
|
|
|
'TRIGGER', |
|
130
|
|
|
$this->triggerDate, |
|
131
|
|
|
true |
|
132
|
|
|
)->addParameter(new Parameter('VALUE', 'DATE-TIME')); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$property = DurationPropertyType::create('TRIGGER', $this->triggerInterval); |
|
136
|
|
|
|
|
137
|
|
|
if ($this->triggerMode === self::TRIGGER_END) { |
|
138
|
|
|
return $property->addParameter(new Parameter('RELATED', 'END')); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $property; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
Late static binding only has effect in subclasses. A
finalclass cannot be extended anymore so late static binding cannot occurr. Consider replacingstatic::withself::.To learn more about late static binding, please refer to the PHP core documentation.