1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the AppleApnPush package |
7
|
|
|
* |
8
|
|
|
* (c) Vitaliy Zhuk <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Apple\ApnPush\Encoder; |
15
|
|
|
|
16
|
|
|
use Apple\ApnPush\Model\Alert; |
17
|
|
|
use Apple\ApnPush\Model\Aps; |
18
|
|
|
use Apple\ApnPush\Model\Payload; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The encoder for encode notification payload to string for next send to Apple Push Notification Service |
22
|
|
|
*/ |
23
|
|
|
class PayloadEncoder implements PayloadEncoderInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function encode(Payload $payload): string |
29
|
|
|
{ |
30
|
|
|
$data = [ |
31
|
|
|
'aps' => $this->convertApsToArray($payload->getAps()), |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
$data = array_merge($payload->getCustomData(), $data); |
35
|
|
|
|
36
|
|
|
return json_encode($data); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Convert APS data to array |
41
|
|
|
* |
42
|
|
|
* @param Aps $aps |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
private function convertApsToArray(Aps $aps): array |
47
|
|
|
{ |
48
|
|
|
$data = [ |
49
|
|
|
'alert' => $this->convertAlertToArray($aps->getAlert()), |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
if ($aps->getSound()) { |
53
|
|
|
$data['sound'] = $aps->getSound(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($aps->getBadge()) { |
57
|
|
|
$data['badge'] = $aps->getBadge(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($aps->getCategory()) { |
61
|
|
|
$data['category'] = $aps->getCategory(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($aps->isContentAvailable()) { |
65
|
|
|
$data['content-available'] = 1; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($aps->isMutableContent()) { |
69
|
|
|
$data['mutable-content'] = 1; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($aps->getThreadId()) { |
73
|
|
|
$data['thread-id'] = $aps->getThreadId(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $data; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Convert alert object to array |
81
|
|
|
* |
82
|
|
|
* @param Alert $alert |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
private function convertAlertToArray(Alert $alert): array |
87
|
|
|
{ |
88
|
|
|
$data = []; |
89
|
|
|
|
90
|
|
|
if ($alert->getBodyLocalized()->getKey()) { |
91
|
|
|
$data['loc-key'] = $alert->getBodyLocalized()->getKey(); |
92
|
|
|
$data['loc-args'] = $alert->getBodyLocalized()->getArgs(); |
93
|
|
|
} else { |
94
|
|
|
$data['body'] = $alert->getBody(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if ($alert->getTitleLocalized()->getKey()) { |
98
|
|
|
$data['title-loc-key'] = $alert->getTitleLocalized()->getKey(); |
99
|
|
|
$data['title-loc-args'] = $alert->getTitleLocalized()->getArgs(); |
100
|
|
|
} elseif ($alert->getTitle()) { |
101
|
|
|
$data['title'] = $alert->getTitle(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($alert->getActionLocalized()->getKey()) { |
105
|
|
|
$data['action-loc-key'] = $alert->getActionLocalized()->getKey(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($alert->getLaunchImage()) { |
109
|
|
|
$data['launch-image'] = $alert->getLaunchImage(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $data; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|