Skip to content

Commit

Permalink
Merge pull request #11 from vitaly-grechkin/master
Browse files Browse the repository at this point in the history
getAddressById added to the client
  • Loading branch information
Sergey Kasatkin authored Oct 23, 2017
2 parents a21e74d + 374aa86 commit e484956
Showing 1 changed file with 40 additions and 26 deletions.
66 changes: 40 additions & 26 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ class Client
*/
protected $baseUrl = 'https://dadata.ru/api';

protected $baseUrlGeolocation = 'https://suggestions.dadata.ru/suggestions/api/4_1/rs/detectAddressByIp';
/**
* Suggestions url
* @var string
*/
protected $baseSuggestionsUrl = 'https://suggestions.dadata.ru/suggestions/api/4_1/rs/';

/**
* @var string
Expand Down Expand Up @@ -92,10 +96,8 @@ public function __construct(ClientInterface $httpClient, array $config = [])
public function cleanAddress($address)
{
$response = $this->query($this->prepareUri('clean/address'), [$address]);
/** @var Address $result */
$result = $this->populate(new Address, $response);
if (!$result instanceof Address) {
throw new RuntimeException('Unexpected populate result: ' . get_class($result). '. Expected: ' . Address::class);
}

return $result;
}
Expand All @@ -112,10 +114,9 @@ public function cleanAddress($address)
public function cleanPhone($phone)
{
$response = $this->query($this->prepareUri('clean/phone'), [$phone]);
/** @var Phone $result */
$result = $this->populate(new Phone, $response);
if (!$result instanceof Phone) {
throw new RuntimeException('Unexpected populate result: ' . get_class($result). '. Expected: ' . Phone::class);
}

return $result;
}

Expand All @@ -131,10 +132,8 @@ public function cleanPhone($phone)
public function cleanPassport($passport)
{
$response = $this->query($this->prepareUri('clean/passport'), [$passport]);
/** @var Passport $result */
$result = $this->populate(new Passport(), $response);
if (!$result instanceof Passport) {
throw new RuntimeException('Unexpected populate result: ' . get_class($result). '. Expected: ' . Passport::class);
}

return $result;
}
Expand All @@ -151,10 +150,8 @@ public function cleanPassport($passport)
public function cleanName($name)
{
$response = $this->query($this->prepareUri('clean/name'), [$name]);
/** @var Name $result */
$result = $this->populate(new Name(), $response);
if (!$result instanceof Name) {
throw new RuntimeException('Unexpected populate result: ' . get_class($result). '. Expected: ' . Name::class);
}

return $result;
}
Expand All @@ -171,10 +168,8 @@ public function cleanName($name)
public function cleanEmail($email)
{
$response = $this->query($this->prepareUri('clean/email'), [$email]);
/** @var Email $result */
$result = $this->populate(new Email, $response);
if (!$result instanceof Email) {
throw new RuntimeException('Unexpected populate result: ' . get_class($result). '. Expected: ' . Email::class);
}

return $result;
}
Expand All @@ -191,10 +186,8 @@ public function cleanEmail($email)
public function cleanDate($date)
{
$response = $this->query($this->prepareUri('clean/birthdate'), [$date]);
/** @var Date $result */
$result = $this->populate(new Date, $response);
if (!$result instanceof Date) {
throw new RuntimeException('Unexpected populate result: ' . get_class($result). '. Expected: ' . Date::class);
}

return $result;
}
Expand All @@ -211,10 +204,8 @@ public function cleanDate($date)
public function cleanVehicle($vehicle)
{
$response = $this->query($this->prepareUri('clean/vehicle'), [$vehicle]);
/** @var Vehicle $result */
$result = $this->populate(new Vehicle, $response);
if (!$result instanceof Vehicle) {
throw new RuntimeException('Unexpected populate result: ' . get_class($result). '. Expected: ' . Vehicle::class);
}

return $result;
}
Expand Down Expand Up @@ -332,7 +323,7 @@ protected function getValueByAnnotatedType(ReflectionProperty $property, $value)
*/
public function detectAddressByIp($ip)
{
$request = new Request('get', $this->baseUrlGeolocation . '?ip=' . $ip, [
$request = new Request('get', $this->baseSuggestionsUrl . 'detectAddressByIp' . '?ip=' . $ip, [
'Accept' => 'application/json',
'Authorization' => 'Token ' . $this->token,
]);
Expand Down Expand Up @@ -361,11 +352,34 @@ public function detectAddressByIp($ip)
return null;
}

/** @var Address $address */
$address = $this->populate(new Address, $result['location']['data']);
if (!$address instanceof Address) {
throw new RuntimeException('Unexpected populate result: ' . get_class($result). '. Expected: ' . Address::class);
}

return $address;
}

/**
* Метод возвращает арес по его коду КЛАДР или ФИАС
*
* Dadata comment: Ищет до улицы включительно, при поиске по коду дома возвращает пустой ответ.
* Так сделано намеренно: КЛАДР-коды и ФИАС-коды домов постоянно изменяются, поэтому хранить их ненадежно.
* Рекомендуем использовать связку «ФИАС-код улицы + домовая часть отдельно»
*
* @param string $addressId
*
* @return AbstractResponse|Address|null
*/
public function getAddressById($addressId)
{
$response = $this->query($this->baseSuggestionsUrl . 'findById/address', ['query' => $addressId]);

if (is_array($response) && 0 < count($response)) {
/** @var Address $address */
$address = $this->populate(new Address, array_shift($response)['data']);

return $address;
}

return null;
}
}

0 comments on commit e484956

Please sign in to comment.