9
9
use Dadata \Response \Name ;
10
10
use Dadata \Response \Passport ;
11
11
use Dadata \Response \Phone ;
12
- use Dadata \Response \Vehicle ;
13
12
use Dadata \Response \Suggestions \Party ;
14
- use Exception ;
13
+ use Dadata \ Response \ Vehicle ;
15
14
use GuzzleHttp \ClientInterface ;
15
+ use GuzzleHttp \Exception \GuzzleException ;
16
16
use GuzzleHttp \Psr7 \Request ;
17
- use InvalidArgumentException ;
18
- use ReflectionClass ;
19
- use ReflectionProperty ;
20
- use RuntimeException ;
21
17
22
18
/**
23
19
* Class Client
@@ -28,19 +24,21 @@ class Client
28
24
* Исходное значение распознано уверенно. Не требуется ручная проверка
29
25
*/
30
26
const QC_OK = 0 ;
27
+
31
28
/**
32
29
* Исходное значение распознано с допущениями или не распознано. Требуется ручная проверка
33
30
*/
34
31
const QC_UNSURE = 1 ;
32
+
35
33
/**
36
34
* Исходное значение пустое или заведомо "мусорное"
37
35
*/
38
36
const QC_INVALID = 2 ;
39
-
37
+
40
38
const METHOD_GET = 'GET ' ;
41
-
39
+
42
40
const METHOD_POST = 'POST ' ;
43
-
41
+
44
42
/**
45
43
* @var string
46
44
*/
@@ -92,8 +90,9 @@ public function __construct(ClientInterface $httpClient, array $config = [])
92
90
*
93
91
* @return Address
94
92
* @throws \ReflectionException
95
- * @throws RuntimeException
96
- * @throws InvalidArgumentException
93
+ * @throws \RuntimeException
94
+ * @throws \InvalidArgumentException
95
+ * @throws GuzzleException
97
96
*/
98
97
public function cleanAddress ($ address )
99
98
{
@@ -111,14 +110,15 @@ public function cleanAddress($address)
111
110
*
112
111
* @return Phone
113
112
* @throws \ReflectionException
114
- * @throws RuntimeException
115
- * @throws InvalidArgumentException
113
+ * @throws \RuntimeException
114
+ * @throws \InvalidArgumentException
115
+ * @throws GuzzleException
116
116
*/
117
117
public function cleanPhone ($ phone )
118
118
{
119
119
$ response = $ this ->query ($ this ->prepareUri ('clean/phone ' ), [$ phone ]);
120
120
/** @var Phone $result */
121
- $ result = $ this ->populate (new Phone , $ response );
121
+ $ result = $ this ->populate (new Phone () , $ response );
122
122
123
123
return $ result ;
124
124
}
@@ -130,8 +130,9 @@ public function cleanPhone($phone)
130
130
*
131
131
* @return Passport
132
132
* @throws \ReflectionException
133
- * @throws RuntimeException
134
- * @throws InvalidArgumentException
133
+ * @throws \RuntimeException
134
+ * @throws \InvalidArgumentException
135
+ * @throws GuzzleException
135
136
*/
136
137
public function cleanPassport ($ passport )
137
138
{
@@ -149,8 +150,9 @@ public function cleanPassport($passport)
149
150
*
150
151
* @return Name
151
152
* @throws \ReflectionException
152
- * @throws RuntimeException
153
- * @throws InvalidArgumentException
153
+ * @throws \RuntimeException
154
+ * @throws \InvalidArgumentException
155
+ * @throws GuzzleException
154
156
*/
155
157
public function cleanName ($ name )
156
158
{
@@ -168,14 +170,15 @@ public function cleanName($name)
168
170
*
169
171
* @return Email
170
172
* @throws \ReflectionException
171
- * @throws RuntimeException
172
- * @throws InvalidArgumentException
173
+ * @throws \RuntimeException
174
+ * @throws \InvalidArgumentException
175
+ * @throws GuzzleException
173
176
*/
174
177
public function cleanEmail ($ email )
175
178
{
176
179
$ response = $ this ->query ($ this ->prepareUri ('clean/email ' ), [$ email ]);
177
180
/** @var Email $result */
178
- $ result = $ this ->populate (new Email , $ response );
181
+ $ result = $ this ->populate (new Email () , $ response );
179
182
180
183
return $ result ;
181
184
}
@@ -187,14 +190,15 @@ public function cleanEmail($email)
187
190
*
188
191
* @return Date
189
192
* @throws \ReflectionException
190
- * @throws RuntimeException
191
- * @throws InvalidArgumentException
193
+ * @throws \RuntimeException
194
+ * @throws \InvalidArgumentException
195
+ * @throws GuzzleException
192
196
*/
193
197
public function cleanDate ($ date )
194
198
{
195
199
$ response = $ this ->query ($ this ->prepareUri ('clean/birthdate ' ), [$ date ]);
196
200
/** @var Date $result */
197
- $ result = $ this ->populate (new Date , $ response );
201
+ $ result = $ this ->populate (new Date () , $ response );
198
202
199
203
return $ result ;
200
204
}
@@ -206,14 +210,15 @@ public function cleanDate($date)
206
210
*
207
211
* @return Vehicle
208
212
* @throws \ReflectionException
209
- * @throws RuntimeException
210
- * @throws InvalidArgumentException
213
+ * @throws \RuntimeException
214
+ * @throws \InvalidArgumentException
215
+ * @throws GuzzleException
211
216
*/
212
217
public function cleanVehicle ($ vehicle )
213
218
{
214
219
$ response = $ this ->query ($ this ->prepareUri ('clean/vehicle ' ), [$ vehicle ]);
215
220
/** @var Vehicle $result */
216
- $ result = $ this ->populate (new Vehicle , $ response );
221
+ $ result = $ this ->populate (new Vehicle () , $ response );
217
222
218
223
return $ result ;
219
224
}
@@ -222,45 +227,47 @@ public function cleanVehicle($vehicle)
222
227
* Gets balance.
223
228
*
224
229
* @return float
225
- * @throws RuntimeException
226
- * @throws InvalidArgumentException
230
+ * @throws \RuntimeException
231
+ * @throws \InvalidArgumentException
232
+ * @throws GuzzleException
227
233
*/
228
234
public function getBalance ()
229
235
{
230
236
$ response = $ this ->query ($ this ->prepareUri ('profile/balance ' ), [], self ::METHOD_GET );
231
- return (double ) $ response ;
237
+ return (float ) $ response ;
232
238
}
233
239
234
240
/**
235
241
* Requests API.
236
242
*
237
243
* @param string $uri
238
- * @param array $params
244
+ * @param array $params
239
245
*
240
246
* @param string $method
241
247
*
242
248
* @return array
243
- * @throws RuntimeException
244
- * @throws InvalidArgumentException
249
+ * @throws \RuntimeException
250
+ * @throws \InvalidArgumentException
251
+ * @throws GuzzleException
245
252
*/
246
253
protected function query ($ uri , array $ params = [], $ method = self ::METHOD_POST )
247
254
{
248
255
$ request = new Request ($ method , $ uri , [
249
- 'Content-Type ' => 'application/json ' ,
256
+ 'Content-Type ' => 'application/json ' ,
250
257
'Authorization ' => 'Token ' . $ this ->token ,
251
- 'X-Secret ' => $ this ->secret ,
258
+ 'X-Secret ' => $ this ->secret ,
252
259
], 0 < count ($ params ) ? json_encode ($ params ) : null );
253
260
254
261
$ response = $ this ->httpClient ->send ($ request , $ this ->httpOptions );
255
262
256
263
$ result = json_decode ($ response ->getBody (), true );
257
264
258
265
if (json_last_error () !== JSON_ERROR_NONE ) {
259
- throw new RuntimeException ('Error parsing response: ' . json_last_error_msg ());
266
+ throw new \ RuntimeException ('Error parsing response: ' . json_last_error_msg ());
260
267
}
261
268
262
269
if (empty ($ result )) {
263
- throw new RuntimeException ('Empty result ' );
270
+ throw new \ RuntimeException ('Empty result ' );
264
271
}
265
272
266
273
return array_shift ($ result );
@@ -288,9 +295,9 @@ protected function prepareUri($endpoint)
288
295
*/
289
296
protected function populate (AbstractResponse $ object , array $ data )
290
297
{
291
- $ reflect = new ReflectionClass ($ object );
298
+ $ reflect = new \ ReflectionClass ($ object );
292
299
293
- $ properties = $ reflect ->getProperties (ReflectionProperty::IS_PUBLIC );
300
+ $ properties = $ reflect ->getProperties (\ ReflectionProperty::IS_PUBLIC );
294
301
295
302
foreach ($ properties as $ property ) {
296
303
if (array_key_exists ($ property ->name , $ data )) {
@@ -309,10 +316,14 @@ protected function populate(AbstractResponse $object, array $data)
309
316
* @return Party\Party
310
317
* @throws \ReflectionException
311
318
*/
312
- protected function populateParty (array $ response )
319
+ protected function populateParty (array $ response )
313
320
{
314
- list ($ name , $ post ) = array_values ($ response ['data ' ]['management ' ]);
315
- $ management = new Party \ManagementDto ($ name , $ post );
321
+ $ management = null ;
322
+ $ managementData = $ response ['data ' ]['management ' ];
323
+ if (is_array ($ managementData ) && array_key_exists ('name ' , $ managementData ) && array_key_exists ('post ' , $ managementData )) {
324
+ list ($ name , $ post ) = array_values ($ response ['data ' ]['management ' ]);
325
+ $ management = new Party \ManagementDto ($ name , $ post );
326
+ }
316
327
317
328
list ($ code , $ full , $ short ) = array_values ($ response ['data ' ]['opf ' ]);
318
329
$ opf = new Party \OpfDto ($ code , $ full , $ short );
@@ -326,7 +337,10 @@ protected function populateParty (array $response)
326
337
list ($ value , $ unrestrictedValue ) = array_values ($ response ['data ' ]['address ' ]);
327
338
$ simpleAddress = new Party \AddressDto ($ value , $ unrestrictedValue );
328
339
329
- $ address = $ this ->populate (new Address (), $ response ['data ' ]['address ' ]['data ' ]);
340
+ $ address = null ;
341
+ if (is_array ($ response ['data ' ]['address ' ]['data ' ])) {
342
+ $ address = $ this ->populate (new Address (), $ response ['data ' ]['address ' ]['data ' ]);
343
+ }
330
344
331
345
return new Party \Party (
332
346
$ response ['value ' ],
@@ -350,11 +364,11 @@ protected function populateParty (array $response)
350
364
/**
351
365
* Guesses and converts property type by phpdoc comment.
352
366
*
353
- * @param ReflectionProperty $property
367
+ * @param \ ReflectionProperty $property
354
368
* @param mixed $value
355
369
* @return mixed
356
370
*/
357
- protected function getValueByAnnotatedType (ReflectionProperty $ property , $ value )
371
+ protected function getValueByAnnotatedType (\ ReflectionProperty $ property , $ value )
358
372
{
359
373
$ comment = $ property ->getDocComment ();
360
374
if (preg_match ('/@var (.+?)(\|null)? / ' , $ comment , $ matches )) {
@@ -375,12 +389,13 @@ protected function getValueByAnnotatedType(ReflectionProperty $property, $value)
375
389
/**
376
390
* @param string $ip
377
391
* @return null|Address
378
- * @throws Exception
392
+ * @throws \Exception
393
+ * @throws GuzzleException
379
394
*/
380
395
public function detectAddressByIp ($ ip )
381
396
{
382
397
$ request = new Request ('get ' , $ this ->baseSuggestionsUrl . 'detectAddressByIp ' . '?ip= ' . $ ip , [
383
- 'Accept ' => 'application/json ' ,
398
+ 'Accept ' => 'application/json ' ,
384
399
'Authorization ' => 'Token ' . $ this ->token ,
385
400
]);
386
401
@@ -389,19 +404,19 @@ public function detectAddressByIp($ip)
389
404
$ result = json_decode ($ response ->getBody (), true );
390
405
391
406
if (json_last_error () !== JSON_ERROR_NONE ) {
392
- throw new RuntimeException ('Error parsing response: ' . json_last_error_msg ());
407
+ throw new \ RuntimeException ('Error parsing response: ' . json_last_error_msg ());
393
408
}
394
409
395
410
if (!array_key_exists ('location ' , $ result )) {
396
- throw new Exception ('Required key "location" is missing ' );
411
+ throw new \ Exception ('Required key "location" is missing ' );
397
412
}
398
413
399
414
if (null === $ result ['location ' ]) {
400
415
return null ;
401
416
}
402
417
403
418
if (!array_key_exists ('data ' , $ result ['location ' ])) {
404
- throw new Exception ('Required key "data" is missing ' );
419
+ throw new \ Exception ('Required key "data" is missing ' );
405
420
}
406
421
407
422
if (null === $ result ['location ' ]['data ' ]) {
@@ -427,6 +442,7 @@ public function detectAddressByIp($ip)
427
442
* @throws \ReflectionException
428
443
* @throws \RuntimeException
429
444
* @throws \InvalidArgumentException
445
+ * @throws GuzzleException
430
446
*/
431
447
public function getAddressById ($ addressId )
432
448
{
@@ -450,10 +466,10 @@ public function getAddressById($addressId)
450
466
* ФИО руководителя компании;
451
467
* адресу до улицы.
452
468
*
453
- * @param $party
469
+ * @param string $party
454
470
*
455
471
* @return \SplObjectStorage
456
- * @throws \GuzzleHttp\Exception\ GuzzleException
472
+ * @throws GuzzleException
457
473
* @throws \ReflectionException
458
474
* @throws \InvalidArgumentException
459
475
* @throws \RuntimeException
@@ -481,6 +497,4 @@ protected function prepareSuggestionsUri($endpoint)
481
497
{
482
498
return $ this ->baseSuggestionsUrl . $ endpoint ;
483
499
}
484
-
485
-
486
500
}
0 commit comments