1
|
|
|
<?php |
2
|
|
|
namespace ker0x\Push\Adapter\Fcm\Message; |
3
|
|
|
|
4
|
|
|
use Cake\Utility\Inflector; |
5
|
|
|
use ker0x\Push\Adapter\Fcm\Message\Exception\InvalidNotificationException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Notification |
9
|
|
|
* @package ker0x\Push\Adapter\Fcm\Message |
10
|
|
|
*/ |
11
|
|
|
class Notification |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var null|string |
16
|
|
|
*/ |
17
|
|
|
protected $title; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var null|string |
21
|
|
|
*/ |
22
|
|
|
protected $body; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var null|string |
26
|
|
|
*/ |
27
|
|
|
protected $sound; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var null|string |
31
|
|
|
*/ |
32
|
|
|
protected $badge; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var null|string |
36
|
|
|
*/ |
37
|
|
|
protected $icon; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var null|string |
41
|
|
|
*/ |
42
|
|
|
protected $tag; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var null|string |
46
|
|
|
*/ |
47
|
|
|
protected $color; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var null|string |
51
|
|
|
*/ |
52
|
|
|
protected $clickAction; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var null|string |
56
|
|
|
*/ |
57
|
|
|
protected $bodyLocalizationKey; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var null|string |
61
|
|
|
*/ |
62
|
|
|
protected $bodyLocalizationArgs; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var null|string |
66
|
|
|
*/ |
67
|
|
|
protected $titleLocalizationKey; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var null|string |
71
|
|
|
*/ |
72
|
|
|
protected $titleLocalizationArgs; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Notification constructor. |
76
|
|
|
* @param array|NotificationBuilder $notificationBuilder |
77
|
|
|
*/ |
78
|
|
|
public function __construct($notificationBuilder) |
79
|
|
|
{ |
80
|
|
|
if (is_array($notificationBuilder)) { |
81
|
|
|
$notificationBuilder = $this->fromArray($notificationBuilder); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->title = $notificationBuilder->getTitle(); |
85
|
|
|
$this->body = $notificationBuilder->getBody(); |
86
|
|
|
$this->sound = $notificationBuilder->getSound(); |
87
|
|
|
$this->badge = $notificationBuilder->getBadge(); |
88
|
|
|
$this->icon = $notificationBuilder->getIcon(); |
89
|
|
|
$this->tag = $notificationBuilder->getTag(); |
90
|
|
|
$this->color = $notificationBuilder->getColor(); |
91
|
|
|
$this->clickAction = $notificationBuilder->getClickAction(); |
92
|
|
|
$this->bodyLocalizationKey = $notificationBuilder->getBodyLocalizationKey(); |
93
|
|
|
$this->bodyLocalizationArgs = $notificationBuilder->getBodyLocalizationArgs(); |
94
|
|
|
$this->titleLocalizationKey = $notificationBuilder->getTitleLocalizationKey(); |
95
|
|
|
$this->titleLocalizationArgs = $notificationBuilder->getTitleLocalizationArgs(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
public function build() |
102
|
|
|
{ |
103
|
|
|
$notification = [ |
104
|
|
|
'title' => $this->title, |
105
|
|
|
'body' => $this->body, |
106
|
|
|
'sound' => $this->sound, |
107
|
|
|
'badge' => $this->badge, |
108
|
|
|
'icon' => $this->icon, |
109
|
|
|
'tag' => $this->tag, |
110
|
|
|
'color' => $this->color, |
111
|
|
|
'click_action' => $this->clickAction, |
112
|
|
|
'body_loc_key' => $this->bodyLocalizationKey, |
113
|
|
|
'body_loc_args' => $this->bodyLocalizationArgs, |
114
|
|
|
'title_loc_key' => $this->titleLocalizationKey, |
115
|
|
|
'title_loc_args' => $this->titleLocalizationArgs, |
116
|
|
|
]; |
117
|
|
|
|
118
|
|
|
return array_filter($notification); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param array $notificationArray |
123
|
|
|
* @return \ker0x\Push\Adapter\Fcm\Message\NotificationBuilder |
124
|
|
|
* @throws \ker0x\Push\Adapter\Fcm\Message\Exception\InvalidNotificationException |
125
|
|
|
*/ |
126
|
|
|
private function fromArray(array $notificationArray): NotificationBuilder |
127
|
|
|
{ |
128
|
|
|
if (empty($notificationArray)) { |
129
|
|
|
throw InvalidNotificationException::arrayEmpty(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$notificationBuilder = new NotificationBuilder($notificationArray['title']); |
133
|
|
|
unset($notificationArray['title']); |
134
|
|
|
foreach ($notificationArray as $key => $value) { |
135
|
|
|
$key = Inflector::camelize($key); |
136
|
|
|
$setter = 'set' . $key; |
137
|
|
|
$notificationBuilder->$setter($value); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $notificationBuilder; |
141
|
|
|
} |
142
|
|
|
} |