Skip to content

Commit af1104e

Browse files
author
Артём Соколовский
committed
Suggestions
1 parent deed46b commit af1104e

File tree

5 files changed

+209
-0
lines changed

5 files changed

+209
-0
lines changed

src/BaseRequester.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace c7v\dadata;
4+
5+
use GuzzleHttp\Client;
6+
7+
abstract class BaseRequester
8+
{
9+
/**
10+
* @var Client Http client.
11+
*/
12+
protected static Client $_httpClient;
13+
14+
/**
15+
* @var array Http options.
16+
*/
17+
protected static array $_httpOptions;
18+
19+
/**
20+
* @param Client $client
21+
* @return void
22+
*/
23+
public static function setHttpClient(Client $client): void
24+
{
25+
self::$_httpClient = $client;
26+
}
27+
28+
/**
29+
* @param array $options
30+
* @return void
31+
*/
32+
public static function setHttpOptions(array $options): void
33+
{
34+
self::$_httpOptions = $options;
35+
}
36+
}

src/FindByIdBank.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace c7v\dadata;
4+
5+
use GuzzleHttp\RequestOptions;
6+
7+
class FindByIdBank extends BaseRequester
8+
{
9+
const METHOD_URL = '4_1/rs/findById/bank';
10+
11+
private array $_data;
12+
13+
public function __construct(string $query, string $kpp = null)
14+
{
15+
$this->_data = [];
16+
$this->_data['query'] = $query;
17+
if (!is_null($kpp)) {
18+
$this->_data['kpp'] = $kpp;
19+
}
20+
}
21+
22+
public function send(): \Psr\Http\Message\ResponseInterface
23+
{
24+
return self::$_httpClient->request('POST', self::METHOD_URL, array_merge(self::$_httpOptions, [
25+
RequestOptions::JSON => $this->_data,
26+
]));
27+
}
28+
}

src/FindByIdFnsUnit.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace c7v\dadata;
4+
5+
use GuzzleHttp\RequestOptions;
6+
7+
class FindByIdFnsUnit extends BaseRequester
8+
{
9+
const METHOD_URL = '4_1/rs/suggest/fns_unit';
10+
11+
private array $_data;
12+
13+
public function __construct(int $query)
14+
{
15+
$this->_data['query'] = $query;
16+
}
17+
18+
public function send(): \Psr\Http\Message\ResponseInterface
19+
{
20+
return self::$_httpClient->request('POST', self::METHOD_URL, array_merge(self::$_httpOptions, [
21+
RequestOptions::JSON => $this->_data,
22+
]));
23+
}
24+
}

src/FindByIdParty.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace c7v\dadata;
4+
5+
use GuzzleHttp\RequestOptions;
6+
7+
class FindByIdParty extends BaseRequester
8+
{
9+
const METHOD_URL = '4_1/rs/findById/party';
10+
11+
private array $_data;
12+
13+
public function __construct(string $inn)
14+
{
15+
$this->_data = [];
16+
$this->_data['query'] = $inn;
17+
}
18+
19+
public function send(): \Psr\Http\Message\ResponseInterface
20+
{
21+
return self::$_httpClient->request('POST', self::METHOD_URL, array_merge(self::$_httpOptions, [
22+
RequestOptions::JSON => $this->_data,
23+
]));
24+
}
25+
}

src/Suggestions.php

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
namespace c7v\dadata;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\RequestOptions;
7+
8+
final class Suggestions
9+
{
10+
/**
11+
* @var Client Http client.
12+
*/
13+
private Client $_httpClient;
14+
15+
/**
16+
* @var array Http options.
17+
*/
18+
private array $_httpOptions;
19+
20+
/**
21+
* @var string Токен доступа к API DaData.Ru
22+
*/
23+
private string $_accessToken;
24+
25+
/**
26+
* @param string $accessToken Токен доступа к API DaData.Ru
27+
*/
28+
public function __construct(string $accessToken, int $timeOut = 60)
29+
{
30+
/** @var string _accessToken */
31+
$this->_accessToken = $accessToken;
32+
33+
$this->_httpClient = new Client([
34+
'base_uri' => 'https://suggestions.dadata.ru/suggestions/api/',
35+
'timeout' => $timeOut,
36+
]);
37+
38+
$this->_httpOptions = [
39+
RequestOptions::HEADERS => [
40+
'Authorization' => 'Token ' . $this->_accessToken
41+
],
42+
];
43+
}
44+
45+
/**
46+
* Данный метод поможет переопределить access token.
47+
*
48+
* @param string $accessToken Токен доступа к API DaData.Ru
49+
* @return $this
50+
*/
51+
public function reSetAccessToken(string $accessToken): Suggestions
52+
{
53+
$this->_accessToken = $accessToken;
54+
55+
$this->_httpOptions = [
56+
RequestOptions::HEADERS => [
57+
'Authorization' => 'Token ' . $this->_accessToken
58+
],
59+
];
60+
return $this;
61+
}
62+
63+
/**
64+
* Получить данные по ИНН.
65+
* @return FindByIdParty
66+
*/
67+
public function requesterFindByIdParty(string $inn): FindByIdParty
68+
{
69+
$findById = new FindByIdParty($inn);
70+
71+
$findById::setHttpClient($this->_httpClient);
72+
$findById::setHttpOptions($this->_httpOptions);
73+
74+
return $findById;
75+
}
76+
77+
public function requesterFindByIdBank(string $query, string $kpp = null): FindByIdBank
78+
{
79+
$findById = new FindByIdBank($query, $kpp);
80+
81+
$findById::setHttpClient($this->_httpClient);
82+
$findById::setHttpOptions($this->_httpOptions);
83+
84+
return $findById;
85+
}
86+
87+
public function requesterFindByIdFnsUnit(int $query): FindByIdFnsUnit
88+
{
89+
$findById = new FindByIdFnsUnit($query);
90+
91+
$findById::setHttpClient($this->_httpClient);
92+
$findById::setHttpOptions($this->_httpOptions);
93+
94+
return $findById;
95+
}
96+
}

0 commit comments

Comments
 (0)