1 | <?php |
||
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) |
||
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) |
||
49 | |||
50 | /** |
||
51 | * Set Button Title. |
||
52 | * |
||
53 | * @param $title |
||
54 | * |
||
55 | * @return $this |
||
56 | * @throws CouldNotCreateButton |
||
57 | */ |
||
58 | public function title($title) |
||
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) |
||
92 | |||
93 | /** |
||
94 | * @param $phone |
||
95 | * |
||
96 | * @return $this |
||
97 | * @throws CouldNotCreateButton |
||
98 | */ |
||
99 | public function phone($phone) |
||
112 | |||
113 | /** |
||
114 | * @param $postback |
||
115 | * |
||
116 | * @return $this |
||
117 | * @throws CouldNotCreateButton |
||
118 | */ |
||
119 | public function postback($postback) |
||
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) |
||
146 | |||
147 | /** |
||
148 | * Set button type as web_url. |
||
149 | * |
||
150 | * @return $this |
||
151 | */ |
||
152 | public function isTypeWebUrl() |
||
158 | |||
159 | /** |
||
160 | * Set button type as postback. |
||
161 | * |
||
162 | * @return $this |
||
163 | */ |
||
164 | public function isTypePostback() |
||
170 | |||
171 | /** |
||
172 | * Set button type as phone_number. |
||
173 | * |
||
174 | * @return $this |
||
175 | */ |
||
176 | public function isTypePhoneNumber() |
||
182 | |||
183 | /** |
||
184 | * Determine Button Type. |
||
185 | * |
||
186 | * @param $type |
||
187 | * |
||
188 | * @return bool |
||
189 | */ |
||
190 | protected function isType($type) |
||
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) |
||
227 | |||
228 | /** |
||
229 | * Builds payload and returns an array. |
||
230 | * |
||
231 | * @return array |
||
232 | * @throws CouldNotCreateButton |
||
233 | */ |
||
234 | public function toArray() |
||
241 | |||
242 | /** |
||
243 | * Convert the object into something JSON serializable. |
||
244 | * |
||
245 | * @return array |
||
246 | */ |
||
247 | public function jsonSerialize() |
||
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) |
||
263 | } |
||
264 |