Skip to content

Commit

Permalink
Merge pull request #12 from camaxtly/master
Browse files Browse the repository at this point in the history
Подсказки для юр лиц и ИП
  • Loading branch information
Sergey Kasatkin authored Oct 24, 2017
2 parents e484956 + 1c02965 commit 8fadeae
Show file tree
Hide file tree
Showing 8 changed files with 638 additions and 1 deletion.
103 changes: 102 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Dadata\Response\Passport;
use Dadata\Response\Phone;
use Dadata\Response\Vehicle;
use Dadata\Response\Suggestions\Party;
use Exception;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7\Request;
Expand Down Expand Up @@ -90,6 +91,7 @@ public function __construct(ClientInterface $httpClient, array $config = [])
* @param string $address
*
* @return Address
* @throws \ReflectionException
* @throws RuntimeException
* @throws InvalidArgumentException
*/
Expand All @@ -108,6 +110,7 @@ public function cleanAddress($address)
* @param string $phone
*
* @return Phone
* @throws \ReflectionException
* @throws RuntimeException
* @throws InvalidArgumentException
*/
Expand All @@ -126,6 +129,7 @@ public function cleanPhone($phone)
* @param string $passport
*
* @return Passport
* @throws \ReflectionException
* @throws RuntimeException
* @throws InvalidArgumentException
*/
Expand All @@ -144,6 +148,7 @@ public function cleanPassport($passport)
* @param string $name
*
* @return Name
* @throws \ReflectionException
* @throws RuntimeException
* @throws InvalidArgumentException
*/
Expand All @@ -162,6 +167,7 @@ public function cleanName($name)
* @param string $email
*
* @return Email
* @throws \ReflectionException
* @throws RuntimeException
* @throws InvalidArgumentException
*/
Expand All @@ -180,6 +186,7 @@ public function cleanEmail($email)
* @param string $date
*
* @return Date
* @throws \ReflectionException
* @throws RuntimeException
* @throws InvalidArgumentException
*/
Expand All @@ -198,6 +205,7 @@ public function cleanDate($date)
* @param string $vehicle
*
* @return Vehicle
* @throws \ReflectionException
* @throws RuntimeException
* @throws InvalidArgumentException
*/
Expand Down Expand Up @@ -273,8 +281,10 @@ protected function prepareUri($endpoint)
* Populates object with data.
*
* @param AbstractResponse $object
* @param array $data
* @param array $data
*
* @return AbstractResponse
* @throws \ReflectionException
*/
protected function populate(AbstractResponse $object, array $data)
{
Expand All @@ -291,6 +301,52 @@ protected function populate(AbstractResponse $object, array $data)
return $object;
}

/**
* Создаем объект ответа по подсказкам организации
*
* @param array $response
*
* @return Party\Party
* @throws \ReflectionException
*/
protected function populateParty (array $response)
{
list($name, $post) = array_values($response['data']['management']);
$management = new Party\ManagementDto($name, $post);

list($code, $full, $short) = array_values($response['data']['opf']);
$opf = new Party\OpfDto($code, $full, $short);

list($fullWithOpf, $shortWithOpf, $latin, $full, $short) = array_values($response['data']['name']);
$name = new Party\NameDto($fullWithOpf, $shortWithOpf, $latin, $full, $short);

list($status, $actualityDate, $registrationDate, $liquidationDate) = array_values($response['data']['state']);
$state = new Party\StateDto($status, $actualityDate, $registrationDate, $liquidationDate);

list($value, $unrestrictedValue) = array_values($response['data']['address']);
$simpleAddress = new Party\AddressDto($value, $unrestrictedValue);

$address = $this->populate(new Address(), $response['data']['address']['data']);

return new Party\Party(
$response['value'],
$response['unrestricted_value'],
$response['data']['kpp'],
$management,
$response['data']['branch_type'],
$response['data']['type'],
$opf,
$name,
$response['data']['inn'],
$response['data']['ogrn'],
$response['data']['okpo'],
$response['data']['okved'],
$state,
$simpleAddress,
$address
);
}

