Completed
Push — master ( e2ccd6...cb7aaa )
by Arthur
02:40
created

Alert::toJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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_BODY_KEY = 'body';
25
    const ALERT_TITLE_LOC_KEY = 'title-loc-key';
26
    const ALERT_TITLE_LOC_ARGS_KEY = 'title-loc-args';
27
    const ALERT_ACTION_LOC_KEY = 'action-loc-key';
28
    const ALERT_LOC_KEY = 'loc-key';
29
    const ALERT_LOC_ARGS_KEY = 'loc-args';
30
    const ALERT_LAUNCH_IMAGE_KEY = 'launch-image';
31
    const ALERT_MUTABLE_CONTENT_KEY = 'mutable-content';
32
33
    /**
34
     * A short string describing the purpose of the notification.
35
     *
36
     * @var string
37
     */
38
    private $title;
39
40
    /**
41
     * The text of the alert message.
42
     *
43
     * @var string
44
     */
45
    private $body;
46
47
    /**
48
     * The key to a title string in the Localizable.strings file for the current localization.
49
     *
50
     * @var string|null
51
     */
52
    private $titleLocKey;
53
54
    /**
55
     * Variable string values to appear in place of the format specifiers in title-loc-key.
56
     *
57
     * @var string[]|null
58
     */
59
    private $titleLocArgs;
60
61
    /**
62
     * If a string is specified, the iOS system displays an alert that includes the Close and View buttons.
63
     *
64
     * @var string|null
65
     */
66
    private $actionLocKey;
67
68
    /**
69
     * A key to an alert-message string in a Localizable.strings file for the current localization.
70
     *
71
     * @var string
72
     */
73
    private $locKey;
74
75
    /**
76
     * Variable string values to appear in place of the format specifiers in loc-key.
77
     *
78
     * @var string[]
79
     */
80
    private $locArgs;
81
82
    /**
83
     * The filename of an image file in the app bundle, with or without the filename extension.
84
     *
85
     * @var string
86
     */
87
    private $launchImage;
88
89
    /**
90
     * Access to modify the content of remote notifications before they are delivered to the user.
91
     *
92
     * @var bool
93
     */
94
    private $mutableContent;
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 $this
110
     */
111
    public function setTitle(string $value)
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 body.
130
     *
131
     * @param string $value
132
     * @return $this
133
     */
134
    public function setBody(string $value)
135
    {
136
        $this->body = $value;
137
138
        return $this;
139
    }
140
141
    /**
142
     * Get Alert body.
143
     *
144
     * @return string
145
     */
146
    public function getBody()
147
    {
148
        return $this->body;
149
    }
150
151
    /**
152
     * Set title-loc-key.
153
     *
154
     * @param string|null $value
155
     * @return $this
156
     */
157
    public function setTitleLocKey(string $value = null)
158
    {
159
        $this->titleLocKey = $value;
160
161
        return $this;
162
    }
163
164
    /**
165
     * Get title-loc-key.
166
     *
167
     * @return string
168
     */
169
    public function getTitleLocKey()
170
    {
171
        return $this->titleLocKey;
172
    }
173
174
    /**
175
     * Set title-loc-args.
176
     *
177
     * @param array|null $value
178
     * @return $this
179
     */
180
    public function setTitleLocArgs(array $value = null)
181
    {
182
        $this->titleLocArgs = $value;
183
184
        return $this;
185
    }
186
187
    /**
188
     * Get title-loc-args.
189
     *
190
     * @return string[]|null
191
     */
192
    public function getTitleLocArgs()
193
    {
194
        return $this->titleLocArgs;
195
    }
196
197
    /**
198
     * Set action-loc-key.
199
     *
200
     * @param string|null $value
201
     * @return $this
202
     */
203
    public function setActionLocKey(string $value = null)
204
    {
205
        $this->actionLocKey = $value;
206
207
        return $this;
208
    }
209
210
    /**
211
     * Get action-loc-key.
212
     *
213
     * @return string|null
214
     */
