1010use Sms77 \Api \Validator \ValidateForVoiceValidator ;
1111use Sms77 \Api \Validator \VoiceValidator ;
1212
13- class Client
14- {
13+ class Client {
14+ const BASE_URI = ' https://gateway.sms77.io/api ' ;
1515 /* @var string $apiKey */
1616 private $ apiKey ;
17-
1817 /* @var string $sendWith */
1918 private $ sendWith ;
2019
21- const BASE_URI = 'https://gateway.sms77.io/api ' ;
22-
23- public function __construct ($ apiKey , $ sendWith = 'php-api ' )
24- {
20+ public function __construct ($ apiKey , $ sendWith = 'php-api ' ) {
2521 $ this ->apiKey = $ apiKey ;
2622 $ this ->sendWith = $ sendWith ;
2723 }
2824
29- public function balance ()
30- {
25+ public function balance () {
3126 return $ this ->request ('balance ' , $ this ->buildOptions ([]));
3227 }
3328
34- public function contacts ($ action , array $ extra = [])
35- {
29+ private function request ($ path , $ options = []) {
30+ $ curl_get_contents = static function ($ url ) {
31+ $ ch = curl_init ();
32+ curl_setopt ($ ch , CURLOPT_URL , $ url );
33+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
34+ $ output = curl_exec ($ ch );
35+ curl_close ($ ch );
36+ return $ output ;
37+ };
38+
39+ return $ curl_get_contents (self ::BASE_URI . '/ ' . $ path . '? ' . http_build_query ($ options ));
40+ }
41+
42+ private function buildOptions (array $ required , array $ extra = []) {
43+ $ required = array_merge ($ required , [
44+ 'p ' => $ this ->apiKey ,
45+ 'sendwith ' => '' === $ this ->sendWith ? 'unknown ' : $ this ->sendWith ,
46+ ]);
47+
48+ return array_merge ($ required , $ extra );
49+ }
50+
51+ public function contacts ($ action , array $ extra = []) {
3652 $ options = $ this ->buildOptions ([
3753 'action ' => $ action ,
3854 ], $ extra );
@@ -42,8 +58,7 @@ public function contacts($action, array $extra = [])
4258 return $ this ->request ('contacts ' , $ options );
4359 }
4460
45- public function lookup ($ type , $ number , array $ extra = [])
46- {
61+ public function lookup ($ type , $ number , array $ extra = []) {
4762 $ options = $ this ->buildOptions ([
4863 'type ' => $ type ,
4964 'number ' => $ number ,
@@ -54,29 +69,26 @@ public function lookup($type, $number, array $extra = [])
5469 return $ this ->request ('lookup ' , $ options );
5570 }
5671
57- public function pricing (array $ extra = [])
58- {
72+ public function pricing (array $ extra = []) {
5973 $ options = $ this ->buildOptions ([], $ extra );
6074
6175 (new PricingValidator ($ options ))->validate ();
6276
6377 return $ this ->request ('pricing ' , $ options );
6478 }
6579
66- public function sms ($ to , $ text , array $ extra = [])
67- {
80+ public function sms ($ to , $ text , array $ extra = []) {
6881 $ options = $ this ->buildOptions ([
6982 'to ' => $ to ,
70- 'text ' => $ text
83+ 'text ' => $ text,
7184 ], $ extra );
7285
7386 (new SmsValidator ($ options ))->validate ();
7487
7588 return $ this ->request ('sms ' , $ options );
7689 }
7790
78- public function status ($ msgId )
79- {
91+ public function status ($ msgId ) {
8092 $ options = $ this ->buildOptions ([
8193 'msg_id ' => $ msgId ,
8294 ]);
@@ -86,8 +98,7 @@ public function status($msgId)
8698 return $ this ->request ('status ' , $ options );
8799 }
88100
89- public function validateForVoice ($ number , array $ extra = [])
90- {
101+ public function validateForVoice ($ number , array $ extra = []) {
91102 $ options = $ this ->buildOptions ([
92103 'number ' => $ number ,
93104 ], $ extra );
@@ -97,39 +108,14 @@ public function validateForVoice($number, array $extra = [])
97108 return $ this ->request ('validate_for_voice ' , $ options );
98109 }
99110
100- public function voice ($ to , $ text , array $ extra = [])
101- {
111+ public function voice ($ to , $ text , array $ extra = []) {
102112 $ options = $ this ->buildOptions ([
103113 'to ' => $ to ,
104- 'text ' => $ text
114+ 'text ' => $ text,
105115 ], $ extra );
106116
107117 (new VoiceValidator ($ options ))->validate ();
108118
109119 return $ this ->request ('voice ' , $ options );
110120 }
111-
112- private function buildOptions (array $ required , array $ extra = [])
113- {
114- $ required = array_merge ($ required , [
115- 'p ' => $ this ->apiKey ,
116- 'sendwith ' => '' === $ this ->sendWith ? 'unknown ' : $ this ->sendWith
117- ]);
118-
119- return array_merge ($ required , $ extra );
120- }
121-
122- private function request ($ path , $ options = [])
123- {
124- $ curl_get_contents = static function ($ url ) {
125- $ ch = curl_init ();
126- curl_setopt ($ ch , CURLOPT_URL , $ url );
127- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
128- $ output = curl_exec ($ ch );
129- curl_close ($ ch );
130- return $ output ;
131- };
132-
133- return $ curl_get_contents (self ::BASE_URI . '/ ' . $ path . '? ' . http_build_query ($ options ));
134- }
135121}
0 commit comments