1
|
|
|
<?php |
2
|
|
|
namespace ker0x\Push\Adapter\Fcm\Message; |
3
|
|
|
|
4
|
|
|
use ker0x\Push\Adapter\Fcm\Message\Exception\InvalidNotificationException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class NotificationBuilder |
8
|
|
|
* @package ker0x\Push\Adapter\Fcm\Message |
9
|
|
|
*/ |
10
|
|
|
class NotificationBuilder |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Indicates notification title. |
15
|
|
|
* |
16
|
|
|
* @var null|string |
17
|
|
|
*/ |
18
|
|
|
protected $title; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Indicates notification body text. |
22
|
|
|
* |
23
|
|
|
* @var null|string |
24
|
|
|
*/ |
25
|
|
|
protected $body; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Indicates a sound to play when the device receives a notification. |
29
|
|
|
* |
30
|
|
|
* @var null|string |
31
|
|
|
*/ |
32
|
|
|
protected $sound; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Indicates the badge on the client app home icon. (iOS) |
36
|
|
|
* |
37
|
|
|
* @var null|string |
38
|
|
|
*/ |
39
|
|
|
protected $badge; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Indicates notification icon. (Android) |
43
|
|
|
* |
44
|
|
|
* @var null|string |
45
|
|
|
*/ |
46
|
|
|
protected $icon; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Indicates whether each notification results in a new entry in the |
50
|
|
|
* notification drawer on Android. (Android) |
51
|
|
|
* |
52
|
|
|
* @var null|string |
53
|
|
|
*/ |
54
|
|
|
protected $tag; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Indicates color of the icon, expressed in #rrggbb format. (Android) |
58
|
|
|
* |
59
|
|
|
* @var null|string |
60
|
|
|
*/ |
61
|
|
|
protected $color; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Indicates the action associated with a user click on the notification. |
65
|
|
|
* |
66
|
|
|
* @var null|string |
67
|
|
|
*/ |
68
|
|
|
protected $clickAction; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Indicates the key to the body string for localization. |
72
|
|
|
* |
73
|
|
|
* @var null|string |
74
|
|
|
*/ |
75
|
|
|
protected $bodyLocKey; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Indicates the string value to replace format specifiers in the |
79
|
|
|
* body string for localization. |
80
|
|
|
* |
81
|
|
|
* @var null|string |
82
|
|
|
*/ |
83
|
|
|
protected $bodyLocArgs; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Indicates the key to the title string for localization. |
87
|
|
|
* |
88
|
|
|
* @var null|string |
89
|
|
|
*/ |
90
|
|
|
protected $titleLocKey; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Indicates the string value to replace format specifiers in |
94
|
|
|
* the title string for localization. |
95
|
|
|
* |
96
|
|
|
* @var null|string |
97
|
|
|
*/ |
98
|
|
|
protected $titleLocArgs; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* NotificationBuilder constructor. |
102
|
|
|
* |
103
|
|
|
* @param string $title Notification title. |
104
|
|
|
*/ |
105
|
|
|
public function __construct($title) |
106
|
|
|
{ |
107
|
|
|
$this->title = $title; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Getter for title. |
112
|
|
|
* |
113
|
|
|
* @return null|string |
114
|
|
|
*/ |
115
|
|
|
public function getTitle() |
116
|
|
|
{ |
117
|
|
|
return $this->title; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Getter for body. |
122
|
|
|
* |
123
|
|
|
* @return null|string |
124
|
|
|
*/ |
125
|
|
|
public function getBody() |
126
|
|
|
{ |
127
|
|
|
return $this->body; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Setter for body. |
132
|
|
|
* |
133
|
|
|
* @param string $body Notification body text. |
134
|
|
|
* @return $this |
135
|
|
|
* @throws \ker0x\Push\Adapter\Fcm\Message\Exception\InvalidNotificationException |
136
|
|
|
*/ |
137
|
|
|
public function setBody($body) |
138
|
|
|
{ |
139
|
|
|
$this->isString($body, 'body'); |
140
|
|
|
$this->body = $body; |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Getter for sound. |
147
|
|
|
* |
148
|
|
|
* @return null|string |
149
|
|
|
*/ |
150
|
|
|
public function getSound() |
151
|
|
|
{ |
152
|
|
|
return $this->sound; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Setter for sound. |
157
|
|
|
* |
158
|
|
|
* @param string $sound Sound to play when the device receives a notification. |
159
|
|
|
* @return $this |
160
|
|
|
* @throws \ker0x\Push\Adapter\Fcm\Message\Exception\InvalidNotificationException |
161
|
|
|
*/ |
162
|
|
|
public function setSound($sound) |
163
|
|
|
{ |
164
|
|
|
$this->isString($sound, 'sound'); |
165
|
|
|
$this->sound = $sound; |
166
|
|
|
|
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Getter for badge. |
172
|
|
|
* |
173
|
|
|
* @return null|string |
174
|
|
|
*/ |
175
|
|
|
public function getBadge() |
176
|
|
|
{ |
177
|
|
|
return $this->badge; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Setter for badge. |
182
|
|
|
* |
183
|
|
|
* @param string $badge Badge on the client app home icon. (iOS) |
184
|
|
|
* @return $this |
185
|
|
|
* @throws \ker0x\Push\Adapter\Fcm\Message\Exception\InvalidNotificationException |
186
|
|
|
*/ |
187
|
|
|
public function setBadge($badge) |
188
|
|
|
{ |
189
|
|
|
$this->isString($badge, 'badge'); |
190
|
|
|
$this->badge = $badge; |
191
|
|
|
|
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Getter for icon. |
197
|
|
|
* |
198
|
|
|
* @return null|string |
199
|
|
|
*/ |
200
|
|
|
public function getIcon() |
201
|
|
|
{ |
202
|
|
|
return $this->icon; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Setter for icon. |
207
|
|
|
* |
208
|
|
|
* @param string $icon Notification icon. (Android) |
209
|
|
|
* @return $this |
210
|
|
|
* @throws \ker0x\Push\Adapter\Fcm\Message\Exception\InvalidNotificationException |
211
|
|
|
*/ |
212
|
|
|
public function setIcon($icon) |
213
|
|
|
{ |
214
|
|
|
$this->isString($icon, 'icon'); |
215
|
|
|
$this->icon = $icon; |
216
|
|
|
|
217
|
|
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Getter for tag. |
222
|
|
|
* |
223
|
|
|
* @return null|string |
224
|
|
|
*/ |
225
|
|
|
public function getTag() |
226
|
|
|
{ |
227
|
|
|
return $this->tag; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Setter for tag. |
232
|
|
|
* |
233
|
|
|
* @param string $tag Notification results in a new entry in the |
234
|
|
|
* notification drawer on Android. (Android) |
235
|
|
|
* @return $this |
236
|
|
|
* @throws \ker0x\Push\Adapter\Fcm\Message\Exception\InvalidNotificationException |
237
|
|
|
*/ |
238
|
|
|
public function setTag($tag) |
239
|
|
|
{ |
240
|
|
|
$this->isString($tag, 'tag'); |
241
|
|
|
$this->tag = $tag; |
242
|
|
|
|
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Getter for color. |
248
|
|
|
* |
249
|
|
|
* @return null|string |
250
|
|
|
*/ |
251
|
|
|
public function getColor() |
252
|
|
|
{ |
253
|
|
|
return $this->color; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Setter for color. |
258
|
|
|
* |
259
|
|
|
* @param string $color Color of the icon, expressed in #rrggbb format. (Android) |
260
|
|
|
* @return $this |
261
|
|
|
* @throws \ker0x\Push\Adapter\Fcm\Message\Exception\InvalidNotificationException |
262
|
|
|
*/ |
263
|
|
|
public function setColor($color) |
264
|
|
|
{ |
265
|
|
|
if (!preg_match('/^#[A-Fa-f0-9]{6}$/', $color)) { |
266
|
|
|
throw InvalidNotificationException::invalidColor(); |
267
|
|
|
} |
268
|
|
|
$this->color = $color; |
269
|
|
|
|
270
|
|
|
return $this; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Getter for clickAction. |
275
|
|
|
* |
276
|
|
|
* @return null|string |
277
|
|
|
*/ |
278
|
|
|
public function getClickAction() |
279
|
|
|
{ |
280
|
|
|
return $this->clickAction; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Setter for clickAction |
285
|
|
|
* |
286
|
|
|
* @param string $clickAction Action associated with a user click on the notification. |
287
|
|
|
* @return $this |
288
|
|
|
* @throws \ker0x\Push\Adapter\Fcm\Message\Exception\InvalidNotificationException |
289
|
|
|
*/ |
290
|
|
|
public function setClickAction($clickAction) |
291
|
|
|
{ |
292
|
|
|
$this->isString($clickAction, 'click_action'); |
293
|
|
|
$this->clickAction = $clickAction; |
294
|
|
|
|
295
|
|
|
return $this; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Getter for bodyLocKey |
300
|
|
|
* |
301
|
|
|
* @return null|string |
302
|
|
|
*/ |
303
|
|
|
public function getBodyLocKey() |
304
|
|
|
{ |
305
|
|
|
return $this->bodyLocKey; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Setter for bodyLocKey |
310
|
|
|
* |
311
|
|
|
* @param string $bodyLocKey Key to the body string for localization. |
312
|
|
|
* @return $this |
313
|
|
|
* @throws \ker0x\Push\Adapter\Fcm\Message\Exception\InvalidNotificationException |
314
|
|
|
*/ |
315
|
|
|
public function setBodyLocKey($bodyLocKey) |
316
|
|
|
{ |
317
|
|
|
$this->isString($bodyLocKey, 'body_loc_key'); |
318
|
|
|
$this->bodyLocKey = $bodyLocKey; |
319
|
|
|
|
320
|
|
|
return $this; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Getter for bodyLocArgs. |
325
|
|
|
* |
326
|
|
|
* @return null|string |
327
|
|
|
*/ |
328
|
|
|
public function getBodyLocArgs() |
329
|
|
|
{ |
330
|
|
|
return $this->bodyLocArgs; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Setter for bodyLocArgs. |
335
|
|
|
* |
336
|
|
|
* @param string $bodyLocArgs String value to replace format specifiers in the |
337
|
|
|
* body string for localization. |
338
|
|
|
* @return $this |
339
|
|
|
* @throws \ker0x\Push\Adapter\Fcm\Message\Exception\InvalidNotificationException |
340
|
|
|
*/ |
341
|
|
|
public function setBodyLocArgs($bodyLocArgs) |
342
|
|
|
{ |
343
|
|
|
$this->isString($bodyLocArgs, 'body_loc_args'); |
344
|
|
|
$this->bodyLocArgs = $bodyLocArgs; |
345
|
|
|
|
346
|
|
|
return $this; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Getter for titleLocKey. |
351
|
|
|
* |
352
|
|
|
* @return null|string |
353
|
|
|
*/ |
354
|
|
|
public function getTitleLocKey() |
355
|
|
|
{ |
356
|
|
|
return $this->titleLocKey; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Setter for titleLocKey. |
361
|
|
|
* |
362
|
|
|
* @param string $titleLocKey Key to the title string for localization. |
363
|
|
|
* @return $this |
364
|
|
|
* @throws \ker0x\Push\Adapter\Fcm\Message\Exception\InvalidNotificationException |
365
|
|
|
*/ |
366
|
|
|
public function setTitleLocKey($titleLocKey) |
367
|
|
|
{ |
368
|
|
|
$this->isString($titleLocKey, 'title_loc_key'); |
369
|
|
|
$this->titleLocKey = $titleLocKey; |
370
|
|
|
|
371
|
|
|
return $this; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Getter for titleLocArgs |
376
|
|
|
* |
377
|
|
|
* @return null|string |
378
|
|
|
*/ |
379
|
|
|
public function getTitleLocArgs() |
380
|
|
|
{ |
381
|
|
|
return $this->titleLocArgs; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Setter for titleLocArgs |
386
|
|
|
* |
387
|
|
|
* @param string $titleLocArgs String value to replace format specifiers in |
388
|
|
|
* the title string for localization. |
389
|
|
|
* @return $this |
390
|
|
|
* @throws \ker0x\Push\Adapter\Fcm\Message\Exception\InvalidNotificationException |
391
|
|
|
*/ |
392
|
|
|
public function setTitleLocArgs($titleLocArgs) |
393
|
|
|
{ |
394
|
|
|
$this->isString($titleLocArgs, 'title_loc_args'); |
395
|
|
|
$this->titleLocArgs = $titleLocArgs; |
396
|
|
|
|
397
|
|
|
return $this; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Test if $value is a string. |
402
|
|
|
* |
403
|
|
|
* @param string $value The value to test. |
404
|
|
|
* @param string $key The key of the value. |
405
|
|
|
* @return void |
406
|
|
|
* @throws \ker0x\Push\Adapter\Fcm\Message\Exception\InvalidNotificationException |
407
|
|
|
*/ |
408
|
|
|
private function isString($value, $key) |
409
|
|
|
{ |
410
|
|
|
if (!is_string($value)) { |
411
|
|
|
throw InvalidNotificationException::mustBeString($key); |
412
|
|
|
} |
413
|
|
|
} |
414
|
|
|
} |
415
|
|
|
|