1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Pushok package. |
5
|
|
|
* |
6
|
|
|
* (c) Arthur Edamov <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Pushok\Payload; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Alert |
16
|
|
|
* |
17
|
|
|
* @package Pushok\Payload |
18
|
|
|
* |
19
|
|
|
* @see http://bit.ly/payload-key-reference |
20
|
|
|
*/ |
21
|
|
|
class Alert implements \JsonSerializable |
22
|
|
|
{ |
23
|
|
|
const ALERT_TITLE_KEY = 'title'; |
24
|
|
|
const ALERT_SUBTITLE = 'subtitle'; |
25
|
|
|
const ALERT_BODY_KEY = 'body'; |
26
|
|
|
const ALERT_TITLE_LOC_KEY = 'loc-key'; |
27
|
|
|
const ALERT_TITLE_LOC_ARGS_KEY = 'loc-args'; |
28
|
|
|
const ALERT_ACTION_LOC_KEY = 'action-loc-key'; |
29
|
|
|
const ALERT_LOC_KEY = 'loc-key'; |
30
|
|
|
const ALERT_LOC_ARGS_KEY = 'loc-args'; |
31
|
|
|
const ALERT_LAUNCH_IMAGE_KEY = 'launch-image'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* A short string describing the purpose of the notification. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $title; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* A subtitle. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $subtitle; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The text of the alert message. |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $body; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The key to a title string in the Localizable.strings file for the current localization. |
56
|
|
|
* |
57
|
|
|
* @var string|null |
58
|
|
|
*/ |
59
|
|
|
private $titleLocKey; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Variable string values to appear in place of the format specifiers in title-loc-key. |
63
|
|
|
* |
64
|
|
|
* @var string[]|null |
65
|
|
|
*/ |
66
|
|
|
private $titleLocArgs; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* If a string is specified, the iOS system displays an alert that includes the Close and View buttons. |
70
|
|
|
* |
71
|
|
|
* @var string|null |
72
|
|
|
*/ |
73
|
|
|
private $actionLocKey; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* A key to an alert-message string in a Localizable.strings file for the current localization. |
77
|
|
|
* |
78
|
|
|
* @var string |
79
|
|
|
*/ |
80
|
|
|
private $locKey; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Variable string values to appear in place of the format specifiers in loc-key. |
84
|
|
|
* |
85
|
|
|
* @var string[] |
86
|
|
|
*/ |
87
|
|
|
private $locArgs; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* The filename of an image file in the app bundle, with or without the filename extension. |
91
|
|
|
* |
92
|
|
|
* @var string |
93
|
|
|
*/ |
94
|
|
|
private $launchImage; |
95
|
|
|
|
96
|
|
|
protected function __construct() |
97
|
|
|
{ |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public static function create() |
101
|
|
|
{ |
102
|
|
|
return new self(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set Alert title. |
107
|
|
|
* |
108
|
|
|
* @param string $value |
109
|
|
|
* @return Alert |
110
|
|
|
*/ |
111
|
|
|
public function setTitle(string $value): Alert |
112
|
|
|
{ |
113
|
|
|
$this->title = $value; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get Alert title. |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function getTitle() |
124
|
|
|
{ |
125
|
|
|
return $this->title; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Set Alert title. |
130
|
|
|
* |
131
|
|
|
* @param string $value |
132
|
|
|
* @return Alert |
133
|
|
|
*/ |
134
|
|
|
public function setSubtitle(string $value): Alert |
135
|
|
|
{ |
136
|
|
|
$this->subtitle = $value; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get Alert subtitle. |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
public function getSubtitle() |
147
|
|
|
{ |
148
|
|
|
return $this->subtitle; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Set Alert body. |
153
|
|
|
* |
154
|
|
|
* @param string $value |
155
|
|
|
* @return Alert |
156
|
|
|
*/ |
157
|
|
|
public function setBody(string $value): Alert |
158
|
|
|
{ |
159
|
|
|
$this->body = $value; |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get Alert body. |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
public function getBody() |
170
|
|
|
{ |
171
|
|
|
return $this->body; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Set title-loc-key. |
176
|
|
|
* |
177
|
|
|
* @param string|null $value |
178
|
|
|
* @return Alert |
179
|
|
|
*/ |
180
|
|
|
public function setTitleLocKey(string $value = null): Alert |
181
|
|
|
{ |
182
|
|
|
$this->titleLocKey = $value; |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get title-loc-key. |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
public function getTitleLocKey() |
193
|
|
|
{ |
194
|
|
|
return $this->titleLocKey; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Set title-loc-args. |
199
|
|
|
* |
200
|
|
|
* @param array|null $value |
201
|
|
|
* @return Alert |
202
|
|
|
*/ |
203
|
|
|
public function setTitleLocArgs(array $value = null): Alert |
204
|
|
|
{ |
205
|
|
|
$this->titleLocArgs = $value; |
206
|
|
|
|
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get title-loc-args. |
212
|
|
|
* |
213
|
|
|
* @return string[]|null |
214
|
|
|
*/ |
215
|
|
|
public function getTitleLocArgs() |
216
|
|
|
{ |
217
|
|
|
return $this->titleLocArgs; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Set action-loc-key. |
222
|
|
|
* |
223
|
|
|
* @param string|null $value |
224
|
|
|
* @return Alert |
225
|
|
|
*/ |
226
|
|
|
public function setActionLocKey(string $value = null): Alert |
227
|
|
|
{ |
228
|
|
|
$this->actionLocKey = $value; |
229
|
|
|
|
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Get action-loc-key. |
235
|
|
|
* |
236
|
|
|
* @return string|null |
237
|
|
|
*/ |
238
|
|
|
public function getActionLocKey() |
239
|
|
|
{ |
240
|
|
|
return $this->actionLocKey; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Set loc-key. |
245
|
|
|
* |
246
|
|
|
* @param string $value |
247
|
|
|
* @return Alert |
248
|
|
|
*/ |
249
|
|
|
public function setLocKey(string $value): Alert |
250
|
|
|
{ |
251
|
|
|
$this->locKey = $value; |
252
|
|
|
|
253
|
|
|
return $this; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Get loc-key. |
258
|
|
|
* |
259
|
|
|
* @return string |
260
|
|
|
*/ |
261
|
|
|
public function getLocKey() |
262
|
|
|
{ |
263
|
|
|
return $this->locKey; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Set loc-args. |
268
|
|
|
* |
269
|
|
|
* @param array $value |
270
|
|
|
* @return Alert |
271
|
|
|
*/ |
272
|
|
|
public function setLocArgs(array $value): Alert |
273
|
|
|
{ |
274
|
|
|
$this->locArgs = $value; |
275
|
|
|
|
276
|
|
|
return $this; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Get loc-args. |
281
|
|
|
* |
282
|
|
|
* @return string[] |
283
|
|
|
*/ |
284
|
|
|
public function getLocArgs() |
285
|
|
|
{ |
286
|
|
|
return $this->locArgs; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Set launch-image. |
291
|
|
|
* |
292
|
|
|
* @param string $value |
293
|
|
|
* @return $this |
294
|
|
|
*/ |
295
|
|
|
public function setLaunchImage(string $value) |
296
|
|
|
{ |
297
|
|
|
$this->launchImage = $value; |
298
|
|
|
|
299
|
|
|
return $this; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Get launch-image. |
304
|
|
|
* |
305
|
|
|
* @return string |
306
|
|
|
*/ |
307
|
|
|
public function getLaunchImage() |
308
|
|
|
{ |
309
|
|
|
return $this->launchImage; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Convert Alert to JSON. |
314
|
|
|
* |
315
|
|
|
* @return string |
316
|
|
|
*/ |
317
|
|
|
public function toJson(): string |
318
|
|
|
{ |
319
|
|
|
return json_encode($this, JSON_UNESCAPED_UNICODE); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Specify data which should be serialized to JSON. |
324
|
|
|
* |
325
|
|
|
* @return array |
326
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
327
|
|
|
*/ |
328
|
|
|
public function jsonSerialize() |
329
|
|
|
{ |
330
|
|
|
$alert = []; |
331
|
|
|
|
332
|
|
|
if (is_string($this->title)) { |
333
|
|
|
$alert[self::ALERT_TITLE_KEY] = $this->title; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
if (is_string($this->subtitle)) { |
337
|
|
|
$alert[self::ALERT_SUBTITLE] = $this->subtitle; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
if (is_string($this->body)) { |
341
|
|
|
$alert[self::ALERT_BODY_KEY] = $this->body; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
if (is_string($this->titleLocKey)) { |
345
|
|
|
$alert[self::ALERT_TITLE_LOC_KEY] = $this->titleLocKey; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
if (is_array($this->titleLocArgs)) { |
349
|
|
|
$alert[self::ALERT_TITLE_LOC_ARGS_KEY] = $this->titleLocArgs; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
if (is_string($this->actionLocKey)) { |
353
|
|
|
$alert[self::ALERT_ACTION_LOC_KEY] = $this->actionLocKey; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
if (is_string($this->locKey)) { |
357
|
|
|
$alert[self::ALERT_LOC_KEY] = $this->locKey; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
if (is_array($this->locArgs)) { |
361
|
|
|
$alert[self::ALERT_LOC_ARGS_KEY] = $this->locArgs; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
if (is_string($this->launchImage)) { |
365
|
|
|
$alert[self::ALERT_LAUNCH_IMAGE_KEY] = $this->launchImage; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
return $alert; |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|