1
|
|
|
<?php namespace Cornford\Notifier\Models; |
2
|
|
|
|
3
|
|
|
use Cornford\Notifier\Contracts\NotifierNotificationInterface; |
4
|
|
|
use Cornford\Notifier\Exceptions\NotifierNotificationArgumentException; |
5
|
|
|
use DateInterval; |
6
|
|
|
use DateTime; |
7
|
|
|
|
8
|
|
|
class Notification implements NotifierNotificationInterface { |
9
|
|
|
|
10
|
|
|
const NOTIFICATION_TYPE_NONE = 'message'; |
11
|
|
|
const NOTIFICATION_TYPE_INFO = 'info'; |
12
|
|
|
const NOTIFICATION_TYPE_SUCCESS = 'success'; |
13
|
|
|
const NOTIFICATION_TYPE_WARNING = 'warn'; |
14
|
|
|
const NOTIFICATION_TYPE_DANGER = 'error'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Options. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $options = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Notification message. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $message; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Notification message type. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $type; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Notification message date time. |
39
|
|
|
* |
40
|
|
|
* @var DateTime |
41
|
|
|
*/ |
42
|
|
|
protected $datetime; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Notification message expiry in minutes. |
46
|
|
|
* |
47
|
|
|
* @var DateTime|integer |
48
|
|
|
*/ |
49
|
|
|
protected $expiry; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Notification message displayed status. |
53
|
|
|
* |
54
|
|
|
* @var boolean |
55
|
|
|
*/ |
56
|
|
|
protected $displayed = false; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Constructor. |
60
|
|
|
* |
61
|
|
|
* @param integer $id |
62
|
|
|
* @param string $message |
63
|
|
|
* @param string $type |
64
|
|
|
* @param DateTime $datetime |
65
|
|
|
* @param DateTime|integer $expiry |
66
|
|
|
* @param array $options |
67
|
|
|
* |
68
|
|
|
* @throws NotifierNotificationArgumentException |
69
|
|
|
*/ |
70
|
|
|
public function __construct($id, $message, $type, $datetime, $expiry = 0, array $options = []) |
71
|
|
|
{ |
72
|
|
|
if (empty($id) || !is_numeric($id)) { |
73
|
|
|
throw new NotifierNotificationArgumentException('Invalid id supplied.'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (empty($message) || !is_string($message)) { |
77
|
|
|
throw new NotifierNotificationArgumentException('Invalid message supplied.'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (empty($message) || |
81
|
|
|
!in_array( |
82
|
|
|
$type, |
83
|
|
|
[ |
84
|
|
|
self::NOTIFICATION_TYPE_NONE, |
85
|
|
|
self::NOTIFICATION_TYPE_INFO, |
86
|
|
|
self::NOTIFICATION_TYPE_SUCCESS, |
87
|
|
|
self::NOTIFICATION_TYPE_WARNING, |
88
|
|
|
self::NOTIFICATION_TYPE_DANGER |
89
|
|
|
] |
90
|
|
|
) |
91
|
|
|
) { |
92
|
|
|
throw new NotifierNotificationArgumentException('Invalid message type supplied.'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (!$datetime instanceof DateTime) { |
96
|
|
|
throw new NotifierNotificationArgumentException('Invalid message date time supplied.'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (!is_numeric($expiry) && !$expiry instanceof DateTime) { |
100
|
|
|
throw new NotifierNotificationArgumentException('Invalid message expiry supplied.'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->setId($id); |
104
|
|
|
$this->setMessage($message); |
105
|
|
|
$this->setType($type); |
106
|
|
|
$this->setDatetime($datetime); |
107
|
|
|
$this->setExpiry($expiry); |
108
|
|
|
$this->setOptions($options); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set the notification id. |
113
|
|
|
* |
114
|
|
|
* @param integer $value |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
public function setId($value) |
119
|
|
|
{ |
120
|
|
|
$this->id = $value; |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the notification id. |
125
|
|
|
* |
126
|
|
|
* @return integer |
127
|
|
|
*/ |
128
|
|
|
public function getId() |
129
|
|
|
{ |
130
|
|
|
return $this->id; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set the notification message. |
135
|
|
|
* |
136
|
|
|
* @param string $value |
137
|
|
|
* |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
public function setMessage($value) |
141
|
|
|
{ |
142
|
|
|
$this->message = $value; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get the notification message. |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public function getMessage() |
151
|
|
|
{ |
152
|
|
|
return $this->message; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Set the notification message type. |
157
|
|
|
* |
158
|
|
|
* @param string $value |
159
|
|
|
* |
160
|
|
|
* @return void |
161
|
|
|
*/ |
162
|
|
|
public function setType($value) |
163
|
|
|
{ |
164
|
|
|
$this->type = $value; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get the notification message. |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public function getType() |
173
|
|
|
{ |
174
|
|
|
return $this->type; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Set the notification message date time. |
179
|
|
|
* |
180
|
|
|
* @param DateTime $value |
181
|
|
|
* |
182
|
|
|
* @return void |
183
|
|
|
*/ |
184
|
|
|
public function setDatetime($value) |
185
|
|
|
{ |
186
|
|
|
$this->datetime = $value; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Get the notification message date time. |
191
|
|
|
* |
192
|
|
|
* @return DateTime |
193
|
|
|
*/ |
194
|
|
|
public function getDatetime() |
195
|
|
|
{ |
196
|
|
|
return $this->datetime; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Set the notification message expiry in minutes or with a DateTime object. |
201
|
|
|
* |
202
|
|
|
* @param DateTime|integer $value |
203
|
|
|
* |
204
|
|
|
* @return void |
205
|
|
|
*/ |
206
|
|
|
public function setExpiry($value) |
207
|
|
|
{ |
208
|
|
|
$this->expiry = $value; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Get the notification message expiry in minutes or a DateTime object. |
213
|
|
|
* |
214
|
|
|
* @return DateTime|integer |
215
|
|
|
*/ |
216
|
|
|
public function getExpiry() |
217
|
|
|
{ |
218
|
|
|
return $this->expiry; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Calculate the number of minutes between now and the given duration. |
223
|
|
|
* |
224
|
|
|
* @param DateTime|integer $duration |
225
|
|
|
* |
226
|
|
|
* @return integer |
227
|
|
|
*/ |
228
|
|
|
protected function getMinutes($duration) |
229
|
|
|
{ |
230
|
|
|
if ($duration instanceof DateTime) { |
231
|
|
|
$dateNow = new DateTime('now'); |
232
|
|
|
|
233
|
|
|
if (($duration->format('U')) < ($dateNow->format('U'))) { |
234
|
|
|
return 0; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
$interval = $dateNow->diff($duration); |
238
|
|
|
|
239
|
|
|
return $interval->format('%i'); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return $duration; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Is notification message expired. |
247
|
|
|
* |
248
|
|
|
* @return boolean |
249
|
|
|
*/ |
250
|
|
|
public function isExpired() |
251
|
|
|
{ |
252
|
|
|
$expiryInSeconds = ($this->getMinutes($this->getExpiry()) * 60); |
253
|
|
|
|
254
|
|
|
if (($this->getDatetime()->add(new DateInterval('PT' . $expiryInSeconds . 'S'))->format('U')) > ((new DateTime('now'))->format('U'))) { |
255
|
|
|
return false; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return true; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Set the notification message displayed status. |
263
|
|
|
* |
264
|
|
|
* @param boolean $value |
265
|
|
|
* |
266
|
|
|
* @return void |
267
|
|
|
*/ |
268
|
|
|
public function setDisplayed($value) |
269
|
|
|
{ |
270
|
|
|
$this->displayed = $value; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Get the notification message displayed status. |
275
|
|
|
* |
276
|
|
|
* @return boolean |
277
|
|
|
*/ |
278
|
|
|
public function getDisplayed() |
279
|
|
|
{ |
280
|
|
|
return $this->displayed; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Get the notification message displayed status. |
285
|
|
|
* |
286
|
|
|
* @return boolean |
287
|
|
|
*/ |
288
|
|
|
public function isDisplayed() |
289
|
|
|
{ |
290
|
|
|
return $this->displayed; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Set the notification message options. |
295
|
|
|
* |
296
|
|
|
* @param array $value |
297
|
|
|
* |
298
|
|
|
* @return void |
299
|
|
|
*/ |
300
|
|
|
public function setOptions(array $value) |
301
|
|
|
{ |
302
|
|
|
$this->options = $value; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Get the notification message options. |
307
|
|
|
* |
308
|
|
|
* @return array |
309
|
|
|
*/ |
310
|
|
|
public function getOptions() |
311
|
|
|
{ |
312
|
|
|
return $this->options; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* To array method. |
317
|
|
|
* |
318
|
|
|
* @return array |
319
|
|
|
*/ |
320
|
|
|
public function __toArray() |
321
|
|
|
{ |
322
|
|
|
return [ |
323
|
|
|
'id' => $this->getId(), |
324
|
|
|
'message' => $this->getMessage(), |
325
|
|
|
'options' => $this->getOptions(), |
326
|
|
|
'type' => $this->getType(), |
327
|
|
|
'datetime' => $this->getDatetime(), |
328
|
|
|
'expiry' => $this->getExpiry(), |
329
|
|
|
'displayed' => $this->getDisplayed(), |
330
|
|
|
'expired' => $this->isExpired(), |
331
|
|
|
]; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: