Completed
Push — master ( a84a75...a58ae8 )
by Arthur
01:12
created

Payload.php ➔ is_countable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 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;
13
14
use Countable;
15
use Pushok\Payload\Alert;
16
17
// Polyfill for PHP 7.2
18
if (!function_exists('is_countable')) {
19
    function is_countable($var)
20
    {
21
        return (is_array($var) || $var instanceof Countable);
22
    }
23
}
24
25
/**
26
 * Class Payload
27
 *
28
 * @package Pushok
29
 *
30
 * @see     http://bit.ly/payload-key-reference
31
 */
32
class Payload implements \JsonSerializable
33
{
34
    const PAYLOAD_ROOT_KEY = 'aps';
35
    const PAYLOAD_ALERT_KEY = 'alert';
36
    const PAYLOAD_BADGE_KEY = 'badge';
37
    const PAYLOAD_SOUND_KEY = 'sound';
38
    const PAYLOAD_CONTENT_AVAILABLE_KEY = 'content-available';
39
    const PAYLOAD_MUTABLE_CONTENT_KEY = 'mutable-content';
40
    const PAYLOAD_CATEGORY_KEY = 'category';
41
    const PAYLOAD_THREAD_ID_KEY = 'thread-id';
42
43
    const PAYLOAD_HTTP2_REGULAR_NOTIFICATION_MAXIMUM_SIZE = 4096;
44
    const PAYLOAD_HTTP2_VOIP_NOTIFICATION_MAXIMUM_SIZE = 5120;
45
    const PAYLOAD_BINARY_REGULAR_NOTIFICATION_MAXIMUM_SIZE = 2048;
46
47
    /**
48
     * The notification settings for your app on the user’s device determine whether an alert or banner is displayed.
49
     *
50
     * @var Alert|string
51
     */
52
    private $alert;
53
54
    /**
55
     * The number to display as the badge of the app icon.
56
     * If this property is absent, the badge is not changed.
57
     *
58
     * @var int
59
     */
60
    private $badge;
61
62
    /**
63
     * The name of a sound file in the app bundle or in the Library/Sounds folder of the app’s data container.
64
     *
65
     * @var string
66
     */
67
    private $sound;
68
69
    /**
70
     * Include this key with a value of true to configure a silent notification.
71
     *
72
     * @var bool
73
     */
74
    private $contentAvailable;
75
76
    /**
77
     * Include this key with a value of true to configure a mutable content notification.
78
     *
79
     * @var bool
80
     */
81
    private $mutableContent;
82
83
    /**
84
     * Provide this key with a string value that represents the notification’s type.
85
     *
86
     * @var string
87
     */
88
    private $category;
89
90
    /**
91
     * Provide this key with a string value that represents the app-specific identifier for grouping notifications.
92
     *
93
     * @var string
94
     */
95
    private $threadId;
96
97
    /**
98
     * Payload custom values.
99
     *
100
     * @var array
101
     */
102
    private $customValues;
103
104
    protected function __construct()
105
    {
106
    }
107
108
    /**
109
     * @return Payload
110
     */
111
    public static function create(): Payload
112
    {
113
        return new self();
114
    }
115
116
    /**
117
     * Get Alert.
118
     *
119
     * @return Alert|null
120
     */
121
    public function getAlert()
122
    {
123
        return $this->alert;
124
    }
125
126
    /**
127
     * Set Alert.
128
     *
129
     * @param Alert|string $alert
130
     *
131
     * @return Payload
132
     */
133
    public function setAlert($alert): Payload
134
    {
135
        if ($alert instanceof Alert || is_string($alert)) {
136
            $this->alert = $alert;
137
        }
138
139
        return $this;
140
    }
141
142
    /**
143
     * Get badge.
144
     *
145
     * @return int|null
146
     */
147
    public function getBadge()
148
    {
149
        return $this->badge;
150
    }
151
152
    /**
153
     * Set badge.
154
     *
155
     * @param int $value
156
     *
157
     * @return Payload
158
     */
159
    public function setBadge(int $value): Payload
160
    {
161
        $this->badge = $value;
162
163
        return $this;
164
    }
165
166
    /**
167
     * Get sound.
168
     *
169
     * @return string|null
170
     */
171
    public function getSound()
172
    {
173
        return $this->sound;
174
    }
175
176
    /**
177
     * Set sound.
178
     *
179
     * @param string $value
180
     *
181
     * @return Payload
182
     */
183
    public function setSound(string $value): Payload
184
    {
185
        $this->sound = $value;
186
187
        return $this;
188
    }
189
190
    /**
191
     * Set content availability.
192
     *
193
     * @param bool $value
194
     *
195
     * @return Payload
196
     */
197
    public function setContentAvailability(bool $value): Payload
198
    {
199
        $this->contentAvailable = $value;
200
201
        return $this;
202
    }
203
204
    /**
205
     * Get content availability.
206
     *
207
     * @return bool|null
208
     */
209
    public function isContentAvailable()
210
    {
211
        return $this->contentAvailable;
212
    }
213
214
    /**
215
     * Set the mutable-content key for Notification Service Extensions on iOS10.
216
     *
217
     * @see http://bit.ly/mutable-content
218
     *
219
     * @param bool $value
220
     *
221
     * @return Payload
222
     */
223
    public function setMutableContent(bool $value): Payload
224
    {
225
        $this->mutableContent = $value;
226
227
        return $this;
228
    }
229
230
    /**
231
     * Is content mutable.
232
     *
233
     * @return bool|null
234
     */
235
    public function hasMutableContent()
236
    {
237
        return $this->mutableContent;
238
    }
239
240
    /**
241
     * Get category.
242
     *
243
     * @return string|null
244
     */
245
    public function getCategory()
246
    {
247
        return $this->category;
248
    }
249
250
    /**
251
     * Set category.
252
     *
253
     * @param string $value
254
     *
255
     * @return Payload
256
     */
257
    public function setCategory(string $value): Payload
258
    {
259
        $this->category = $value;
260
261
        return $this;
262
    }
263
264
    /**
265
     * Get thread-id.
266
     *
267
     * @return string|null
268
     */
269
    public function getThreadId()
270
    {
271
        return $this->threadId;
272
    }
273
274
    /**
275
     * Set thread-id.
276
     *
277
     * @param string $value
278
     *
279
     * @return Payload
280
     */
281
    public function setThreadId(string $value): Payload
282
    {
283
        $this->threadId = $value;
284
285
        return $this;
286
    }
287
288
    /**
289
     * Set custom value for Payload.
290
     *
291
     * @param string $key
292
     * @param mixed $value
293
     *
294
     * @return Payload
295
     * @throws InvalidPayloadException
296
     */
297
    public function setCustomValue(string $key, $value): Payload
298
    {
299
        if ($key === self::PAYLOAD_ROOT_KEY) {
300
            throw InvalidPayloadException::reservedKey();
301
        }
302
303
        $this->customValues[$key] = $value;
304
305
        return $this;
306
    }
307
308
    /**
309
     * Get custom value.
310
     *
311
     * @param $key
312
     *
313
     * @return mixed
314
     * @throws InvalidPayloadException
315
     */
316
    public function getCustomValue($key)
317
    {
318
        if (!array_key_exists($key, $this->customValues)) {
319
            throw InvalidPayloadException::notExistingCustomValue($key);
320
        }
321
322
        return $this->customValues[$key];
323
    }
324
325
    /**
326
     * Convert Payload to JSON.
327
     *
328
     * @return string
329
     */
330
    public function toJson(): string
331
    {
332
        return json_encode($this, JSON_UNESCAPED_UNICODE);
333
    }
334
335
    /**
336
     * Specify data which should be serialized to JSON.
337
     *
338
     * @return array
339
     * @link   http://php.net/manual/en/jsonserializable.jsonserialize.php
340
     */
341
    public function jsonSerialize()
342
    {
343
        $payload = self::getDefaultPayloadStructure();
344
345
        if ($this->alert instanceof Alert || is_string($this->alert)) {
346
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_ALERT_KEY} = $this->alert;
347
        }
348
349
        if (is_int($this->badge)) {
350
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_BADGE_KEY} = $this->badge;
351
        }
352
353
        if (is_string($this->sound)) {
354
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_SOUND_KEY} = $this->sound;
355
        }
356
357
        if (is_bool($this->contentAvailable)) {
358
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_CONTENT_AVAILABLE_KEY} = (int)$this->contentAvailable;
359
        }
360
361
        if (is_bool($this->mutableContent)) {
362
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_MUTABLE_CONTENT_KEY} = (int)$this->mutableContent;
363
        }
364
365
        if (is_string($this->category)) {
366
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_CATEGORY_KEY} = $this->category;
367
        }
368
369
        if (is_string($this->threadId)) {
370
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_THREAD_ID_KEY} = $this->threadId;
371
        }
372
373
        if (is_countable($this->customValues) && count($this->customValues)) {
374
            $payload = array_merge($payload, $this->customValues);
375
        }
376
377
        return $payload;
378
    }
379
380
    /**
381
     * Get default payload structure.
382
     *
383
     * @return array
384
     */
385
    private static function getDefaultPayloadStructure()
386
    {
387
        return [self::PAYLOAD_ROOT_KEY => new \stdClass];
388
    }
389
}
390