-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathApi.php
284 lines (237 loc) · 6.89 KB
/
Api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
namespace Zelenin\SmsRu;
use Zelenin\SmsRu\Auth\AuthInterface;
use Zelenin\SmsRu\Client\ClientInterface;
use Zelenin\SmsRu\Entity\AbstractSms;
use Zelenin\SmsRu\Entity\Sms;
use Zelenin\SmsRu\Entity\SmsPool;
use Zelenin\SmsRu\Entity\StoplistPhone;
use Zelenin\SmsRu\Exception\Exception;
use Zelenin\SmsRu\Response\AuthCheckResponse;
use Zelenin\SmsRu\Response\MyBalanceResponse;
use Zelenin\SmsRu\Response\MyLimitResponse;
use Zelenin\SmsRu\Response\MySendersResponse;
use Zelenin\SmsRu\Response\SmsCostResponse;
use Zelenin\SmsRu\Response\SmsResponse;
use Zelenin\SmsRu\Response\SmsStatusResponse;
use Zelenin\SmsRu\Response\StoplistAddResponse;
use Zelenin\SmsRu\Response\StoplistDelResponse;
use Zelenin\SmsRu\Response\StoplistGetResponse;
class Api
{
/**
* @var AuthInterface
*/
private $auth;
/**
* @var ClientInterface
*/
private $client;
/**
* @param AuthInterface $auth
*/
public function __construct(AuthInterface $auth, ClientInterface $client)
{
$this->auth = $auth;
$this->auth->setContext($this);
$this->client = $client;
}
/**
* @param AbstractSms $sms
*
* @return SmsResponse
* @throws Exception
*/
public function smsSend(AbstractSms $sms)
{
$params = [];
if ($sms instanceof Sms) {
$params['to'] = $sms->to;
$params['text'] = $sms->text;
} else if ($sms instanceof SmsPool) {
foreach ($sms->messages as $message) {
$params['multi'][$message->to] = $message->text;
}
} else {
throw new Exception('Only Sms or SmsPool instances');
}
if ($sms->from) {
$params['from'] = $sms->from;
}
if ($sms->time && $sms->time < (time() + 7 * 24 * 60 * 60)) {
$params['time'] = $sms->time;
}
if ($sms->translit) {
$params['translit'] = 1;
}
if ($sms->test) {
$params['test'] = 1;
}
if ($sms->partner_id) {
$params['partner_id'] = $sms->partner_id;
} else if ($this->getAuth()->getPartnerId()) {
$params['partner_id'] = $this->getAuth()->getPartnerId();
}
$response = $this->request('sms/send', $params);
$response = explode("\n", $response);
$smsResponse = new SmsResponse(array_shift($response));
if ($smsResponse->code == 100) {
foreach ($response as $id) {
if (!preg_match('/=/', $id)) {
$smsResponse->ids[] = $id;
}
// else {
// $result = explode('=', $id);
// $response[$result[0]] = $result[1];
// }
}
}
return $smsResponse;
}
/**
* @param string $id
*
* @return SmsStatusResponse
*/
public function smsStatus($id)
{
$response = $this->request(
'sms/status',
[
'id' => $id,
]
);
$response = explode("\n", $response);
return new SmsStatusResponse(array_shift($response));
}
/**
* @param Sms $sms
*
* @return SmsCostResponse
*/
public function smsCost(Sms $sms)
{
$params = [
'to' => $sms->to,
'text' => $sms->text,
];
$response = $this->request('sms/cost', $params);
$response = explode("\n", $response);
$smsCostResponse = new SmsCostResponse(array_shift($response));
$smsCostResponse->price = (float)$response[0];
$smsCostResponse->length = (int)$response[1];
return $smsCostResponse;
}
/**
* @return MyBalanceResponse
*/
public function myBalance()
{
$response = $this->request('my/balance');
$response = explode("\n", $response);
$myBalanceResponse = new MyBalanceResponse(array_shift($response));
$myBalanceResponse->balance = (float)$response[0];
return $myBalanceResponse;
}
/**
* @return MyLimitResponse
*/
public function myLimit()
{
$response = $this->request('my/limit');
$response = explode("\n", $response);
$myLimitResponse = new MyLimitResponse(array_shift($response));
$myLimitResponse->limit = (int)$response[0];
$myLimitResponse->current = (int)$response[1];
return $myLimitResponse;
}
/**
* @return MySendersResponse
*/
public function mySenders()
{
$response = $this->request('my/senders');
$response = explode("\n", $response);
$mySendersResponse = new MySendersResponse(array_shift($response));
foreach ($response as $phone) {
if ($phone) {
$mySendersResponse->phones[] = $phone;
}
}
return $mySendersResponse;
}
/**
* @return AuthCheckResponse
*/
public function authCheck()
{
$response = $this->request('auth/check');
$response = explode("\n", $response);
return new AuthCheckResponse(array_shift($response));
}
/**
* @param string $stoplistPhone
* @param string $stoplistText
*
* @return StoplistAddResponse
*/
public function stoplistAdd($stoplistPhone, $stoplistText)
{
$response = $this->request(
'stoplist/add',
[
'stoplist_phone' => $stoplistPhone,
'stoplist_text' => $stoplistText,
]
);
$response = explode("\n", $response);
return new StoplistAddResponse(array_shift($response));
}
/**
* @param string $stoplistPhone
*
* @return StoplistDelResponse
*/
public function stoplistDel($stoplistPhone)
{
$response = $this->request(
'stoplist/del',
[
'stoplist_phone' => $stoplistPhone,
]
);
$response = explode("\n", $response);
return new StoplistDelResponse(array_shift($response));
}
/**
* @return StoplistGetResponse
*/
public function stoplistGet()
{
$response = $this->request('stoplist/get');
$response = explode("\n", $response);
$stoplistGetResponse = new StoplistGetResponse(array_shift($response));
foreach ($response as $phone) {
$phone = explode(';', $phone);
$stoplistGetResponse->phones[] = new StoplistPhone($phone[0], $phone[1]);
}
return $stoplistGetResponse;
}
/**
* @param string $method
* @param array $params
*
* @return string
*/
public function request($method, $params = [])
{
return $this->client->request($method, array_merge($params, $this->getAuth()->getAuthParams()));
}
/**
* @return AuthInterface
*/
private function getAuth()
{
return $this->auth;
}
}