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