Passed
Push — master ( 77fbef...5b00d5 )
by Sergey
02:50 queued 35s
created

Address::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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'],
101
            'CounterpartyRef' => $address['CounterpartyRef'],
102
            'StreetRef' => $address['StreetRef'],
103
            'BuildingNumber' => $address['BuildingNumber'],
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
     * @see https://developers.novaposhta.ua/view/model/a0cf0f5f-8512-11ec-8ced-005056b2dbe1/method/a1e6f0a7-8512-11ec-8ced-005056b2dbe1
124
     * @param int $page
125
     * @param int $limit
126
     * @param string|null $search
127
     * @return array
128
     * @throws NovaPoshtaApiException
129
     */
130
    public function getCities(int $page = 1, int $limit = PHP_INT_MAX, string $search = null): array
131
    {
132
        $params = array_filter([
133
            'Page' => $page,
134
            'Limit' => $limit,
135
            'FindByString' => $search,
136
        ]);
137
        return $this->connection->post(self::MODEL_NAME, 'getCities', $params);
138
    }
139
140
    /**
141
     * @see https://developers.novaposhta.ua/view/model/a0cf0f5f-8512-11ec-8ced-005056b2dbe1/method/a2322f38-8512-11ec-8ced-005056b2dbe1
142
     * @param array $filters Available filters are: CityName, CityRef, TypeOfWarehouseRef, WarehouseId
143
     * @param int $page
144
     * @param int $limit
145
     * @param string $lang
146
     * @return array
147
     * @throws NovaPoshtaApiException
148
     */
149
    public function getWarehouses(array $filters = [], int $page = 1, int $limit = 1000, string $lang = 'UA'): array
150
    {
151
        $params = array_filter([
152
            'CityName' => $filters['CityName'] ?? null,
153
            'CityRef' => $filters['CityRef'] ?? null,
154
            'TypeOfWarehouseRef' => $filters['TypeOfWarehouseRef'] ?? null,
155
            'WarehouseId' => $filters['WarehouseId'] ?? null,
156
            'Page' => $page,
157
            'Limit' => $limit,
158
            'Language' => $lang,
159
        ]);
160
        return $this->connection->post(self::MODEL_NAME, 'getWarehouses', $params);
161
    }
162
}
163