@@ -50,7 +50,11 @@ class Client
50
50
*/
51
51
protected $ baseUrl = 'https://dadata.ru/api ' ;
52
52
53
- protected $ baseUrlGeolocation = 'https://suggestions.dadata.ru/suggestions/api/4_1/rs/detectAddressByIp ' ;
53
+ /**
54
+ * Suggestions url
55
+ * @var string
56
+ */
57
+ protected $ baseSuggestionsUrl = 'https://suggestions.dadata.ru/suggestions/api/4_1/rs/ ' ;
54
58
55
59
/**
56
60
* @var string
@@ -92,10 +96,8 @@ public function __construct(ClientInterface $httpClient, array $config = [])
92
96
public function cleanAddress ($ address )
93
97
{
94
98
$ response = $ this ->query ($ this ->prepareUri ('clean/address ' ), [$ address ]);
99
+ /** @var Address $result */
95
100
$ result = $ this ->populate (new Address , $ response );
96
- if (!$ result instanceof Address) {
97
- throw new RuntimeException ('Unexpected populate result: ' . get_class ($ result ). '. Expected: ' . Address::class);
98
- }
99
101
100
102
return $ result ;
101
103
}
@@ -112,10 +114,9 @@ public function cleanAddress($address)
112
114
public function cleanPhone ($ phone )
113
115
{
114
116
$ response = $ this ->query ($ this ->prepareUri ('clean/phone ' ), [$ phone ]);
117
+ /** @var Phone $result */
115
118
$ result = $ this ->populate (new Phone , $ response );
116
- if (!$ result instanceof Phone) {
117
- throw new RuntimeException ('Unexpected populate result: ' . get_class ($ result ). '. Expected: ' . Phone::class);
118
- }
119
+
119
120
return $ result ;
120
121
}
121
122
@@ -131,10 +132,8 @@ public function cleanPhone($phone)
131
132
public function cleanPassport ($ passport )
132
133
{
133
134
$ response = $ this ->query ($ this ->prepareUri ('clean/passport ' ), [$ passport ]);
135
+ /** @var Passport $result */
134
136
$ result = $ this ->populate (new Passport (), $ response );
135
- if (!$ result instanceof Passport) {
136
- throw new RuntimeException ('Unexpected populate result: ' . get_class ($ result ). '. Expected: ' . Passport::class);
137
- }
138
137
139
138
return $ result ;
140
139
}
@@ -151,10 +150,8 @@ public function cleanPassport($passport)
151
150
public function cleanName ($ name )
152
151
{
153
152
$ response = $ this ->query ($ this ->prepareUri ('clean/name ' ), [$ name ]);
153
+ /** @var Name $result */
154
154
$ result = $ this ->populate (new Name (), $ response );
155
- if (!$ result instanceof Name) {
156
- throw new RuntimeException ('Unexpected populate result: ' . get_class ($ result ). '. Expected: ' . Name::class);
157
- }
158
155
159
156
return $ result ;
160
157
}
@@ -171,10 +168,8 @@ public function cleanName($name)
171
168
public function cleanEmail ($ email )
172
169
{
173
170
$ response = $ this ->query ($ this ->prepareUri ('clean/email ' ), [$ email ]);
171
+ /** @var Email $result */
174
172
$ result = $ this ->populate (new Email , $ response );
175
- if (!$ result instanceof Email) {
176
- throw new RuntimeException ('Unexpected populate result: ' . get_class ($ result ). '. Expected: ' . Email::class);
177
- }
178
173
179
174
return $ result ;
180
175
}
@@ -191,10 +186,8 @@ public function cleanEmail($email)
191
186
public function cleanDate ($ date )
192
187
{
193
188
$ response = $ this ->query ($ this ->prepareUri ('clean/birthdate ' ), [$ date ]);
189
+ /** @var Date $result */
194
190
$ result = $ this ->populate (new Date , $ response );
195
- if (!$ result instanceof Date) {
196
- throw new RuntimeException ('Unexpected populate result: ' . get_class ($ result ). '. Expected: ' . Date::class);
197
- }
198
191
199
192
return $ result ;
200
193
}
@@ -211,10 +204,8 @@ public function cleanDate($date)
211
204
public function cleanVehicle ($ vehicle )
212
205
{
213
206
$ response = $ this ->query ($ this ->prepareUri ('clean/vehicle ' ), [$ vehicle ]);
207
+ /** @var Vehicle $result */
214
208
$ result = $ this ->populate (new Vehicle , $ response );
215
- if (!$ result instanceof Vehicle) {
216
- throw new RuntimeException ('Unexpected populate result: ' . get_class ($ result ). '. Expected: ' . Vehicle::class);
217
- }
218
209
219
210
return $ result ;
220
211
}
@@ -332,7 +323,7 @@ protected function getValueByAnnotatedType(ReflectionProperty $property, $value)
332
323
*/
333
324
public function detectAddressByIp ($ ip )
334
325
{
335
- $ request = new Request ('get ' , $ this ->baseUrlGeolocation . '?ip= ' . $ ip , [
326
+ $ request = new Request ('get ' , $ this ->baseSuggestionsUrl . ' detectAddressByIp ' . '?ip= ' . $ ip , [
336
327
'Accept ' => 'application/json ' ,
337
328
'Authorization ' => 'Token ' . $ this ->token ,
338
329
]);
@@ -361,11 +352,34 @@ public function detectAddressByIp($ip)
361
352
return null ;
362
353
}
363
354
355
+ /** @var Address $address */
364
356
$ address = $ this ->populate (new Address , $ result ['location ' ]['data ' ]);
365
- if (!$ address instanceof Address) {
366
- throw new RuntimeException ('Unexpected populate result: ' . get_class ($ result ). '. Expected: ' . Address::class);
367
- }
368
357
369
358
return $ address ;
370
359
}
360
+
361
+ /**
362
+ * Метод возвращает арес по его коду КЛАДР или ФИАС
363
+ *
364
+ * Dadata comment: Ищет до улицы включительно, при поиске по коду дома возвращает пустой ответ.
365
+ * Так сделано намеренно: КЛАДР-коды и ФИАС-коды домов постоянно изменяются, поэтому хранить их ненадежно.
366
+ * Рекомендуем использовать связку «ФИАС-код улицы + домовая часть отдельно»
367
+ *
368
+ * @param string $addressId
369
+ *
370
+ * @return AbstractResponse|Address|null
371
+ */
372
+ public function getAddressById ($ addressId )
373
+ {
374
+ $ response = $ this ->query ($ this ->baseSuggestionsUrl . 'findById/address ' , ['query ' => $ addressId ]);
375
+
376
+ if (is_array ($ response ) && 0 < count ($ response )) {
377
+ /** @var Address $address */
378
+ $ address = $ this ->populate (new Address , array_shift ($ response )['data ' ]);
379
+
380
+ return $ address ;
381
+ }
382
+
383
+ return null ;
384
+ }
371
385
}
0 commit comments