Passed
Pull Request — master (#5)
by Sergey
02:15
created

Address::searchSettlements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
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 $ref
66
     * @param string $streetRef
67
     * @param string $building
68
     * @param string $flat
69
     * @param string|null $note
70
     * @return array
71
     * @throws NovaPoshtaApiException
72
     */
73
    public function save(string $ref, string $streetRef, string $building, string $flat, string $note = null): array
74
    {
75
        if ($note && strlen($note) > 40) {
76
            throw new RuntimeException('Note exceeds the limit of 40 symbols');
77
        }
78
        $params = array_filter([
79
            'CounterpartyRef' => $ref,
80
            'StreetRef' => $streetRef,
81
            'BuildingNumber' => $building,
82
            'Flat' => $flat,
83
            'Note' => $note
84
        ]);
85
        $result = $this->connection->post(self::MODEL_NAME, 'save', $params);
86
        return array_shift($result);
87
    }
88
89
    /**
90
     * @see https://developers.novaposhta.ua/view/model/a0cf0f5f-8512-11ec-8ced-005056b2dbe1/method/a19ba934-8512-11ec-8ced-005056b2dbe1
91
     * @param string $addressRef
92
     * @param array $address
93
     * @param string|null $note
94
     * @return array
95
     * @throws NovaPoshtaApiException
96
     */
97
    public function update(string $addressRef, array $address, string $note = null): array
98
    {
99
        if ($note && strlen($note) > 40) {
100
            throw new RuntimeException('Note exceeds the limit of 40 symbols');
101
        }
102
        $params = array_filter([
103
            'Ref' => $addressRef,
104
            'CounterpartyRef' => $address['CounterpartyRef'],
105
            'StreetRef' => $address['StreetRef'],
106
            'BuildingNumber' => $address['BuildingNumber'],
107
            'Flat' => $address['Flat'],
108
            'Note' => $note
109
        ]);
110
        $result = $this->connection->post(self::MODEL_NAME, 'update', $params);
111
        return array_shift($result);
112
    }
113
114
    /**
115
     * @see https://developers.novaposhta.ua/view/model/a0cf0f5f-8512-11ec-8ced-005056b2dbe1/method/a177069a-8512-11ec-8ced-005056b2dbe1
116
     * @param string $addressRef
117
     * @return void
118
     * @throws NovaPoshtaApiException
119
     */
120
    public function delete(string $addressRef): void
121
    {
122
        $this->connection->post(self::MODEL_NAME, 'delete', ['Ref' => $addressRef]);
123
    }
124
125
    /**
126
     * @see https://developers.novaposhta.ua/view/model/a0cf0f5f-8512-11ec-8ced-005056b2dbe1/method/a1e6f0a7-8512-11ec-8ced-005056b2dbe1
127
     * @param int $page
128
     * @param int $limit
129
     * @param string|null $search
130
     * @return array
131
     * @throws NovaPoshtaApiException
132
     */
133
    public function getCities(int $page = 1, int $limit = PHP_INT_MAX, string $search = null): array
134
    {
135
        $params = array_filter([
136
            'Page' => $page,
137
            'Limit' => $limit,
138
            'FindByString' => $search,
139
        ]);
140
        return $this->connection->post(self::MODEL_NAME, 'getCities', $params);
141
    }
142
143
    /**
144
     * @see https://developers.novaposhta.ua/view/model/a0cf0f5f-8512-11ec-8ced-005056b2dbe1/method/a2322f38-8512-11ec-8ced-005056b2dbe1
145
     * @param array $filters Available filters are: CityName, CityRef, TypeOfWarehouseRef, WarehouseId
146
     * @param int $page
147
     * @param int $limit
148
     * @param string $lang
149
     * @return array
150
     * @throws NovaPoshtaApiException
151
     */
152
    public function getWarehouses(array $filters = [], int $page = 1, int $limit = 1000, string $lang = 'UA'): array
153
    {
154
        $params = array_filter([
155
            'CityName' => $filters['CityName'] ?? null,
156
            'CityRef' => $filters['CityRef'] ?? null,
157
            'TypeOfWarehouseRef' => $filters['TypeOfWarehouseRef'] ?? null,
158
            'WarehouseId' => $filters['WarehouseId'] ?? null,
159
            'Page' => $page,
160
            'Limit' => $limit,
161
            'Language' => $lang,
162
        ]);
163
        return $this->connection->post(self::MODEL_NAME, 'getWarehouses', $params);
164
    }
165
}
166