Skip to content

Commit

Permalink
[INFR-459] поддержка PHP 8.2, независимость от порядка полей в ответах
Browse files Browse the repository at this point in the history
  • Loading branch information
smirnov-e committed Apr 28, 2023
1 parent bb63adb commit 4153872
Show file tree
Hide file tree
Showing 8 changed files with 982 additions and 41 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
/.idea
/composer.lock
/vendor
/.phpunit.cache
19 changes: 14 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
{
"name": "kstkn/dadata",
"name": "flowwow/dadata",
"description": "Dadata API client (https://dadata.ru)",
"minimum-stability": "stable",
"license": "MIT",
"support": {
"issues": "https://github.com/kstkn/dadata/issues",
"source": "https://github.com/kstkn/dadata"
"issues": "https://github.com/flowwow/dadata/issues",
"source": "https://github.com/flowwow/dadata"
},
"authors": [
{
"name": "Sergei Kasatkin"
}
],
"require": {
"php": ">=5.5.0",
"guzzlehttp/guzzle": "^6.2"
"php": ">=7.3.0",
"guzzlehttp/guzzle": "^6.2|^7.0",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^8.0|^9.0"
},
"autoload": {
"psr-4": {
"Dadata\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Dadata\\Tests\\": "tests/"
}
}
}
30 changes: 30 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
bootstrap="tests/bootstrap.php"
cacheResultFile=".phpunit.cache/test-results"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
convertDeprecationsToExceptions="true"
failOnRisky="true"
failOnWarning="true"
verbose="true">
<php>
<ini name="error_reporting" value="E_ALL"/>
</php>
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>

<coverage cacheDirectory=".phpunit.cache/code-coverage"
processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
87 changes: 52 additions & 35 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ public function cleanVehicle($vehicle)
public function getBalance()
{
$response = $this->query($this->prepareUri('profile/balance'), [], self::METHOD_GET);
return (float) $response;

return (float)$response;
}

/**
Expand All @@ -254,17 +255,13 @@ protected function query($uri, array $params = [], $method = self::METHOD_POST)
{
$request = new Request($method, $uri, [
'Content-Type' => 'application/json',
'Authorization' => 'Token ' . $this->token,
'Authorization' => 'Token '.$this->token,
'X-Secret' => $this->secret,
], 0 < count($params) ? json_encode($params) : null);

$response = $this->httpClient->send($request, $this->httpOptions);

$result = json_decode($response->getBody(), true);

if (json_last_error() !== JSON_ERROR_NONE) {
throw new \RuntimeException('Error parsing response: ' . json_last_error_msg());
}
$result = json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);

if (empty($result)) {
throw new \RuntimeException('Empty result');
Expand All @@ -281,14 +278,14 @@ protected function query($uri, array $params = [], $method = self::METHOD_POST)
*/
protected function prepareUri($endpoint)
{
return $this->baseUrl . '/' . $this->version . '/' . $endpoint;
return $this->baseUrl.'/'.$this->version.'/'.$endpoint;
}

/**
* Populates object with data.
*
* @param AbstractResponse $object
* @param array $data
* @param array $data
*
* @return AbstractResponse
* @throws \ReflectionException
Expand Down Expand Up @@ -319,38 +316,57 @@ protected function populate(AbstractResponse $object, array $data)
protected function populateParty(array $response)
{
$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);
if (array_key_exists('management', $response['data'])
&& is_array($response['data']['management'])
&& array_key_exists('name', $response['data']['management'])
&& array_key_exists('post', $response['data']['management'])
) {
$management = new Party\ManagementDto(
$response['data']['management']['name'],
$response['data']['management']['post']
);
}

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

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

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

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

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

return new Party\Party(
$response['value'],
$response['unrestricted_value'],
$response['data']['kpp'],
$response['data']['kpp'] ?? null,
$management,
$response['data']['branch_type'],
$response['data']['branch_type'] ?? null,
$response['data']['type'],
$opf,
$name,
Expand All @@ -368,7 +384,7 @@ protected function populateParty(array $response)
* Guesses and converts property type by phpdoc comment.
*
* @param \ReflectionProperty $property
* @param mixed $value
* @param mixed $value
* @return mixed
*/
protected function getValueByAnnotatedType(\ReflectionProperty $property, $value)
Expand All @@ -378,10 +394,10 @@ protected function getValueByAnnotatedType(\ReflectionProperty $property, $value
switch ($matches[1]) {
case 'integer':
case 'int':
$value = (int) $value;
$value = (int)$value;
break;
case 'float':
$value = (float) $value;
$value = (float)$value;
break;
}
}
Expand All @@ -397,17 +413,17 @@ protected function getValueByAnnotatedType(\ReflectionProperty $property, $value
*/
public function detectAddressByIp($ip)
{
$request = new Request('get', $this->baseSuggestionsUrl . 'detectAddressByIp' . '?ip=' . $ip, [
$request = new Request('get', $this->baseSuggestionsUrl.'detectAddressByIp'.'?ip='.$ip, [
'Accept' => 'application/json',
'Authorization' => 'Token ' . $this->token,
'Authorization' => 'Token '.$this->token,
]);

$response = $this->httpClient->send($request);

$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)) {
Expand Down Expand Up @@ -449,7 +465,7 @@ public function detectAddressByIp($ip)
*/
public function getAddressById($addressId)
{
$response = $this->query($this->baseSuggestionsUrl . 'findById/address', ['query' => $addressId]);
$response = $this->query($this->baseSuggestionsUrl.'findById/address', ['query' => $addressId]);

if (is_array($response) && 0 < count($response)) {
/** @var Address $address */
Expand Down Expand Up @@ -486,6 +502,7 @@ public function suggestParties($party)
$party = $this->populateParty($arParty);
$collection->attach($party);
}

return $collection;
}

Expand All @@ -498,6 +515,6 @@ public function suggestParties($party)
*/
protected function prepareSuggestionsUri($endpoint)
{
return $this->baseSuggestionsUrl . $endpoint;
return $this->baseSuggestionsUrl.$endpoint;
}
}
Loading

0 comments on commit 4153872

Please sign in to comment.