1 | <?php |
||
13 | class Button implements JsonSerializable |
||
14 | { |
||
15 | /** @var string Button Title */ |
||
16 | protected $title; |
||
17 | |||
18 | /** @var string Button Type */ |
||
19 | protected $type; |
||
20 | |||
21 | /** @var string|array Button URL, Postback Data or Phone Number */ |
||
22 | protected $data; |
||
23 | |||
24 | /** @var array Payload */ |
||
25 | protected $payload = []; |
||
26 | |||
27 | /** |
||
28 | * Create a button. |
||
29 | * |
||
30 | * @param string $title |
||
31 | * @param string|array $data |
||
32 | * @param string $type |
||
33 | * |
||
34 | * @return static |
||
35 | */ |
||
36 | public static function create(string $title = '', $data = null, string $type = ButtonType::WEB_URL): self |
||
40 | |||
41 | /** |
||
42 | * Button Constructor. |
||
43 | * |
||
44 | * @param string $title |
||
45 | * @param string|array $data |
||
46 | * @param string $type |
||
47 | */ |
||
48 | public function __construct(string $title = '', $data = null, string $type = ButtonType::WEB_URL) |
||
54 | |||
55 | /** |
||
56 | * Set Button Title. |
||
57 | * |
||
58 | * @param string $title |
||
59 | * |
||
60 | * @throws CouldNotCreateButton |
||
61 | * @return $this |
||
62 | */ |
||
63 | public function title(string $title): self |
||
77 | |||
78 | /** |
||
79 | * Set a URL for the button. |
||
80 | * |
||
81 | * @param string $url |
||
82 | * |
||
83 | * @throws CouldNotCreateButton |
||
84 | * @return $this |
||
85 | */ |
||
86 | public function url(string $url): self |
||
101 | |||
102 | /** |
||
103 | * @param string $phone |
||
104 | * |
||
105 | * @throws CouldNotCreateButton |
||
106 | * @return $this |
||
107 | */ |
||
108 | public function phone(string $phone): self |
||
123 | |||
124 | /** |
||
125 | * @param array $postback |
||
126 | * |
||
127 | * @throws CouldNotCreateButton |
||
128 | * @return $this |
||
129 | */ |
||
130 | public function postback(array $postback): self |
||
141 | |||
142 | /** |
||
143 | * Set Button Type. |
||
144 | * |
||
145 | * @param string $type Possible Values: "web_url", "postback" or "phone_number". Default: "web_url" |
||
146 | * |
||
147 | * @return $this |
||
148 | */ |
||
149 | public function type(string $type): self |
||
155 | |||
156 | /** |
||
157 | * Set button type as web_url. |
||
158 | * |
||
159 | * @return $this |
||
160 | */ |
||
161 | public function isTypeWebUrl(): self |
||
167 | |||
168 | /** |
||
169 | * Set button type as postback. |
||
170 | * |
||
171 | * @return $this |
||
172 | */ |
||
173 | public function isTypePostback(): self |
||
179 | |||
180 | /** |
||
181 | * Set button type as phone_number. |
||
182 | * |
||
183 | * @return $this |
||
184 | */ |
||
185 | public function isTypePhoneNumber(): self |
||
191 | |||
192 | /** |
||
193 | * Determine Button Type. |
||
194 | * |
||
195 | * @param string $type |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | protected function isType(string $type): bool |
||
203 | |||
204 | /** |
||
205 | * Make payload by data and type. |
||
206 | * |
||
207 | * @param mixed $data |
||
208 | * |
||
209 | * @throws CouldNotCreateButton |
||
210 | * @return $this |
||
211 | */ |
||
212 | protected function makePayload($data): self |
||
236 | |||
237 | /** |
||
238 | * Builds payload and returns an array. |
||
239 | * |
||
240 | * @throws CouldNotCreateButton |
||
241 | * @return array |
||
242 | */ |
||
243 | public function toArray(): array |
||
250 | |||
251 | /** |
||
252 | * Convert the object into something JSON serializable. |
||
253 | * |
||
254 | * @throws CouldNotCreateButton |
||
255 | * @return mixed |
||
256 | */ |
||
257 | public function jsonSerialize() |
||
261 | } |
||
262 |