10
10
use Sms77 \Api \Validator \ValidateForVoiceValidator ;
11
11
use Sms77 \Api \Validator \VoiceValidator ;
12
12
13
- class Client
14
- {
13
+ class Client {
14
+ const BASE_URI = ' https://gateway.sms77.io/api ' ;
15
15
/* @var string $apiKey */
16
16
private $ apiKey ;
17
-
18
17
/* @var string $sendWith */
19
18
private $ sendWith ;
20
19
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 ' ) {
25
21
$ this ->apiKey = $ apiKey ;
26
22
$ this ->sendWith = $ sendWith ;
27
23
}
28
24
29
- public function balance ()
30
- {
25
+ public function balance () {
31
26
return $ this ->request ('balance ' , $ this ->buildOptions ([]));
32
27
}
33
28
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 = []) {
36
52
$ options = $ this ->buildOptions ([
37
53
'action ' => $ action ,
38
54
], $ extra );
@@ -42,8 +58,7 @@ public function contacts($action, array $extra = [])
42
58
return $ this ->request ('contacts ' , $ options );
43
59
}
44
60
45
- public function lookup ($ type , $ number , array $ extra = [])
46
- {
61
+ public function lookup ($ type , $ number , array $ extra = []) {
47
62
$ options = $ this ->buildOptions ([
48
63
'type ' => $ type ,
49
64
'number ' => $ number ,
@@ -54,29 +69,26 @@ public function lookup($type, $number, array $extra = [])
54
69
return $ this ->request ('lookup ' , $ options );
55
70
}
56
71
57
- public function pricing (array $ extra = [])
58
- {
72
+ public function pricing (array $ extra = []) {
59
73
$ options = $ this ->buildOptions ([], $ extra );
60
74
61
75
(new PricingValidator ($ options ))->validate ();
62
76
63
77
return $ this ->request ('pricing ' , $ options );
64
78
}
65
79
66
- public function sms ($ to , $ text , array $ extra = [])
67
- {
80
+ public function sms ($ to , $ text , array $ extra = []) {
68
81
$ options = $ this ->buildOptions ([
69
82
'to ' => $ to ,
70
- 'text ' => $ text
83
+ 'text ' => $ text,
71
84
], $ extra );
72
85
73
86
(new SmsValidator ($ options ))->validate ();
74
87
75
88
return $ this ->request ('sms ' , $ options );
76
89
}
77
90
78
- public function status ($ msgId )
79
- {
91
+ public function status ($ msgId ) {
80
92
$ options = $ this ->buildOptions ([
81
93
'msg_id ' => $ msgId ,
82
94
]);
@@ -86,8 +98,7 @@ public function status($msgId)
86
98
return $ this ->request ('status ' , $ options );
87
99
}
88
100
89
- public function validateForVoice ($ number , array $ extra = [])
90
- {
101
+ public function validateForVoice ($ number , array $ extra = []) {
91
102
$ options = $ this ->buildOptions ([
92
103
'number ' => $ number ,
93
104
], $ extra );
@@ -97,39 +108,14 @@ public function validateForVoice($number, array $extra = [])
97
108
return $ this ->request ('validate_for_voice ' , $ options );
98
109
}
99
110
100
- public function voice ($ to , $ text , array $ extra = [])
101
- {
111
+ public function voice ($ to , $ text , array $ extra = []) {
102
112
$ options = $ this ->buildOptions ([
103
113
'to ' => $ to ,
104
- 'text ' => $ text
114
+ 'text ' => $ text,
105
115
], $ extra );
106
116
107
117
(new VoiceValidator ($ options ))->validate ();
108
118
109
119
return $ this ->request ('voice ' , $ options );
110
120
}
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
- }
135
121
}
0 commit comments