Skip to content

Commit e0d16b9

Browse files
PussPuss
Puss
authored and
Puss
committed
init composer and create src
0 parents  commit e0d16b9

13 files changed

+496
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor/*

composer.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "supasta/fns-service",
3+
"description": "Find out the INN using passport data using the official service of the FNS",
4+
"autoload": {
5+
"psr-4": {
6+
"Supasta\\FnsService\\": "src/"
7+
}
8+
},
9+
"authors": [
10+
{
11+
"name": "Supasta",
12+
"email": "[email protected]"
13+
}
14+
],
15+
"minimum-stability": "stable",
16+
"require": {}
17+
}

index.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use Supasta\FnsService\Entity\DirectFullName;
4+
use Supasta\FnsService\Entity\FnsEntity;
5+
use Supasta\FnsService\Entity\ParseFullName;
6+
use Supasta\FnsService\Factory\Client;
7+
use Supasta\FnsService\Service\FnsService;
8+
9+
require __DIR__ . '/vendor/autoload.php';
10+
11+
// $fullName = new ParseFullName("Буртикd Валерий Владимирович");
12+
13+
// $entity = new FnsEntity($fullName, new DateTime('06.02.1991'), identifyDocument: "2510 503246");
14+
15+
// $fnsService = (new FnsService())->findInnByPassport($entity);
16+
17+
18+
// var_dump($fnsService);
19+
$client = new Client('https://service.nalog.ru/inn-new-proc.json?fam=%D0%91%D1%83%D1%80%D1%82%D0%B8%D0%BA%D0%BE%D0%B2&nam=%D0%92%D0%B0%D0%BB%D0%B5%D1%80%D0%B8%D0%B9&otch=%D0%92%D0%BB%D0%B0%D0%B4%D0%B8%D0%BC%D0%B8%D1%80%D0%BE%D0%B2%D0%B8%D1%87&docno=2510+503246&c=find&captcha=&captchaToken=&bdate=06.02.1991&doctype=10');
20+
$response = $client->post();
21+
22+
var_dump($response);
23+
24+
// // Initialize
25+
// $ch = curl_init();
26+
27+
// // Set the URL to fetch
28+
// curl_setopt($ch, CURLOPT_URL, 'https://jsonplaceholder.typicode.com/posts/1');
29+
30+
// // Set the return transfer to true so that the response is returned as a string
31+
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
32+
33+
// // Execute the request
34+
// $response = curl_exec($ch);
35+
36+
// // Close cURL session
37+
// curl_close($ch);
38+
39+
// // Output the response
40+
// echo $response;

src/Contract/FnsEntityInterface.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Supasta\FnsService\Contract;
4+
5+
interface FnsEntityInterface
6+
{
7+
public function urlEncode(): string;
8+
9+
public function toArray(): array;
10+
}

src/Contract/FnsResponse.php

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Supasta\FnsService\Contract;
4+
5+
6+
abstract class FnsResponse
7+
{
8+
protected $data = [];
9+
10+
protected function has($key): bool
11+
{
12+
return array_key_exists($key, $this->data);
13+
}
14+
15+
protected function isEmpty(): bool
16+
{
17+
return empty($this->data);
18+
}
19+
20+
public function __get($key)
21+
{
22+
return $this->has($key) ? $this->data[$key] : null;
23+
}
24+
25+
public function __set($key, $value)
26+
{
27+
$this->data[$key] = $value;
28+
}
29+
30+
public function getStatus(): int
31+
{
32+
return (int)$this->STATUS ?? 200;
33+
}
34+
35+
public function getError(): ?object
36+
{
37+
return $this->ERRORS;
38+
}
39+
40+
public function getRequestId()
41+
{
42+
return $this->requestId;
43+
}
44+
45+
public function getInn()
46+
{
47+
return $this->inn;
48+
}
49+
}

src/Contract/FullName.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Supasta\FnsService\Contract;
4+
5+
abstract class FullName
6+
{
7+
/**
8+
* @var string The last name of the person.
9+
*/
10+
public $lastName;
11+
12+
/**
13+
* @var string The first name of the person.
14+
*/
15+
public $firstName;
16+
17+
/**
18+
* @var string|null The patronymic (middle name) of the person.
19+
*/
20+
public $patronymic;
21+
}

src/Entity/DirectFullName.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Supasta\FnsService\Entity;
4+
5+
use Supasta\FnsService\Contract\FullName;
6+
7+
class DirectFullName extends FullName
8+
{
9+
/**
10+
* FullName constructor.
11+
*
12+
* @param string $lastName The last name of the person.
13+
* @param string $firstName The first name of the person.
14+
* @param string $patronymic The patronymic of the person.
15+
*/
16+
public function __construct(?string $lastName, ?string $firstName, ?string $patronymic)
17+
{
18+
$this->lastName = $lastName;
19+
$this->firstName = $firstName;
20+
$this->patronymic = $patronymic;
21+
}
22+
}

