-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEgrp365.php
165 lines (144 loc) · 4.24 KB
/
Egrp365.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace egrp365\egrp365api;
use Exception;
use yii\base\Component;
use yii\httpclient\Client;
/**
* Class Egrp365
* @package egrp365\egrp365api
*/
class Egrp365 extends Component
{
/** @var string */
public $apiKey = '';
/** @var string */
public $userAgent = 'yii-egrp365-client';
/** @var string */
protected $baseURL = 'https://egrp365.ru/api/v2/';
/**
* @inheritdoc
* @throws Exception
*/
public function init()
{
if (!$this->apiKey) {
throw new Exception('Не установлен индивидуальный ключ пользователя');
}
parent::init();
}
/**
* @param $method string
* @param $apiMethod string
* @param array $data
* @return array
* @throws Exception
*/
protected function _send($method, $apiMethod, array $data = [])
{
$response = (new Client())->createRequest()
->setMethod($method)
->setData(array_merge(['apiKey' => $this->apiKey], $data))
->setHeaders([
'Accept' => 'application/json, text/javascript',
'User-Agent' => $this->userAgent
])
->setUrl($this->baseURL . $apiMethod)
->send();
if (!$response->getIsOk()) {
throw new Exception('Ошибка получения данных');
}
$data = $response->getData();
if (isset($data['error']) && isset($data['error'][1])) {
throw new Exception($data['error'][1]);
}
return $response->getData();
}
/**
* @param array $required
* @param array $params
* @throws Exception
*/
protected function _checkRequired(array $required, array $params)
{
$errorFields = array_diff($required, array_keys($params));
if (!empty($errorFields)) {
throw new Exception('Отсутствуют обязательные поля: ' . implode(', ', $errorFields));
}
}
/**
* @return array
* @throws Exception
*/
public function getDocs()
{
return $this->_send('get', 'getDocs');
}
/**
* @param string $kadnum
* @param null|string $reestr
* @return array
* @throws Exception
*/
public function getObjectsByKadnum(string $kadnum, $reestr = null)
{
$params = [
'kadnum' => $kadnum,
'reestr' => $reestr,
];
return $this->_send('GET', 'getObjectsByKadnum', $params);
}
/**
* @param array $params
* @return array
* @throws Exception
*/
public function getObjectsByAddress(array $params)
{
$required = ['region', 'street', 'house'];
$this->_checkRequired($required, $params);
return $this->_send('GET', 'getObjectsByAddress', $params);
}
/**
* @param string $objectid
* @return array
* @throws Exception
*/
public function getInfoByObjectId(string $objectid)
{
if (!preg_match('#^([0-9\-\:\/\ \_\*]+)$#', $objectid)) {
throw new Exception('Неверный формат id объекта');
}
return $this->_send('GET', 'getInfoByObjectId', ['objectid' => $objectid]);
}
/**
* @param array $params
* @return array
* @throws Exception
*/
public function postOrder(array $params)
{
$required = ['kadnum', 'objectid', 'email', 'phone'];
$this->_checkRequired($required, $params);
if (!preg_match('#^([0-9\-\:\/\ \_\*]+)$#', $params['objectid'])) {
throw new Exception('Неверный формат id объекта');
}
if (!preg_match('#^([0-9\-\:\/\ \_\*]+)$#', $params['kadnum'])) {
throw new Exception('Неверный формат кадастрового номера');
}
return $this->_send('POST', 'postOrder', $params);
}
/**
* @param int $orderid
* @param string $email
* @return array
* @throws Exception
*/
public function getOrderStatus(int $orderid, string $email)
{
$params = [
'orderid' => $orderid,
'email' => $email,
];
return $this->_send('GET', 'getOrderStatus', $params);
}
}