Skip to content

Commit 242c99a

Browse files
PussPuss
Puss
authored and
Puss
committed
refactor and add declare strict
1 parent fab6026 commit 242c99a

15 files changed

+122
-48
lines changed

index.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@
33

44
use FnsService\Factory\FNS;
55

6+
/**
7+
* Если ФИО целиком
8+
*/
9+
$fnsResponse = FNS::parse("Иванов Иван", "02.06.1999", "1234 123456")->getInn();
10+
/**
11+
* Если ФИО сложное
12+
*/
13+
$fnsResponse = FNS::direct("Галиев", "Шавкат", "Тимур Угли", "02.06.1999", "FA123456")->getInn();
614

7-
$fnsResponse = FNS::make("Чертова Юлия Алексеевна", "03.01.2004", "6017 270691")->getInn();
8-
if ($fnsResponse->hasError()) {
9-
var_dump($fnsResponse->getError('ru'));
15+
if ($fnsResponse->hasErrors()) {
16+
var_dump($fnsResponse->getErrors());
1017
} else {
1118
var_dump($fnsResponse->inn);
1219
}

src/Contracts/FnsEntity.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace FnsService\Contracts;
5+
6+
/**
7+
* Interface FnsEntity
8+
* Defines methods for encoding URL and converting entity to array.
9+
* @package FnsService\Contracts
10+
*/
11+
interface FnsEntity
12+
{
13+
/**
14+
* Encode the entity data for URL usage.
15+
* @return string The URL-encoded entity data
16+
*/
17+
public function urlEncode(): string;
18+
19+
/**
20+
* Convert the entity to an array representation.
21+
* @return array The entity data as an array
22+
*/
23+
public function toArray(): array;
24+
}

src/Contracts/FnsEntityInterface.php

-10
This file was deleted.

src/Contracts/FullName.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace FnsService\Contracts;
46

57
abstract class FullName

src/Contracts/Localization.php

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace FnsService\Contracts;
45

5-
6+
/**
7+
* Interface Localization
8+
* Defines methods for localizing arrays based on a specific language.
9+
* @package FnsService\Contracts
10+
*/
611
interface Localization
712
{
13+
/**
14+
* Set the localization language for the localization process.
15+
* @param string $language The language code to set
16+
* @return self The instance of the Localization interface
17+
*/
818
public function setLocalization(string $language): self;
19+
20+
/**
21+
* Localize an array based on the set language.
22+
* @param array $array The array to be localized
23+
* @return array The localized array
24+
*/
925
public function localize(array $array): array;
10-
}
26+
}

src/Contracts/Response.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace FnsService\Contracts;
46

57
use Exception;

src/Entity/DirectFullName.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace FnsService\Entity;
46

57
use FnsService\Contracts\FullName;

src/Entity/FnsEntity.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace FnsService\Entity;
46

57
use DateTime;
6-
use FnsService\Contracts\FnsEntity as ContractsFnsEntity;
8+
use FnsService\Contracts\FnsEntity as ContractFnsEntity;
79
use FnsService\Contracts\FullName as ContractFullName;
810

