Completed
Pull Request — master (#88)
by Rasmus
01:09
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
    /**
105
     * @return Payload
106
     */
107
    public static function create(): Payload
108
    {
109
        return new self();
110
    }
111
112
    /**
113
     * Get default payload structure.
114
     *
115
     * @return array
116
     */
117
    private static function getDefaultPayloadStructure()
118
    {
119
        return [self::PAYLOAD_ROOT_KEY => new \stdClass];
120
    }
121
122
    protected function __construct()
123
    {
124
    }
125
126
    /**
127
     * Get Alert.
128
     *
129
     * @return Alert|null
130
     */
131
    public function getAlert()
132
    {
133
        return $this->alert;
134
    }
135
136
    /**
137
     * Set Alert.
138
     *
139
     * @param Alert|string $alert
140
     *
141
     * @return Payload
142
     */
143
    public function setAlert($alert): Payload
144
    {
145
        if ($alert instanceof Alert || is_string($alert)) {
146
            $this->alert = $alert;
147
        }
148
149
        return $this;
150
    }
151
152
    /**
153
     * Get badge.
154
     *
155
     * @return int|null
156
     */
157
    public function getBadge()
158
    {
159
        return $this->badge;
160
    }
161
162
    /**
163
     * Set badge.
164
     *
165
     * @param int $value
166
     *
167
     * @return Payload
168
     */
169
    public function setBadge(int $value): Payload
170
    {
171
        $this->badge = $value;
172
173
        return $this;
174
    }
175
176
    /**
177
     * Get sound.
178
     *
179
     * @return string|null
180
     */
181
    public function getSound()
182
    {
183
        return $this->sound;
184
    }
185
186
    /**
187
     * Set sound.
188
     *
189
     * @param string $value
190
     *
191
     * @return Payload
192
     */
193
    public function setSound(string $value): Payload
194
    {
195
        $this->sound = $value;
196
197
        return $this;
198
    }
199
200
    /**
201
     * Set content availability.
202
     *
203
     * @param bool $value
204
     *
205
     * @return Payload
206
     */
207
    public function setContentAvailability(bool $value): Payload
208
    {
209
        $this->contentAvailable = $value;
210
211
        return $this;
212
    }
213
214
    /**
215
     * Get content availability.
216
     *
217
     * @return bool|null
218
     */
219
    public function isContentAvailable()
220
    {
221
        return $this->contentAvailable;
222
    }
223
224
    /**
225
     * Set the mutable-content key for Notification Service Extensions on iOS10.
226
     *
227
     * @see http://bit.ly/mutable-content
228
     *
229
     * @param bool $value
230
     *
231
     * @return Payload
232
     */
233
    public function setMutableContent(bool $value): Payload
234
    {
235
        $this->mutableContent = $value;
236
237
        return $this;
238
    }
239
240
    /**
241
     * Is content mutable.
242
     *
243
     * @return bool|null
244
     */
245
    public function hasMutableContent()
246
    {
247
        return $this->mutableContent;
248
    }
249
250
    /**
251
     * Get category.
252
     *
253
     * @return string|null
254
     */
255
    public function getCategory()
256
    {
257
        return $this->category;
258
    }
259
260
    /**
261
     * Set category.
262
     *
263
     * @param string $value
264
     *
265
     * @return Payload
266
     */
267
    public function setCategory(string $value): Payload
268
    {
269
        $this->category = $value;
270
271
        return $this;
272
    }
273
274
    /**
275
     * Get thread-id.
276
     *
277
     * @return string|null
278
     */
279
    public function getThreadId()
280
    {
281
        return $this->threadId;
282
    }
283
284
    /**
285
     * Set thread-id.
286
     *
287
     * @param string $value
288
     *
289
     * @return Payload
290
     */
291
    public function setThreadId(string $value): Payload
292
    {
293
        $this->threadId = $value;
294
295
        return $this;
296
    }
297
298
    /**
299
     * Set custom value for Payload.
300
     *
301
     * @param string $key
302
     * @param mixed  $value
303
     *
304
     * @return Payload
305
     * @throws InvalidPayloadException
306
     */
307
    public function setCustomValue(string $key, $value): Payload
308
    {
309
        if ($key === self::PAYLOAD_ROOT_KEY) {
310
            throw InvalidPayloadException::reservedKey();
311
        }
312
313
        $this->customValues[$key] = $value;
314
315
        return $this;
316
    }
317
318
    /**
319
     * Get custom value.
320
     *
321
     * @param $key
322
     *
323
     * @return mixed
324
     * @throws InvalidPayloadException
325
     */
326
    public function getCustomValue($key)
327
    {
328
        if (!array_key_exists($key, $this->customValues)) {
329
            throw InvalidPayloadException::notExistingCustomValue($key);
330
        }
331
332
        return $this->customValues[$key];
333
    }
334
335
    /**
336
     * Convert Payload to JSON.
337
     *
338
     * @return string
339
     */
340
    public function toJson(): string
341
    {
342
        return json_encode($this, JSON_UNESCAPED_UNICODE);
343
    }
344
345
    /**
346
     * Specify data which should be serialized to JSON.
347
     *
348
     * @return array
349
     * @link   http://php.net/manual/en/jsonserializable.jsonserialize.php
350
     */
351
    public function jsonSerialize()
352
    {
353
        $payload = self::getDefaultPayloadStructure();
354
355
        if ($this->alert instanceof Alert || is_string($this->alert)) {
356
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_ALERT_KEY} = $this->alert;
357
        }
358
359
        if (is_int($this->badge)) {
360
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_BADGE_KEY} = $this->badge;
361
        }
362
363
        if (is_string($this->sound)) {
364
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_SOUND_KEY} = $this->sound;
365
        }
366
367
        if (is_bool($this->contentAvailable)) {
368
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_CONTENT_AVAILABLE_KEY} = (int)$this->contentAvailable;
369
        }
370
371
        if (is_bool($this->mutableContent)) {
372
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_MUTABLE_CONTENT_KEY} = (int)$this->mutableContent;
373
        }
374
375
        if (is_string($this->category)) {
376
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_CATEGORY_KEY} = $this->category;
377
        }
378
379
        if (is_string($this->threadId)) {
380
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_THREAD_ID_KEY} = $this->threadId;
381
        }
382
383
        if (is_countable($this->customValues) && count($this->customValues)) {
384
            $payload = array_merge($payload, $this->customValues);
385
        }
386
387
        return $payload;
388
    }
389
}
390