@@ -18,46 +18,41 @@ class Button implements JsonSerializable
1818 /** @var string Button Type */
1919 protected $ type ;
2020
21- /** @var string| array Button URL, Postback Data or Phone Number */
21+ /** @var array|string Button URL, Postback Data or Phone Number */
2222 protected $ data ;
2323
2424 /** @var array Payload */
2525 protected $ payload = [];
2626
2727 /**
28- * Create a button.
29- *
30- * @param string $title
31- * @param string|array $data
32- * @param string $type
28+ * Button Constructor.
3329 *
34- * @return static
30+ * @param array|string $data
3531 */
36- public static function create (string $ title = '' , $ data = null , string $ type = ButtonType::WEB_URL ): self
32+ public function __construct (string $ title = '' , $ data = null , string $ type = ButtonType::WEB_URL )
3733 {
38- return new static ($ title , $ data , $ type );
34+ $ this ->title = $ title ;
35+ $ this ->data = $ data ;
36+ $ this ->payload ['type ' ] = $ type ;
3937 }
4038
4139 /**
42- * Button Constructor.
40+ * Create a button.
41+ *
42+ * @param array|string $data
4343 *
44- * @param string $title
45- * @param string|array $data
46- * @param string $type
44+ * @return static
4745 */
48- public function __construct (string $ title = '' , $ data = null , string $ type = ButtonType::WEB_URL )
46+ public static function create (string $ title = '' , $ data = null , string $ type = ButtonType::WEB_URL ): self
4947 {
50- $ this ->title = $ title ;
51- $ this ->data = $ data ;
52- $ this ->payload ['type ' ] = $ type ;
48+ return new static ($ title , $ data , $ type );
5349 }
5450
5551 /**
5652 * Set Button Title.
5753 *
58- * @param string $title
59- *
6054 * @throws CouldNotCreateButton
55+ *
6156 * @return $this
6257 */
6358 public function title (string $ title ): self
@@ -78,9 +73,8 @@ public function title(string $title): self
7873 /**
7974 * Set a URL for the button.
8075 *
81- * @param string $url
82- *
8376 * @throws CouldNotCreateButton
77+ *
8478 * @return $this
8579 */
8680 public function url (string $ url ): self
@@ -89,7 +83,7 @@ public function url(string $url): self
8983 throw CouldNotCreateButton::urlNotProvided ();
9084 }
9185
92- if (! filter_var ($ url , FILTER_VALIDATE_URL )) {
86+ if (!filter_var ($ url , FILTER_VALIDATE_URL )) {
9387 throw CouldNotCreateButton::invalidUrlProvided ($ url );
9488 }
9589
@@ -100,9 +94,8 @@ public function url(string $url): self
10094 }
10195
10296 /**
103- * @param string $phone
104- *
10597 * @throws CouldNotCreateButton
98+ *
10699 * @return $this
107100 */
108101 public function phone (string $ phone ): self
@@ -111,7 +104,7 @@ public function phone(string $phone): self
111104 throw CouldNotCreateButton::phoneNumberNotProvided ();
112105 }
113106
114- if (is_string ($ phone ) && ! Str::startsWith ($ phone , '+ ' )) {
107+ if (is_string ($ phone ) && !Str::startsWith ($ phone , '+ ' )) {
115108 throw CouldNotCreateButton::invalidPhoneNumberProvided ($ phone );
116109 }
117110
@@ -122,10 +115,11 @@ public function phone(string $phone): self
122115 }
123116
124117 /**
125- * @param $postback
118+ * @param $postback
126119 *
127- * @return $this
128120 * @throws CouldNotCreateButton|\JsonException
121+ *
122+ * @return $this
129123 */
130124 public function postback ($ postback ): self
131125 {
@@ -142,7 +136,7 @@ public function postback($postback): self
142136 /**
143137 * Set Button Type.
144138 *
145- * @param string $type Possible Values: "web_url", "postback" or "phone_number". Default: "web_url"
139+ * @param string $type Possible Values: "web_url", "postback" or "phone_number". Default: "web_url"
146140 *
147141 * @return $this
148142 */
@@ -190,11 +184,32 @@ public function isTypePhoneNumber(): self
190184 }
191185
192186 /**
193- * Determine Button Type.
187+ * Builds payload and returns an array.
188+ *
189+ * @throws CouldNotCreateButton
190+ */
191+ public function toArray (): array
192+ {
193+ $ this ->title ($ this ->title );
194+ $ this ->makePayload ($ this ->data );
195+
196+ return $ this ->payload ;
197+ }
198+
199+ /**
200+ * Convert the object into something JSON serializable.
194201 *
195- * @param string $type
202+ * @throws CouldNotCreateButton
196203 *
197- * @return bool
204+ * @return mixed
205+ */
206+ public function jsonSerialize ()
207+ {
208+ return $ this ->toArray ();
209+ }
210+
211+ /**
212+ * Determine Button Type.
198213 */
199214 protected function isType (string $ type ): bool
200215 {
@@ -204,9 +219,10 @@ protected function isType(string $type): bool
204219 /**
205220 * Make payload by data and type.
206221 *
207- * @param mixed $data
222+ * @param mixed $data
208223 *
209224 * @throws CouldNotCreateButton
225+ *
210226 * @return $this
211227 */
212228 protected function makePayload ($ data ): self
@@ -218,12 +234,17 @@ protected function makePayload($data): self
218234 switch ($ this ->payload ['type ' ]) {
219235 case ButtonType::WEB_URL :
220236 $ this ->url ($ data );
237+
221238 break ;
239+
222240 case ButtonType::PHONE_NUMBER :
223241 $ this ->phone ($ data );
242+
224243 break ;
244+
225245 case ButtonType::POSTBACK :
226246 $ this ->postback ($ data );
247+
227248 break ;
228249 }
229250
@@ -233,29 +254,4 @@ protected function makePayload($data): self
233254
234255 return $ this ;
235256 }
236-
237- /**
238- * Builds payload and returns an array.
239- *
240- * @throws CouldNotCreateButton
241- * @return array
242- */
243- public function toArray (): array
244- {
245- $ this ->title ($ this ->title );
246- $ this ->makePayload ($ this ->data );
247-
248- return $ this ->payload ;
249- }
250-
251- /**
252- * Convert the object into something JSON serializable.
253- *
254- * @throws CouldNotCreateButton
255- * @return mixed
256- */
257- public function jsonSerialize ()
258- {
259- return $ this ->toArray ();
260- }
261257}
0 commit comments