1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SergeyNezbritskiy\NovaPoshta\Models; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
use SergeyNezbritskiy\NovaPoshta\Connection; |
9
|
|
|
use SergeyNezbritskiy\NovaPoshta\ModelInterface; |
10
|
|
|
use SergeyNezbritskiy\NovaPoshta\NovaPoshtaApiException; |
11
|
|
|
|
12
|
|
|
class Address implements ModelInterface |
13
|
|
|
{ |
14
|
|
|
private const MODEL_NAME = 'Address'; |
15
|
|
|
private Connection $connection; |
16
|
|
|
|
17
|
3 |
|
/** |
18
|
|
|
* @param Connection $connection |
19
|
3 |
|
*/ |
20
|
|
|
public function __construct(Connection $connection) |
21
|
|
|
{ |
22
|
|
|
$this->connection = $connection; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @see https://developers.novaposhta.ua/view/model/a0cf0f5f-8512-11ec-8ced-005056b2dbe1/method/a0eb83ab-8512-11ec-8ced-005056b2dbe1 |
27
|
|
|
* @param string $cityName |
28
|
|
|
* @param int $page |
29
|
1 |
|
* @param int $limit |
30
|
|
|
* @return array |
31
|
1 |
|
* @throws NovaPoshtaApiException |
32
|
1 |
|
*/ |
33
|
1 |
|
public function searchSettlements(string $cityName, int $page = 1, int $limit = PHP_INT_MAX): array |
34
|
1 |
|
{ |
35
|
1 |
|
$params = array_filter([ |
36
|
1 |
|
'CityName' => $cityName, |
37
|
|
|
'Page' => $page, |
38
|
|
|
'Limit' => $limit, |
39
|
|
|
]); |
40
|
|
|
$result = $this->connection->post(self::MODEL_NAME, 'searchSettlements', $params); |
41
|
|
|
return array_shift($result); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @see https://developers.novaposhta.ua/view/model/a0cf0f5f-8512-11ec-8ced-005056b2dbe1/method/a1329635-8512-11ec-8ced-005056b2dbe1 |
46
|
|
|
* @param string $streetName |
47
|
1 |
|
* @param string $settlementRef |
48
|
|
|
* @param int|null $limit |
49
|
1 |
|
* @return array |
50
|
1 |
|
* @throws NovaPoshtaApiException |
51
|
1 |
|
*/ |
52
|
1 |
|
public function searchSettlementStreets(string $streetName, string $settlementRef, int $limit = null): array |
53
|
1 |
|
{ |
54
|
1 |
|
$params = array_filter([ |
55
|
1 |
|
'StreetName' => $streetName, |
56
|
1 |
|
'SettlementRef' => $settlementRef, |
57
|
1 |
|
'Limit' => $limit, |
58
|
1 |
|
]); |
59
|
|
|
$result = $this->connection->post(self::MODEL_NAME, 'searchSettlementStreets', $params); |
60
|
|
|
return array_shift($result); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @see https://developers.novaposhta.ua/view/model/a0cf0f5f-8512-11ec-8ced-005056b2dbe1/method/a155d0d9-8512-11ec-8ced-005056b2dbe1 |
65
|
|
|
* @param string $counterpartyRef |
66
|
|
|
* @param array $address |
67
|
|
|
* @param string|null $note |
68
|
|
|
* @return array |
69
|
|
|
* @throws NovaPoshtaApiException |
70
|
|
|
*/ |
71
|
|
|
public function save(string $counterpartyRef, array $address, string $note = null): array |
72
|
|
|
{ |
73
|
|
|
if ($note && strlen($note) > 40) { |
74
|
|
|
throw new RuntimeException('Note exceeds the limit of 40 symbols'); |
75
|
|
|
} |
76
|
|
|
$params = array_filter([ |
77
|
|
|
'CounterpartyRef' => $counterpartyRef, |
78
|
|
|
'StreetRef' => $address['StreetRef'], |
79
|
|
|
'BuildingNumber' => $address['BuildingNumber'], |
80
|
|
|
'Flat' => $address['Flat'], |
81
|
|
|
'Note' => $note |
82
|
|
|
]); |
83
|
|
|
$result = $this->connection->post(self::MODEL_NAME, 'save', $params); |
84
|
|
|
return array_shift($result); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @see https://developers.novaposhta.ua/view/model/a0cf0f5f-8512-11ec-8ced-005056b2dbe1/method/a19ba934-8512-11ec-8ced-005056b2dbe1 |
89
|
|
|
* @param array $address |
90
|
|
|
* @param string|null $note |
91
|
|
|
* @return array |
92
|
|
|
* @throws NovaPoshtaApiException |
93
|
|
|
*/ |
94
|
|
|
public function update(array $address, string $note = null): array |
95
|
|
|
{ |
96
|
|
|
if ($note && strlen($note) > 40) { |
97
|
|
|
throw new RuntimeException('Note exceeds the limit of 40 symbols'); |
98
|
|
|
} |
99
|
|
|
$params = array_filter([ |
100
|
|
|
'Ref' => $address['Ref'] ?? null, |
101
|
|
|
'CounterpartyRef' => $address['CounterpartyRef'] ?? null, |
102
|
|
|
'StreetRef' => $address['StreetRef'] ?? null, |
103
|
|
|
'BuildingNumber' => $address['BuildingNumber'] ?? null, |
104
|
|
|
'Flat' => $address['Flat'], |
105
|
|
|
'Note' => $note |
106
|
|
|
]); |
107
|
|
|
$result = $this->connection->post(self::MODEL_NAME, 'update', $params); |
108
|
|
|
return array_shift($result); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @see https://developers.novaposhta.ua/view/model/a0cf0f5f-8512-11ec-8ced-005056b2dbe1/method/a177069a-8512-11ec-8ced-005056b2dbe1 |
113
|
|
|
* @param string $addressRef |
114
|
|
|
* @return void |
115
|
|
|
* @throws NovaPoshtaApiException |
116
|
|
|
*/ |
117
|
|
|
public function delete(string $addressRef): void |
118
|
|
|
{ |
119
|
|
|
$this->connection->post(self::MODEL_NAME, 'delete', ['Ref' => $addressRef]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* https://developers.novaposhta.ua/view/model/a0cf0f5f-8512-11ec-8ced-005056b2dbe1/method/a1c42723-8512-11ec-8ced-005056b2dbe1 |
124
|
|
|
* @param array $filters |
125
|
|
|
* @param bool $warehouse |
126
|
|
|
* @param int|null $page |
127
|
|
|
* @param int $limit |
128
|
|
|
* @return array |
129
|
|
|
* @throws NovaPoshtaApiException |
130
|
|
|
*/ |
131
|
|
|
public function getSettlements(array $filters, bool $warehouse, int $page = null, int $limit = 150): array |
132
|
|
|
{ |
133
|
|
|
$params = array_filter([ |
134
|
|
|
'AreaRef' => $filters['AreaRef'] ?? null, |
135
|
|
|
'Ref' => $filters['Ref'] ?? null, |
136
|
|
|
'RegionRef' => $filters['RegionRef'] ?? null, |
137
|
|
|
'FindByString' => $filters['FindByString'] ?? null, |
138
|
|
|
'Warehouse' => (int)$warehouse, |
139
|
|
|
'Page' => $page, |
140
|
|
|
'Limit' => $limit, |
141
|
|
|
]); |
142
|
|
|
return $this->connection->post('AddressGeneral', 'getSettlements', $params); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @see https://developers.novaposhta.ua/view/model/a0cf0f5f-8512-11ec-8ced-005056b2dbe1/method/a1e6f0a7-8512-11ec-8ced-005056b2dbe1 |
147
|
|
|
* @param int $page |
148
|
|
|
* @param int $limit |
149
|
|
|
* @param string|null $search |
150
|
|
|
* @return array |
151
|
|
|
* @throws NovaPoshtaApiException |
152
|
|
|
*/ |
153
|
|
|
public function getCities(int $page = 1, int $limit = PHP_INT_MAX, string $search = null): array |
154
|
|
|
{ |
155
|
|
|
$params = array_filter([ |
156
|
|
|
'Page' => $page, |
157
|
|
|
'Limit' => $limit, |
158
|
|
|
'FindByString' => $search, |
159
|
|
|
]); |
160
|
|
|
return $this->connection->post(self::MODEL_NAME, 'getCities', $params); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @see https://developers.novaposhta.ua/view/model/a0cf0f5f-8512-11ec-8ced-005056b2dbe1/method/a20ee6e4-8512-11ec-8ced-005056b2dbe1 |
165
|
|
|
* @throws NovaPoshtaApiException |
166
|
|
|
*/ |
167
|
|
|
public function getAreas(): array |
168
|
|
|
{ |
169
|
|
|
return $this->connection->post(self::MODEL_NAME, 'getAreas'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @see https://developers.novaposhta.ua/view/model/a0cf0f5f-8512-11ec-8ced-005056b2dbe1/method/a2322f38-8512-11ec-8ced-005056b2dbe1 |
174
|
|
|
* @param array $filters Available filters are: CityName, CityRef, TypeOfWarehouseRef, WarehouseId |
175
|
|
|
* @param int $page |
176
|
|
|
* @param int $limit |
177
|
|
|
* @param string $lang |
178
|
|
|
* @return array |
179
|
|
|
* @throws NovaPoshtaApiException |
180
|
|
|
*/ |
181
|
|
|
public function getWarehouses(array $filters = [], int $page = 1, int $limit = 1000, string $lang = 'UA'): array |
182
|
|
|
{ |
183
|
|
|
$params = array_filter([ |
184
|
|
|
'CityName' => $filters['CityName'] ?? null, |
185
|
|
|
'CityRef' => $filters['CityRef'] ?? null, |
186
|
|
|
'TypeOfWarehouseRef' => $filters['TypeOfWarehouseRef'] ?? null, |
187
|
|
|
'WarehouseId' => $filters['WarehouseId'] ?? null, |
188
|
|
|
'Page' => $page, |
189
|
|
|
'Limit' => $limit, |
190
|
|
|
'Language' => $lang, |
191
|
|
|
]); |
192
|
|
|
return $this->connection->post(self::MODEL_NAME, 'getWarehouses', $params); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @see https://developers.novaposhta.ua/view/model/a0cf0f5f-8512-11ec-8ced-005056b2dbe1/method/a2587b53-8512-11ec-8ced-005056b2dbe1 |
197
|
|
|
* @throws NovaPoshtaApiException |
198
|
|
|
*/ |
199
|
|
|
public function getWarehouseTypes(): array |
200
|
|
|
{ |
201
|
|
|
return $this->connection->post(self::MODEL_NAME, 'getWarehouseTypes'); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @see https://developers.novaposhta.ua/view/model/a0cf0f5f-8512-11ec-8ced-005056b2dbe1/method/a27c20d7-8512-11ec-8ced-005056b2dbe1 |
206
|
|
|
* @param string $cityRef |
207
|
|
|
* @param string $findByString |
208
|
|
|
* @param int|null $page |
209
|
|
|
* @param int|null $limit |
210
|
|
|
* @return array |
211
|
|
|
* @throws NovaPoshtaApiException |
212
|
|
|
*/ |
213
|
|
|
public function getStreet(string $cityRef, string $findByString, int $page = null, int $limit = null): array |
214
|
|
|
{ |
215
|
|
|
$params = array_filter([ |
216
|
|
|
'CityRef' => $cityRef, |
217
|
|
|
'FindByString' => $findByString, |
218
|
|
|
'Page' => $page, |
219
|
|
|
'Limit' => $limit, |
220
|
|
|
]); |
221
|
|
|
return $this->connection->post(self::MODEL_NAME, 'getStreet', $params); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|