|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SergeyNezbritskiy\NovaPoshta; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
9
|
|
|
use SergeyNezbritskiy\NovaPoshta\Models\Address; |
|
10
|
|
|
use SergeyNezbritskiy\NovaPoshta\Models\Common; |
|
11
|
|
|
use SergeyNezbritskiy\NovaPoshta\Models\ContactPerson; |
|
12
|
|
|
use SergeyNezbritskiy\NovaPoshta\Models\Counterparty; |
|
13
|
|
|
use SergeyNezbritskiy\NovaPoshta\Models\InternetDocument; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class Client |
|
17
|
|
|
* |
|
18
|
|
|
* Class-connector with NovaPoshta API |
|
19
|
|
|
* |
|
20
|
|
|
* @property Address $address |
|
21
|
|
|
* @property Counterparty $counterparty |
|
22
|
|
|
* @property ContactPerson $contactPerson |
|
23
|
|
|
* @property ContactPerson $common |
|
24
|
|
|
* @property ContactPerson $internetDocument |
|
25
|
|
|
* @see https://developers.novaposhta.ua/documentation |
|
26
|
|
|
*/ |
|
27
|
|
|
class Client |
|
28
|
|
|
{ |
|
29
|
|
|
private const array MODELS_MAP = [ |
|
|
|
|
|
|
30
|
|
|
'address' => Address::class, |
|
31
|
2 |
|
'counterparty' => Counterparty::class, |
|
32
|
|
|
'contactPerson' => ContactPerson::class, |
|
33
|
2 |
|
'common' => Common::class, |
|
34
|
|
|
'internetDocument' => InternetDocument::class, |
|
35
|
|
|
]; |
|
36
|
2 |
|
|
|
37
|
|
|
private string $apiKey; |
|
38
|
2 |
|
private Connection $connection; |
|
39
|
1 |
|
|
|
40
|
|
|
/** |
|
41
|
1 |
|
* @param string $apiKey |
|
42
|
1 |
|
*/ |
|
43
|
1 |
|
public function __construct(string $apiKey) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->apiKey = $apiKey; |
|
46
|
1 |
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
/** |
|
49
|
1 |
|
* @throws Exception |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function __get(string $property): ModelInterface |
|
52
|
|
|
{ |
|
53
|
|
|
if (!array_key_exists($property, self::MODELS_MAP)) { |
|
54
|
|
|
throw new Exception(sprintf('Model `%s` not supported by Nova Poshta API Client', $property)); |
|
55
|
|
|
} |
|
56
|
|
|
$class = self::MODELS_MAP[$property]; |
|
57
|
|
|
$this->$property = new $class($this->getConnection()); |
|
58
|
|
|
return $this->$property; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function getConnection(): Connection |
|
62
|
|
|
{ |
|
63
|
|
|
if (empty($this->connection)) { |
|
64
|
|
|
$this->connection = new Connection($this->apiKey, new HttpClient()); |
|
65
|
|
|
} |
|
66
|
|
|
return $this->connection; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|