1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Facebook; |
4
|
|
|
|
5
|
|
|
use NotificationChannels\Facebook\Exceptions\CouldNotCreateMessage; |
6
|
|
|
use NotificationChannels\Facebook\Exceptions\CouldNotSendNotification; |
7
|
|
|
use NotificationChannels\Facebook\Enums\AttachmentType; |
8
|
|
|
use NotificationChannels\Facebook\Enums\NotificationType; |
9
|
|
|
use NotificationChannels\Facebook\Traits\ButtonsTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class FacebookMessage. |
13
|
|
|
*/ |
14
|
|
|
class FacebookMessage implements \JsonSerializable |
15
|
|
|
{ |
16
|
|
|
use ButtonsTrait; |
17
|
|
|
|
18
|
|
|
/** @var string Recipient's ID. */ |
19
|
|
|
public $recipient; |
20
|
|
|
|
21
|
|
|
/** @var string Notification Text. */ |
22
|
|
|
public $text; |
23
|
|
|
|
24
|
|
|
/** @var string Notification Type */ |
25
|
|
|
public $notificationType = 'REGULAR'; |
26
|
|
|
|
27
|
|
|
/** @var array Generic Template Cards (items) */ |
28
|
|
|
public $cards = []; |
29
|
|
|
|
30
|
|
|
/** @var string Notification Type */ |
31
|
|
|
public $notification_type = NotificationType::REGULAR; |
32
|
|
|
|
33
|
|
|
/** @var string Attachment Type |
34
|
|
|
* Defaults to File |
35
|
|
|
*/ |
36
|
|
|
public $attachment_type = AttachmentType::FILE; |
37
|
|
|
|
38
|
|
|
/** @var string Attachment URL */ |
39
|
|
|
public $attachment_url; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
protected $has_attachment = false; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
protected $has_text = false; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $text |
53
|
|
|
* |
54
|
|
|
* @return static |
55
|
|
|
*/ |
56
|
|
|
public static function create($text = '') |
57
|
|
|
{ |
58
|
|
|
return new static($text); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $text |
63
|
|
|
*/ |
64
|
|
|
public function __construct($text = '') |
65
|
|
|
{ |
66
|
|
|
if ($text != '') { |
67
|
|
|
$this->text($text); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Recipient's PSID or Phone Number. |
73
|
|
|
* |
74
|
|
|
* The id must be an ID that was retrieved through the |
75
|
|
|
* Messenger entry points or through the Messenger webhooks. |
76
|
|
|
* |
77
|
|
|
* @param $recipient ID of recipient or Phone number of the recipient |
78
|
|
|
* with the format +1(212)555-2368 |
79
|
|
|
* |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
|
|
public function to($recipient) |
83
|
|
|
{ |
84
|
|
|
$this->recipient = $recipient; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Notification text. |
91
|
|
|
* |
92
|
|
|
* @param $text |
93
|
|
|
* @throws CouldNotCreateMessage |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function text($text) |
98
|
|
|
{ |
99
|
|
|
if (!mb_strlen($text) > 320) { |
100
|
|
|
$this->text = $text; |
101
|
|
|
} else { |
102
|
|
|
throw CouldNotCreateMessage::textTooLong(); |
103
|
|
|
} |
104
|
|
|
$this->has_text = true; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Add Attachment. |
111
|
|
|
* |
112
|
|
|
* @param $attachment_type |
113
|
|
|
* @param $url |
114
|
|
|
* @throws CouldNotCreateMessage |
115
|
|
|
* |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
|
|
public function attach($attachment_type, $url) |
119
|
|
|
{ |
120
|
|
|
$attachment_types = [AttachmentType::FILE, AttachmentType::IMAGE, AttachmentType::VIDEO, AttachmentType::AUDIO]; |
121
|
|
|
if (in_array($attachment_type, $attachment_types)) { |
122
|
|
|
$this->notificationType = $attachment_type; |
123
|
|
|
} else { |
124
|
|
|
throw CouldNotCreateMessage::invalidAttachmentType(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
if (isset($url)) { |
129
|
|
|
$this->attachment_url = $url; |
130
|
|
|
} else { |
131
|
|
|
throw CouldNotCreateMessage::urlNotProvided(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->has_attachment = true; |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Push notification type. |
141
|
|
|
* |
142
|
|
|
* @param string $notificationType Possible values: REGULAR, SILENT_PUSH, NO_PUSH |
143
|
|
|
* |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
|
|
public function notificationType($notificationType = 'REGULAR') |
147
|
|
|
{ |
148
|
|
|
$this->notificationType = $notificationType; |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Add up to 3 call to action buttons. |
155
|
|
|
* |
156
|
|
|
* @param array $buttons |
|
|
|
|
157
|
|
|
* |
158
|
|
|
* @return $this |
159
|
|
|
* @throws CouldNotSendNotification |
160
|
|
|
*/ |
161
|
|
|
public function cards(array $cards = []) |
162
|
|
|
{ |
163
|
|
|
if (count($cards) > 10) { |
164
|
|
|
throw CouldNotCreateMessage::messageCardsLimitExceeded(); |
165
|
|
|
} |
166
|
|
|
$this->cards = $cards; |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Determine if user id is not given. |
173
|
|
|
* |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
|
|
public function toNotGiven() |
177
|
|
|
{ |
178
|
|
|
return !isset($this->recipient); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Convert the object into something JSON serializable. |
183
|
|
|
* |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
|
|
public function jsonSerialize() |
187
|
|
|
{ |
188
|
|
|
return $this->toArray(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Returns message payload for JSON conversion. |
193
|
|
|
* @throws CouldNotCreateMessage |
194
|
|
|
* @return array |
195
|
|
|
*/ |
196
|
|
|
public function toArray() |
197
|
|
|
{ |
198
|
|
|
if ($this->has_attachment) { |
199
|
|
|
return $this->attachmentMessageToArray(); |
200
|
|
|
} |
201
|
|
|
if ($this->has_text) { |
202
|
|
|
//check if has buttons |
203
|
|
|
if (count($this->buttons) > 0) { |
204
|
|
|
return $this->buttonMessageToArray(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $this->textMessageToArray(); |
208
|
|
|
} |
209
|
|
|
if (count($this->cards) > 0) { |
210
|
|
|
return $this->genericMessageToArray(); |
211
|
|
|
} |
212
|
|
|
throw CouldNotCreateMessage::dataNotProvided(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Returns message for simple text message. |
217
|
|
|
* @return array |
218
|
|
|
*/ |
219
|
|
|
protected function textMessageToArray() |
220
|
|
|
{ |
221
|
|
|
$message = []; |
222
|
|
|
$message['recipient'] = $this->recipient; |
223
|
|
|
$message['notification_type'] = $this->notificationType; |
224
|
|
|
$message['message']['text'] = $this->text; |
225
|
|
|
|
226
|
|
|
return $message; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Returns message for attachment message. |
231
|
|
|
* @return array |
232
|
|
|
*/ |
233
|
|
|
protected function attachmentMessageToArray() |
234
|
|
|
{ |
235
|
|
|
$message = []; |
236
|
|
|
$message['recipient'] = $this->recipient; |
237
|
|
|
$message['notification_type'] = $this->notificationType; |
238
|
|
|
$message['message']['attachment']['type'] = $this->attachment_type; |
239
|
|
|
$message['message']['attachment']['payload']['url'] = $this->attachment_url; |
240
|
|
|
|
241
|
|
|
return $message; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Returns message for Generic Template message. |
246
|
|
|
* @return array |
247
|
|
|
*/ |
248
|
|
|
protected function genericMessageToArray() |
249
|
|
|
{ |
250
|
|
|
$message = []; |
251
|
|
|
$message['recipient'] = $this->recipient; |
252
|
|
|
$message['notification_type'] = $this->notificationType; |
253
|
|
|
$message['message']['attachment']['type'] = 'template'; |
254
|
|
|
$message['message']['attachment']['payload']['template_type'] = 'generic'; |
255
|
|
|
$message['message']['attachment']['payload']['elements'] = $this->cards; |
256
|
|
|
|
257
|
|
|
return $message; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Returns message for Button Template message. |
262
|
|
|
* @return array |
263
|
|
|
*/ |
264
|
|
|
protected function buttonMessageToArray() |
265
|
|
|
{ |
266
|
|
|
$message = []; |
267
|
|
|
$message['recipient'] = $this->recipient; |
268
|
|
|
$message['notification_type'] = $this->notificationType; |
269
|
|
|
$message['message']['attachment']['type'] = 'template'; |
270
|
|
|
$message['message']['attachment']['payload']['template_type'] = 'button'; |
271
|
|
|
$message['message']['attachment']['payload']['text'] = $this->text; |
272
|
|
|
$message['message']['attachment']['payload']['buttons'] = $this->buttons; |
273
|
|
|
|
274
|
|
|
return $message; |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.