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