Skip to content

Commit

Permalink
Fix #31 Checks for nullable properties [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Kasatkin committed Mar 5, 2018
1 parent 8fadeae commit 1f6e97b
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 52 deletions.
112 changes: 63 additions & 49 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@
use Dadata\Response\Name;
use Dadata\Response\Passport;
use Dadata\Response\Phone;
use Dadata\Response\Vehicle;
use Dadata\Response\Suggestions\Party;
use Exception;
use Dadata\Response\Vehicle;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request;
use InvalidArgumentException;
use ReflectionClass;
use ReflectionProperty;
use RuntimeException;

/**
* Class Client
Expand All @@ -28,19 +24,21 @@ class Client
* Исходное значение распознано уверенно. Не требуется ручная проверка
*/
const QC_OK = 0;

/**
* Исходное значение распознано с допущениями или не распознано. Требуется ручная проверка
*/
const QC_UNSURE = 1;

/**
* Исходное значение пустое или заведомо "мусорное"
*/
const QC_INVALID = 2;

const METHOD_GET = 'GET';

const METHOD_POST = 'POST';

/**
* @var string
*/
Expand Down Expand Up @@ -92,8 +90,9 @@ public function __construct(ClientInterface $httpClient, array $config = [])
*
* @return Address
* @throws \ReflectionException
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws GuzzleException
*/
public function cleanAddress($address)
{
Expand All @@ -111,14 +110,15 @@ public function cleanAddress($address)
*
* @return Phone
* @throws \ReflectionException
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws GuzzleException
*/
public function cleanPhone($phone)
{
$response = $this->query($this->prepareUri('clean/phone'), [$phone]);
/** @var Phone $result */
$result = $this->populate(new Phone, $response);
$result = $this->populate(new Phone(), $response);

return $result;
}
Expand All @@ -130,8 +130,9 @@ public function cleanPhone($phone)
*
* @return Passport
* @throws \ReflectionException
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws GuzzleException
*/
public function cleanPassport($passport)
{
Expand All @@ -149,8 +150,9 @@ public function cleanPassport($passport)
*
* @return Name
* @throws \ReflectionException
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws GuzzleException
*/
public function cleanName($name)
{
Expand All @@ -168,14 +170,15 @@ public function cleanName($name)
*
* @return Email
* @throws \ReflectionException
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws GuzzleException
*/
public function cleanEmail($email)
{
$response = $this->query($this->prepareUri('clean/email'), [$email]);
/** @var Email $result */
$result = $this->populate(new Email, $response);
$result = $this->populate(new Email(), $response);

return $result;
}
Expand All @@ -187,14 +190,15 @@ public function cleanEmail($email)
*
* @return Date
* @throws \ReflectionException
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws GuzzleException
*/
public function cleanDate($date)
{
$response = $this->query($this->prepareUri('clean/birthdate'), [$date]);
/** @var Date $result */
$result = $this->populate(new Date, $response);
$result = $this->populate(new Date(), $response);

return $result;
}
Expand All @@ -206,14 +210,15 @@ public function cleanDate($date)
*
* @return Vehicle
* @throws \ReflectionException
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws GuzzleException
*/
public function cleanVehicle($vehicle)
{
$response = $this->query($this->prepareUri('clean/vehicle'), [$vehicle]);
/** @var Vehicle $result */
$result = $this->populate(new Vehicle, $response);
$result = $this->populate(new Vehicle(), $response);

return $result;
}
Expand All @@ -222,8 +227,9 @@ public function cleanVehicle($vehicle)
* Gets balance.
*
* @return float
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws GuzzleException
*/
public function getBalance()
{
Expand All @@ -235,13 +241,14 @@ public function getBalance()
* Requests API.
*
* @param string $uri
* @param array $params
* @param array $params
*
* @param string $method
*
* @return array
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws GuzzleException
*/
protected function query($uri, array $params = [], $method = self::METHOD_POST)
{
Expand All @@ -256,11 +263,11 @@ protected function query($uri, array $params = [], $method = self::METHOD_POST)
$result = json_decode($response->getBody(), true);

if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException('Error parsing response: ' . json_last_error_msg());
throw new \RuntimeException('Error parsing response: ' . json_last_error_msg());
}

if (empty($result)) {
throw new RuntimeException('Empty result');
throw new \RuntimeException('Empty result');
}

return array_shift($result);
Expand Down Expand Up @@ -288,9 +295,9 @@ protected function prepareUri($endpoint)
*/
protected function populate(AbstractResponse $object, array $data)
{
$reflect = new ReflectionClass($object);
$reflect = new \ReflectionClass($object);

$properties = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
$properties = $reflect->getProperties(\ReflectionProperty::IS_PUBLIC);

foreach ($properties as $property) {
if (array_key_exists($property->name, $data)) {
Expand All @@ -311,8 +318,12 @@ protected function populate(AbstractResponse $object, array $data)
*/
protected function populateParty (array $response)
{
list($name, $post) = array_values($response['data']['management']);
$management = new Party\ManagementDto($name, $post);
$management = null;
$managementData = $response['data']['management'];
if (is_array($managementData) && array_key_exists('name', $managementData) && array_key_exists('post', $managementData)) {
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);
Expand All @@ -326,7 +337,10 @@ protected function populateParty (array $response)
list($value, $unrestrictedValue) = array_values($response['data']['address']);
$simpleAddress = new Party\AddressDto($value, $unrestrictedValue);

$address = $this->populate(new Address(), $response['data']['address']['data']);
$address = null;
if (is_array($response['data']['address']['data'])) {
$address = $this->populate(new Address(), $response['data']['address']['data']);
}

return new Party\Party(
$response['value'],
Expand All @@ -350,11 +364,11 @@ protected function populateParty (array $response)
/**
* Guesses and converts property type by phpdoc comment.
*
* @param ReflectionProperty $property
* @param \ReflectionProperty $property
* @param mixed $value
* @return mixed
*/
protected function getValueByAnnotatedType(ReflectionProperty $property, $value)
protected function getValueByAnnotatedType(\ReflectionProperty $property, $value)
{
$comment = $property->getDocComment();
if (preg_match('/@var (.+?)(\|null)? /', $comment, $matches)) {
Expand All @@ -375,7 +389,8 @@ protected function getValueByAnnotatedType(ReflectionProperty $property, $value)
/**
* @param string $ip
* @return null|Address
* @throws Exception
* @throws \Exception
* @throws GuzzleException
*/
public function detectAddressByIp($ip)
{
Expand All @@ -389,19 +404,19 @@ public function detectAddressByIp($ip)
$result = json_decode($response->getBody(), true);

if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException('Error parsing response: ' . json_last_error_msg());
throw new \RuntimeException('Error parsing response: ' . json_last_error_msg());
}

if (!array_key_exists('location', $result)) {
throw new Exception('Required key "location" is missing');
throw new \Exception('Required key "location" is missing');
}

if (null === $result['location']) {
return null;
}

if (!array_key_exists('data', $result['location'])) {
throw new Exception('Required key "data" is missing');
throw new \Exception('Required key "data" is missing');
}

if (null === $result['location']['data']) {
Expand All @@ -427,6 +442,7 @@ public function detectAddressByIp($ip)
* @throws \ReflectionException
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws GuzzleException
*/
public function getAddressById($addressId)
{
Expand All @@ -450,10 +466,10 @@ public function getAddressById($addressId)
* ФИО руководителя компании;
* адресу до улицы.
*
* @param $party
* @param string $party
*
* @return \SplObjectStorage
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
* @throws \ReflectionException
* @throws \InvalidArgumentException
* @throws \RuntimeException
Expand Down Expand Up @@ -481,6 +497,4 @@ protected function prepareSuggestionsUri($endpoint)
{
return $this->baseSuggestionsUrl . $endpoint;
}


}
37 changes: 34 additions & 3 deletions src/Response/Suggestions/Party/Party.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Party extends AbstractResponse
*/
private $kpp;
/**
* @var ManagementDto Информация о руководителе
* @var ManagementDto|null Информация о руководителе
*/
private $management;
/**
Expand Down Expand Up @@ -76,11 +76,28 @@ class Party extends AbstractResponse
*/
private $simpleAddress;

/**
* @param $value
* @param $unrestrictedValue
* @param $kpp
* @param ManagementDto|null $management
* @param $branchType
* @param $type
* @param OpfDto $opf
* @param NameDto $name
* @param $inn
* @param $ogrn
* @param $okpo
* @param $okved
* @param StateDto $state
* @param AddressDto $simpleAddress
* @param Address|null $address
*/
public function __construct(
$value,
$unrestrictedValue,
$kpp,
ManagementDto $management,
$management,
$branchType,
$type,
OpfDto $opf,
Expand All @@ -91,11 +108,18 @@ public function __construct(
$okved,
StateDto $state,
AddressDto $simpleAddress,
Address $address
$address
) {
$this->value = $value;
$this->unrestrictedValue = $unrestrictedValue;
$this->kpp = $kpp;
if ($management !== null && !$management instanceof ManagementDto) {
throw new \InvalidArgumentException(sprintf(
'Argument $management passed to Part::__construct must be instance of %s, %s given',
ManagementDto::class,
gettype($management)
));
}
$this->management = $management;
$this->branchType = $branchType;
$this->type = $type;
Expand All @@ -106,6 +130,13 @@ public function __construct(
$this->okpo = $okpo;
$this->okved = $okved;
$this->state = $state;
if ($address !== null && !$address instanceof Address) {
throw new \InvalidArgumentException(sprintf(
'Argument $address passed to Part::__construct must be instance of %s, %s given',
Address::class,
gettype($address)
));
}
$this->address = $address;
$this->simpleAddress = $simpleAddress;
}
Expand Down

0 comments on commit 1f6e97b

Please sign in to comment.