/**
* Guesses and converts property type by phpdoc comment.
*
Expand Down Expand Up @@ -368,6 +424,9 @@ public function detectAddressByIp($ip)
* @param string $addressId
*
* @return AbstractResponse|Address|null
* @throws \ReflectionException
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/
public function getAddressById($addressId)
{
Expand All @@ -382,4 +441,46 @@ public function getAddressById($addressId)

return null;
}

/**
* Ищет организации и индивидуальных предпринимателей:
* по ИНН / ОГРН;
* названию (полному, краткому, латинскому);
* ФИО (для индивидуальных предпринимателей);
* ФИО руководителя компании;
* адресу до улицы.
*
* @param $party
*
* @return \SplObjectStorage
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \ReflectionException
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
public function suggestParties($party)
{
$response = $this->query($this->prepareSuggestionsUri('suggest/party'), ['query' => $party]);
$collection = new \SplObjectStorage();

foreach ($response as $arParty) {
$party = $this->populateParty($arParty);
$collection->attach($party);
}
return $collection;
}

/**
* Prepares suggest URI for the request.
*
* @param string $endpoint
*
* @return string
*/
protected function prepareSuggestionsUri($endpoint)
{
return $this->baseSuggestionsUrl . $endpoint;
}


}
13 changes: 13 additions & 0 deletions src/Response/Suggestions/AbstractResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Dadata\Response\Suggestions;

abstract class AbstractResponse
{
protected $rawResponse;

public function getRawResponse()
{
return $this->rawResponse;
}
}
33 changes: 33 additions & 0 deletions src/Response/Suggestions/Party/AddressDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Dadata\Response\Suggestions\Party;

class AddressDto
{
private $value;
private $unrestrictedValue;

public function __construct(
$value,
$unrestrictedValue
) {
$this->value = $value;
$this->unrestrictedValue = $unrestrictedValue;
}

/**
* @return string
*/
public function getValue()
{
return $this->value;
}

/**
* @return string
*/
public function getUnrestrictedValue()
{
return $this->unrestrictedValue;
}
}
41 changes: 41 additions & 0 deletions src/Response/Suggestions/Party/ManagementDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Dadata\Response\Suggestions\Party;

class ManagementDto
{
/**
* @var string ФИО руководителя
*/
private $name;
/**
* @var string Должность руководителя
*/
private $post;

public function __construct($name, $post)
{
$this->name = $name;
$this->post = $post;
}

/**
* Получить ФИО руководителя
*
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* Получить должность руководителя
*
* @return string
*/
public function getPost()
{
return $this->post;
}
}
81 changes: 81 additions & 0 deletions src/Response/Suggestions/Party/NameDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Dadata\Response\Suggestions\Party;

class NameDto
{
/**
* @var string Полное наименование с ОПФ
*/
private $fullWithOpf;
/**
* @var string Краткое наименование с ОПФ
*/
private $shortWithOpf;
/**
* @var string Наименование на латинице
*/
private $latin;
/**
* @var string Полное наименование
*/
private $full;
/**
* @var string Краткое наименование
*/
private $short;

public function __construct(
$fullWithOpf,
$shortWithOpf,
$latin,
$full,
$short
) {
$this->fullWithOpf = $fullWithOpf;
$this->shortWithOpf = $shortWithOpf;
$this->latin = $latin;
$this->full = $full;
$this->short = $short;
}

/**
* @return string
*/
public function getFullWithOpf()
{
return $this->fullWithOpf;
}

/**
* @return string
*/
public function getShortWithOpf()
{
return $this->shortWithOpf;
}

/**
* @return string
*/
public function getLatin()
{
return $this->latin;
}

/**
* @return string
*/
public function getFull()
{
return $this->full;
}

/**
* @return string
*/
public function getShort()
{
return $this->short;
}
}
Loading

0 comments on commit 8fadeae

Please sign in to comment.