|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\OneSignal; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use NotificationChannels\OneSignal\Traits\OneSignalHelpers; |
|
7
|
|
|
|
|
8
|
|
|
class OneSignalMessage |
|
9
|
|
|
{ |
|
10
|
|
|
use OneSignalHelpers; |
|
11
|
|
|
/** @var string */ |
|
12
|
|
|
protected $body; |
|
13
|
|
|
|
|
14
|
|
|
/** @var string */ |
|
15
|
|
|
protected $subject; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
protected $url; |
|
19
|
|
|
|
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
protected $icon; |
|
22
|
|
|
|
|
23
|
|
|
/** @var array */ |
|
24
|
|
|
protected $data = []; |
|
25
|
|
|
|
|
26
|
|
|
/** @var array */ |
|
27
|
|
|
protected $buttons = []; |
|
28
|
|
|
|
|
29
|
|
|
/** @var array */ |
|
30
|
|
|
protected $webButtons = []; |
|
31
|
|
|
|
|
32
|
|
|
/** @var array */ |
|
33
|
|
|
protected $extraParameters = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $body |
|
37
|
|
|
* |
|
38
|
|
|
* @return static |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public static function create($body = '') |
|
41
|
|
|
{ |
|
42
|
1 |
|
return new static($body); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $body |
|
47
|
|
|
*/ |
|
48
|
20 |
|
public function __construct($body = '') |
|
49
|
|
|
{ |
|
50
|
20 |
|
$this->body = $body; |
|
51
|
20 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Set the message body. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $value |
|
57
|
|
|
* |
|
58
|
|
|
* @return $this |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function body($value) |
|
61
|
|
|
{ |
|
62
|
1 |
|
$this->body = $value; |
|
63
|
|
|
|
|
64
|
1 |
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Set the message icon. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $value |
|
71
|
|
|
* |
|
72
|
|
|
* @return $this |
|
73
|
|
|
*/ |
|
74
|
6 |
|
public function icon($value) |
|
75
|
|
|
{ |
|
76
|
6 |
|
$this->icon = $value; |
|
77
|
|
|
|
|
78
|
6 |
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Set the message subject. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $value |
|
85
|
|
|
* |
|
86
|
|
|
* @return $this |
|
87
|
|
|
*/ |
|
88
|
6 |
|
public function subject($value) |
|
89
|
|
|
{ |
|
90
|
6 |
|
$this->subject = $value; |
|
91
|
|
|
|
|
92
|
6 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Set the message url. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $value |
|
99
|
|
|
* |
|
100
|
|
|
* @return $this |
|
101
|
|
|
*/ |
|
102
|
6 |
|
public function url($value) |
|
103
|
|
|
{ |
|
104
|
6 |
|
$this->url = $value; |
|
105
|
|
|
|
|
106
|
6 |
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Set additional data. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $key |
|
114
|
|
|
* @param string $value |
|
115
|
|
|
* |
|
116
|
|
|
* @return $this |
|
117
|
|
|
*/ |
|
118
|
1 |
|
public function setData($key, $value) |
|
119
|
|
|
{ |
|
120
|
1 |
|
$this->data[$key] = $value; |
|
121
|
|
|
|
|
122
|
1 |
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Set additional parameters. |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $key |
|
129
|
|
|
* @param string $value |
|
130
|
|
|
* |
|
131
|
|
|
* @return $this |
|
132
|
|
|
*/ |
|
133
|
5 |
|
public function setParameter($key, $value) |
|
134
|
|
|
{ |
|
135
|
5 |
|
$this->extraParameters[$key] = $value; |
|
136
|
|
|
|
|
137
|
5 |
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Add a web button to the message. |
|
142
|
|
|
* |
|
143
|
|
|
* @param OneSignalWebButton $button |
|
144
|
|
|
* |
|
145
|
|
|
* @return $this |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public function webButton(OneSignalWebButton $button) |
|
148
|
|
|
{ |
|
149
|
1 |
|
$this->webButtons[] = $button->toArray(); |
|
150
|
|
|
|
|
151
|
1 |
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Add a native button to the message. |
|
156
|
|
|
* |
|
157
|
|
|
* @param OneSignalButton $button |
|
158
|
|
|
* |
|
159
|
|
|
* @return $this |
|
160
|
|
|
*/ |
|
161
|
1 |
|
public function button(OneSignalButton $button) |
|
162
|
|
|
{ |
|
163
|
1 |
|
$this->buttons[] = $button->toArray(); |
|
164
|
|
|
|
|
165
|
1 |
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return array |
|
170
|
|
|
*/ |
|
171
|
20 |
|
public function toArray() |
|
172
|
|
|
{ |
|
173
|
|
|
$message = [ |
|
174
|
20 |
|
'contents' => ['en' => $this->body], |
|
175
|
20 |
|
'headings' => $this->subjectToArray(), |
|
176
|
20 |
|
'url' => $this->url, |
|
177
|
20 |
|
'buttons' => $this->buttons, |
|
178
|
20 |
|
'web_buttons' => $this->webButtons, |
|
179
|
20 |
|
'chrome_web_icon' => $this->icon, |
|
180
|
20 |
|
'chrome_icon' => $this->icon, |
|
181
|
20 |
|
'adm_small_icon' => $this->icon, |
|
182
|
20 |
|
'small_icon' => $this->icon, |
|
183
|
|
|
]; |
|
184
|
|
|
|
|
185
|
20 |
|
foreach ($this->extraParameters as $key => $value) { |
|
186
|
5 |
|
Arr::set($message, $key, $value); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
20 |
|
foreach ($this->data as $data => $value) { |
|
190
|
1 |
|
Arr::set($message, 'data.'.$data, $value); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
20 |
|
return $message; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @return array |
|
198
|
|
|
*/ |
|
199
|
20 |
|
protected function subjectToArray() |
|
200
|
|
|
{ |
|
201
|
20 |
|
if ($this->subject === null) { |
|
202
|
14 |
|
return []; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
6 |
|
return ['en' => $this->subject]; |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|