src/Entity/FnsEntity.php

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace Supasta\FnsService\Entity;
4+
5+
use DateTime;
6+
use Supasta\FnsService\Contract\FnsEntityInterface;
7+
use Supasta\FnsService\Contract\FullName as ContractFullName;
8+
9+
/**
10+
* Class FnsEntity
11+
*/
12+
class FnsEntity implements FnsEntityInterface
13+
{
14+
/**
15+
* @var FullName The full name of the entity.
16+
*/
17+
private ContractFullName $fullName;
18+
19+
/**
20+
* @var DateTime The birth date of the entity.
21+
*/
22+
private DateTime $birthDay;
23+
24+
/**
25+
* @var int The document type of the entity.
26+
*/
27+
private $docType = 10;
28+
29+
/**
30+
* @var string|int The identification document of the entity.
31+
*/
32+
private $identifyDocument;
33+
34+
/**
35+
* @var array The query string parameters for the FNS service.
36+
*/
37+
private $queryString;
38+
39+
/**
40+
* FnsEntity constructor.
41+
*
42+
* @param FullName $fullName The full name of the entity.
43+
* @param DateTime $birthDay The birth date of the entity.
44+
* @param FnsDocumentType $docType The document type of the entity.
45+
* @param mixed $identifyDocument The identification document of the entity.
46+
*/
47+
public function __construct(ContractFullName $fullName, DateTime $birthDay, $identifyDocument)
48+
{
49+
$this->fullName = $fullName;
50+
$this->birthDay = $birthDay;
51+
$this->identifyDocument = $identifyDocument;
52+
$this->prepare();
53+
}
54+
55+
/**
56+
* Prepare the query string parameters for the FNS service.
57+
*
58+
* @return self
59+
*/
60+
protected function prepare(): self
61+
{
62+
$this->queryString = [
63+
'fam' => $this->fullName->lastName,
64+
'nam' => $this->fullName->firstName,
65+
'otch' => $this->fullName->patronymic,
66+
'docno' => $this->identifyDocument,
67+
'c' => 'find',
68+
'captcha' => '',
69+
'captchaToken' => '',
70+
'bdate' => $this->birthDay->format('d.m.Y'),
71+
'doctype' => $this->docType
72+
];
73+
74+
if (!$this->fullName->patronymic) {
75+
$this->queryString['opt_otch'] = '1';
76+
}
77+
78+
return $this;
79+
}
80+
81+
/**
82+
* URL-encode the query string parameters.
83+
*
84+
* @return string The URL-encoded query string.
85+
*/
86+
public function urlEncode(): string
87+
{
88+
return http_build_query($this->queryString);
89+
}
90+
91+
/**
92+
* Get the query string parameters as an array.
93+
*
94+
* @return array The query string parameters as an array.
95+
*/
96+
public function toArray(): array
97+
{
98+
return $this->queryString;
99+
}
100+
}

src/Entity/FnsErrorResponse.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Supasta\FnsService\Entity;
4+
5+
use Exception;
6+
use Supasta\FnsService\Contract\FnsResponse;
7+
8+
class FnsErrorResponse extends FnsResponse
9+
{
10+
private $responseObject;
11+
12+
public function __construct(mixed $response)
13+
{
14+
if (is_string($response)) {
15+
$this->responseObject = json_decode($response);
16+
if (json_last_error() !== JSON_ERROR_NONE) {
17+
throw new Exception('Failed to decode JSON response');
18+
}
19+
} elseif (is_array($response) || is_object($response)) {
20+
$this->responseObject = (object)$response;
21+
} else {
22+
throw new Exception('Unsupported response type');
23+
}
24+
$this->setData();
25+
}
26+
27+
private function setData()
28+
{
29+
$this->data = (array)$this->responseObject;
30+
}
31+
}

src/Entity/ParseFullName.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Supasta\FnsService\Entity;
4+
5+
use Supasta\FnsService\Contract\FullName;
6+
7+
class ParseFullName extends FullName
8+
{
9+
/**
10+
* FullName constructor.
11+
*
12+
* @param string $fullName The full name of the person.
13+
*/
14+
public function __construct(string $fullName)
15+
{
16+
$fullNameArray = explode(' ', $fullName);
17+
$this->lastName = $fullNameArray[0];
18+
$this->firstName = $fullNameArray[1];
19+
$this->patronymic = $this->extractPatronymic($fullNameArray);
20+
}
21+
22+
/**
23+
* Extract the patronymic part from the full name array.
24+
*
25+
* @param array $fullNameArray The array of full name parts.
26+
* @return string|null The patronymic part or null if not present.
27+
*/
28+
private function extractPatronymic(array $fullNameArray): ?string
29+
{
30+
$patronymicParts = array_slice($fullNameArray, 2);
31+
if (!empty($patronymicParts)) {
32+
return implode(' ', $patronymicParts);
33+
}
34+
return null;
35+
}
36+
}

0 commit comments

Comments
 (0)