911
/**
1012
* Class FnsEntity
1113
*/
12-
class FnsEntity implements ContractsFnsEntity
14+
class FnsEntity implements ContractFnsEntity
1315
{
1416
/**
1517
* @var FullName The full name of the entity.

src/Entity/FnsResponse.php

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace FnsService\Entity;
46

57
use FnsService\Contracts\Response;
@@ -23,13 +25,19 @@ public function getStatus(): int
2325

2426
/**
2527
* Get the error object of the response, if any.
28+
* @param string $languages The language for error localization
2629
* @return object|null The error object or null if not set
2730
*/
28-
public function getError($languages = 'ru'): ?object
31+
public function getErrors($languages = 'ru'): ?object
2932
{
3033
return $this->ERRORS ? (object)$this->prepareErrors($languages) : null;
3134
}
3235

36+
/**
37+
* Prepare errors for localization based on language.
38+
* @param string $languages The language for error localization
39+
* @return array The prepared errors array
40+
*/
3341
private function prepareErrors($languages): array
3442
{
3543
if ($languages) {
@@ -41,18 +49,18 @@ private function prepareErrors($languages): array
4149

4250
/**
4351
* Get the request ID from the response.
44-
* @return mixed The request ID
52+
* @return string|null The request ID
4553
*/
46-
public function getRequestId()
54+
public function getRequestId(): ?string
4755
{
4856
return $this->requestId;
4957
}
5058

5159
/**
5260
* Get the INN (Taxpayer Identification Number) from the response.
53-
* @return mixed The INN
61+
* @return string|null The INN
5462
*/
55-
public function getInn()
63+
public function getInn(): ?string
5664
{
5765
return $this->inn;
5866
}
@@ -61,8 +69,8 @@ public function getInn()
6169
* Check if the response has an error.
6270
* @return object|null The error object or null if no error
6371
*/
64-
public function hasError()
72+
public function hasErrors(): ?object
6573
{
66-
return $this->getError();
74+
return $this->getErrors();
6775
}
6876
}

src/Entity/ParseFullName.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace FnsService\Entity;
46

57
use FnsService\Contracts\FullName;

src/Factory/Client.php

+2-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace FnsService\Factory;
46

57
use Exception;
@@ -46,26 +48,6 @@ public function setTimeout(int $timeout): self
4648
return $this;
4749
}
4850

49-
/**
50-
* Send a GET request and return the response as an FnsResponse object.
51-
* @return FnsResponse The response object
52-
* @throws Exception If there is a cURL error
53-
*/
54-
public function get(): FnsResponse
55-
{
56-
$ch = curl_init();
57-
curl_setopt($ch, CURLOPT_URL, $this->url);
58-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
59-
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
60-
$response = curl_exec($ch);
61-
if ($response === false) {
62-
$error = curl_error($ch);
63-
curl_close($ch);
64-
throw new Exception('Curl error: ' . $error);
65-
}
66-
curl_close($ch);
67-
return new FnsResponse($response);
68-
}
6951

7052
/**
7153
* Send a POST request and return the response as an FnsResponse object.

src/Factory/FNS.php

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace FnsService\Factory;
46

57
use DateTime;
68
use FnsService\Contracts\FullName;
9+
use FnsService\Entity\DirectFullName;
710
use FnsService\Entity\FnsEntity;
811
use FnsService\Entity\FnsResponse;
912
use FnsService\Entity\ParseFullName;
@@ -27,8 +30,11 @@ class FNS
2730
* @param string $passport The passport number of the individual
2831
* @return self
2932
*/
30-
public static function make(string $fullName, string $birthDate, string $passport): self
31-
{
33+
public static function parse(
34+
string $fullName,
35+
string $birthDate,
36+
string $passport
37+
): self {
3238
$fns = new self();
3339
$fns->fullName = new ParseFullName($fullName);
3440
$fns->birthDay = new DateTime($birthDate);
@@ -37,6 +43,30 @@ public static function make(string $fullName, string $birthDate, string $passpor
3743
return $fns;
3844
}
3945

46+
/**
47+
* Create a new instance of FNS with the provided direct full name details.
48+
* @param string $lastName The last name of the individual
49+
* @param string $firstName The first name of the individual
50+
* @param string $patronimyc The patronymic name of the individual
51+
* @param string $birthDate The birthdate of the individual
52+
* @param string $passport The passport number of the individual
53+
* @return self
54+
*/
55+
public static function direct(
56+
string $lastName,
57+
string $firstName,
58+
string $patronymic,
59+
string $birthDate,
60+
string $passport
61+
): self {
62+
$fns = new self();
63+
$fns->fullName = new DirectFullName($lastName, $firstName, $patronymic);
64+
$fns->birthDay = new DateTime($birthDate);
65+
$fns->passport = $passport;
66+
$fns->entity = new FnsEntity($fns->fullName, $fns->birthDay, $fns->passport);
67+
return $fns;
68+
}
69+
4070
/**
4171
* Get the INN (Taxpayer Identification Number) using the passport details.
4272
* @return FnsResponse The response object containing the INN information

src/Factory/Localization.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace FnsService\Factory;
46

7+
use FnsService\Contracts\Localization as ContractsLocalization;
58
use FnsService\Services\LocalizationService;
69

710
/**
@@ -15,7 +18,7 @@ class Localization
1518
*
1619
* @return LocalizationService A new instance of LocalizationService.
1720
*/
18-
static public function make()
21+
static public function make(): ContractsLocalization
1922
{
2023
return new LocalizationService();
2124
}

src/Services/FnsService.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace FnsService\Services;
46

57
use FnsService\Contracts\FnsEntity;

src/Services/LocalizationService.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace FnsService\Services;
46

57
use Exception;
@@ -12,7 +14,7 @@
1214
*/
1315
class LocalizationService implements Localization
1416
{
15-
private $language = 'en';
17+
private $language = 'ru';
1618
private $languages = ['ru', 'en'];
1719

1820
/**

0 commit comments

Comments
 (0)