215
    public function getActionLocKey()
216
    {
217
        return $this->actionLocKey;
218
    }
219
220
    /**
221
     * Set loc-key.
222
     *
223
     * @param string $value
224
     * @return $this
225
     */
226
    public function setLocKey(string $value)
227
    {
228
        $this->locKey = $value;
229
230
        return $this;
231
    }
232
233
    /**
234
     * Get loc-key.
235
     *
236
     * @return string
237
     */
238
    public function getLocKey()
239
    {
240
        return $this->locKey;
241
    }
242
243
    /**
244
     * Set loc-args.
245
     *
246
     * @param array $value
247
     * @return $this
248
     */
249
    public function setLocArgs(array $value)
250
    {
251
        $this->locArgs = $value;
252
253
        return $this;
254
    }
255
256
    /**
257
     * Get loc-args.
258
     *
259
     * @return string[]
260
     */
261
    public function getLocArgs()
262
    {
263
        return $this->locArgs;
264
    }
265
266
    /**
267
     * Set launch-image.
268
     *
269
     * @param string $value
270
     * @return $this
271
     */
272
    public function setLaunchImage(string $value)
273
    {
274
        $this->launchImage = $value;
275
276
        return $this;
277
    }
278
279
    /**
280
     * Get launch-image.
281
     *
282
     * @return string
283
     */
284
    public function getLaunchImage()
285
    {
286
        return $this->launchImage;
287
    }
288
289
    /**
290
     * Set the mutable-content key for Notification Service Extensions on iOS10.
291
     * @see http://bit.ly/mutable-content
292
     *
293
     * @param bool $value
294
     * @return $this
295
     */
296
    public function setMutableContent(bool $value)
297
    {
298
        $this->mutableContent = $value;
299
300
        return $this;
301
    }
302
303
    /**
304
     * Is content mutable.
305
     *
306
     * @return bool|null
307
     */
308
    public function hasMutableContent()
309
    {
310
        return $this->mutableContent;
311
    }
312
313
    /**
314
     * Convert Alert to JSON.
315
     *
316
     * @return string
317
     */
318
    public function toJson(): string
319
    {
320
        return json_encode($this, JSON_UNESCAPED_UNICODE);
321
    }
322
323
    /**
324
     * Specify data which should be serialized to JSON.
325
     *
326
     * @return array
327
     * @link   http://php.net/manual/en/jsonserializable.jsonserialize.php
328
     */
329
    public function jsonSerialize()
330
    {
331
        $alert = [];
332
333
        if (is_string($this->title)) {
334
            $alert[self::ALERT_TITLE_KEY] = $this->title;
335
        }
336
337
        if (is_string($this->body)) {
338
            $alert[self::ALERT_BODY_KEY] = $this->body;
339
        }
340
341
        if (is_string($this->titleLocKey)) {
342
            $alert[self::ALERT_TITLE_LOC_KEY] = $this->titleLocKey;
343
        }
344
345
        if (is_array($this->titleLocArgs)) {
346
            $alert[self::ALERT_TITLE_LOC_ARGS_KEY] = $this->titleLocArgs;
347
        }
348
349
        if (is_string($this->actionLocKey)) {
350
            $alert[self::ALERT_ACTION_LOC_KEY] = $this->actionLocKey;
351
        }
352
353
        if (is_string($this->locKey)) {
354
            $alert[self::ALERT_LOC_KEY] = $this->locKey;
355
        }
356
357
        if (is_array($this->locArgs)) {
358
            $alert[self::ALERT_LOC_ARGS_KEY] = $this->locArgs;
359
        }
360
361
        if (is_string($this->launchImage)) {
362
            $alert[self::ALERT_LAUNCH_IMAGE_KEY] = $this->launchImage;
363
        }
364
365
        if (is_bool($this->mutableContent)) {
366
            $alert[self::ALERT_MUTABLE_CONTENT_KEY] = (int)$this->mutableContent;
367
        }
368
369
        return $alert;
370
    }
371
}
372