Skip to content

Commit e484956

Browse files
author
Sergey Kasatkin
authored
Merge pull request #11 from vitaly-grechkin/master
getAddressById added to the client
2 parents a21e74d + 374aa86 commit e484956

File tree

1 file changed

+40
-26
lines changed

1 file changed

+40
-26
lines changed

src/Client.php

+40-26
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ class Client
5050
*/
5151
protected $baseUrl = 'https://dadata.ru/api';
5252

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/';
5458

5559
/**
5660
* @var string
@@ -92,10 +96,8 @@ public function __construct(ClientInterface $httpClient, array $config = [])
9296
public function cleanAddress($address)
9397
{
9498
$response = $this->query($this->prepareUri('clean/address'), [$address]);
99+
/** @var Address $result */
95100
$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-
}
99101

100102
return $result;
101103
}
@@ -112,10 +114,9 @@ public function cleanAddress($address)
112114
public function cleanPhone($phone)
113115
{
114116
$response = $this->query($this->prepareUri('clean/phone'), [$phone]);
117+
/** @var Phone $result */
115118
$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+
119120
return $result;
120121
}
121122

@@ -131,10 +132,8 @@ public function cleanPhone($phone)
131132
public function cleanPassport($passport)
132133
{
133134
$response = $this->query($this->prepareUri('clean/passport'), [$passport]);
135+
/** @var Passport $result */
134136
$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-
}
138137

139138
return $result;
140139
}
@@ -151,10 +150,8 @@ public function cleanPassport($passport)
151150
public function cleanName($name)
152151
{
153152
$response = $this->query($this->prepareUri('clean/name'), [$name]);
153+
/** @var Name $result */
154154
$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-
}
158155

159156
return $result;
160157
}
@@ -171,10 +168,8 @@ public function cleanName($name)
171168
public function cleanEmail($email)
172169
{
173170
$response = $this->query($this->prepareUri('clean/email'), [$email]);
171+
/** @var Email $result */
174172
$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-
}
178173

179174
return $result;
180175
}
@@ -191,10 +186,8 @@ public function cleanEmail($email)
191186
public function cleanDate($date)
192187
{
193188
$response = $this->query($this->prepareUri('clean/birthdate'), [$date]);
189+
/** @var Date $result */
194190
$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-
}
198191

199192
return $result;
200193
}
@@ -211,10 +204,8 @@ public function cleanDate($date)
211204
public function cleanVehicle($vehicle)
212205
{
213206
$response = $this->query($this->prepareUri('clean/vehicle'), [$vehicle]);
207+
/** @var Vehicle $result */
214208
$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-
}
218209

219210
return $result;
220211
}
@@ -332,7 +323,7 @@ protected function getValueByAnnotatedType(ReflectionProperty $property, $value)
332323
*/
333324
public function detectAddressByIp($ip)
334325
{
335-
$request = new Request('get', $this->baseUrlGeolocation . '?ip=' . $ip, [
326+
$request = new Request('get', $this->baseSuggestionsUrl . 'detectAddressByIp' . '?ip=' . $ip, [
336327
'Accept' => 'application/json',
337328
'Authorization' => 'Token ' . $this->token,
338329
]);
@@ -361,11 +352,34 @@ public function detectAddressByIp($ip)
361352
return null;
362353
}
363354

355+
/** @var Address $address */
364356
$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-
}
368357

369358
return $address;
370359
}
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+
}
371385
}

0 commit comments

Comments
 (0)