|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\Facebook\Components; |
|
4
|
|
|
|
|
5
|
|
|
use NotificationChannels\Facebook\Enums\ButtonType; |
|
6
|
|
|
use NotificationChannels\Facebook\Exceptions\CouldNotCreateButton; |
|
7
|
|
|
|
|
8
|
|
|
class Button implements \JsonSerializable |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var string Button Title */ |
|
11
|
|
|
protected $title; |
|
12
|
|
|
|
|
13
|
|
|
/** @var string Button Type */ |
|
14
|
|
|
protected $type; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string|array Button URL, Postback Data or Phone Number */ |
|
17
|
|
|
protected $data; |
|
18
|
|
|
|
|
19
|
|
|
/** @var array Payload */ |
|
20
|
|
|
protected $payload = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Create a button. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $title |
|
26
|
|
|
* @param string|array $data |
|
27
|
|
|
* @param string $type |
|
28
|
|
|
* |
|
29
|
|
|
* @return static |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function create($title = '', $data = null, $type = ButtonType::WEB_URL) |
|
32
|
|
|
{ |
|
33
|
|
|
return new static($title, $data, $type); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Button Constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $title |
|
40
|
|
|
* @param string|array $data |
|
41
|
|
|
* @param string $type |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct($title = '', $data = null, $type = ButtonType::WEB_URL) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->title = $title; |
|
46
|
|
|
$this->data = $data; |
|
47
|
|
|
$this->payload['type'] = $type; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Set Button Title. |
|
52
|
|
|
* |
|
53
|
|
|
* @param $title |
|
54
|
|
|
* |
|
55
|
|
|
* @return $this |
|
56
|
|
|
* @throws CouldNotCreateButton |
|
57
|
|
|
*/ |
|
58
|
|
|
public function title($title) |
|
59
|
|
|
{ |
|
60
|
|
|
if ($this->isNotSetOrEmpty($title)) { |
|
61
|
|
|
throw CouldNotCreateButton::titleNotProvided(); |
|
62
|
|
|
} elseif (mb_strlen($title) > 20) { |
|
63
|
|
|
throw CouldNotCreateButton::titleLimitExceeded($title); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->payload['title'] = $title; |
|
67
|
|
|
|
|
68
|
|
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Set a URL for the button. |
|
73
|
|
|
* |
|
74
|
|
|
* @param $url |
|
75
|
|
|
* |
|
76
|
|
|
* @return $this |
|
77
|
|
|
* @throws CouldNotCreateButton |
|
78
|
|
|
*/ |
|
79
|
|
|
public function url($url) |
|
80
|
|
|
{ |
|
81
|
|
|
if ($this->isNotSetOrEmpty($url)) { |
|
82
|
|
|
throw CouldNotCreateButton::urlNotProvided(); |
|
83
|
|
|
} elseif (!filter_var($url, FILTER_VALIDATE_URL)) { |
|
84
|
|
|
throw CouldNotCreateButton::invalidUrlProvided($url); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$this->payload['url'] = $url; |
|
88
|
|
|
$this->isTypeWebUrl(); |
|
89
|
|
|
|
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param $phone |
|
95
|
|
|
* |
|
96
|
|
|
* @return $this |
|
97
|
|
|
* @throws CouldNotCreateButton |
|
98
|
|
|
*/ |
|
99
|
|
|
public function phone($phone) |
|
100
|
|
|
{ |
|
101
|
|
|
if ($this->isNotSetOrEmpty($phone)) { |
|
102
|
|
|
throw CouldNotCreateButton::phoneNumberNotProvided(); |
|
103
|
|
|
} elseif (is_string($phone) && !starts_with($phone, '+')) { |
|
104
|
|
|
throw CouldNotCreateButton::invalidPhoneNumberProvided($phone); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$this->payload['payload'] = $phone; |
|
108
|
|
|
$this->isTypePhoneNumber(); |
|
109
|
|
|
|
|
110
|
|
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param $postback |
|
115
|
|
|
* |
|
116
|
|
|
* @return $this |
|
117
|
|
|
* @throws CouldNotCreateButton |
|
118
|
|
|
*/ |
|
119
|
|
|
public function postback($postback) |
|
120
|
|
|
{ |
|
121
|
|
|
if ($this->isNotSetOrEmpty($postback)) { |
|
122
|
|
|
throw CouldNotCreateButton::postbackNotProvided(); |
|
123
|
|
|
} elseif (!is_array($postback)) { |
|
124
|
|
|
throw CouldNotCreateButton::invalidPostbackProvided($postback); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$this->payload['payload'] = json_encode($postback); |
|
128
|
|
|
$this->isTypePostback(); |
|
129
|
|
|
|
|
130
|
|
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Set Button Type. |
|
135
|
|
|
* |
|
136
|
|
|
* @param $type Possible Values: "web_url", "postback" or "phone_number". Default: "web_url" |
|
137
|
|
|
* |
|
138
|
|
|
* @return $this |
|
139
|
|
|
*/ |
|
140
|
|
|
public function type($type) |
|
141
|
|
|
{ |
|
142
|
|
|
$this->payload['type'] = $type; |
|
143
|
|
|
|
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Set button type as web_url. |
|
149
|
|
|
* |
|
150
|
|
|
* @return $this |
|
151
|
|
|
*/ |
|
152
|
|
|
public function isTypeWebUrl() |
|
153
|
|
|
{ |
|
154
|
|
|
$this->payload['type'] = ButtonType::WEB_URL; |
|
155
|
|
|
|
|
156
|
|
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Set button type as postback. |
|
161
|
|
|
* |
|
162
|
|
|
* @return $this |
|
163
|
|
|
*/ |
|
164
|
|
|
public function isTypePostback() |
|
165
|
|
|
{ |
|
166
|
|
|
$this->payload['type'] = ButtonType::POSTBACK; |
|
167
|
|
|
|
|
168
|
|
|
return $this; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Set button type as phone_number. |
|
173
|
|
|
* |
|
174
|
|
|
* @return $this |
|
175
|
|
|
*/ |
|
176
|
|
|
public function isTypePhoneNumber() |
|
177
|
|
|
{ |
|
178
|
|
|
$this->payload['type'] = ButtonType::PHONE_NUMBER; |
|
179
|
|
|
|
|
180
|
|
|
return $this; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Determine Button Type. |
|
185
|
|
|
* |
|
186
|
|
|
* @param $type |
|
187
|
|
|
* |
|
188
|
|
|
* @return bool |
|
189
|
|
|
*/ |
|
190
|
|
|
protected function isType($type) |
|
191
|
|
|
{ |
|
192
|
|
|
return isset($this->payload['type']) && $type === $this->payload['type']; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Make payload by data and type. |
|
197
|
|
|
* |
|
198
|
|
|
* @param mixed $data |
|
199
|
|
|
* |
|
200
|
|
|
* @return $this |
|
201
|
|
|
* @throws CouldNotCreateButton |
|
202
|
|
|
*/ |
|
203
|
|
|
protected function makePayload($data) |
|
204
|
|
|
{ |
|
205
|
|
|
if ($this->isNotSetOrEmpty($data)) { |
|
206
|
|
|
return $this; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
switch ($this->payload['type']) { |
|
210
|
|
|
case ButtonType::WEB_URL: |
|
211
|
|
|
$this->url($data); |
|
212
|
|
|
break; |
|
213
|
|
|
case ButtonType::PHONE_NUMBER: |
|
214
|
|
|
$this->phone($data); |
|
215
|
|
|
break; |
|
216
|
|
|
case ButtonType::POSTBACK: |
|
217
|
|
|
$this->postback($data); |
|
218
|
|
|
break; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
if (isset($this->payload['payload']) && mb_strlen($this->payload['payload']) > 1000) { |
|
222
|
|
|
throw CouldNotCreateButton::payloadLimitExceeded($this->payload['payload']); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return $this; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Builds payload and returns an array. |
|
230
|
|
|
* |
|
231
|
|
|
* @return array |
|
232
|
|
|
* @throws CouldNotCreateButton |
|
233
|
|
|
*/ |
|
234
|
|
|
public function toArray() |
|
235
|
|
|
{ |
|
236
|
|
|
$this->title($this->title); |
|
237
|
|
|
$this->makePayload($this->data); |
|
238
|
|
|
|
|
239
|
|
|
return $this->payload; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Convert the object into something JSON serializable. |
|
244
|
|
|
* |
|
245
|
|
|
* @return array |
|
246
|
|
|
*/ |
|
247
|
|
|
public function jsonSerialize() |
|
248
|
|
|
{ |
|
249
|
|
|
return $this->toArray(); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Determine if it's not set or is empty. |
|
254
|
|
|
* |
|
255
|
|
|
* @param $var |
|
256
|
|
|
* |
|
257
|
|
|
* @return bool |
|
258
|
|
|
*/ |
|
259
|
|
|
protected function isNotSetOrEmpty($var) |
|
260
|
|
|
{ |
|
261
|
|
|
return !isset($var) || empty($var); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|