Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiPostcode.php
More file actions
Latest commit
127 lines (110 loc) · 3.44 KB
/
Copy pathApiPostcode.php
File metadata and controls
127 lines (110 loc) · 3.44 KB
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
<?php
namespaceDevMob\Postcodes\Providers\ApiPostcode;
useDevMob\Postcodes\Exceptions\BadRequestException;
useDevMob\Postcodes\Exceptions\BadResponseException;
useDevMob\Postcodes\Exceptions\JsonException;
useDevMob\Postcodes\Exceptions\NoSuchCombinationException;
useDevMob\Postcodes\Providers\HttpProvider;
useDevMob\Postcodes\Util\Json;
useDevMob\Postcodes\Util\Uri;
useGuzzleHttp\Psr7\Request;
useInvalidArgumentException;
usePsr\Http\Message\RequestInterface;
usePsr\Http\Message\ResponseInterface;
/**
* @link https://api-postcode.nl/documentation.html
*/
class ApiPostcode extends HttpProvider
{
protectedconstHOST = 'http://json.api-postcode.nl';
/**
* @var array
*/
private$config;
/**
* @var \DevMob\Postcodes\Providers\ApiPostcode\AddressFactory
*/
private$addressFactory;
/**
* Constructor.
*
* @param array $config
* @param \DevMob\Postcodes\Providers\ApiPostcode\AddressFactory|null $addressFactory
*/
publicfunction__construct(array$config, ?AddressFactory$addressFactory = null)
{
$this->validateConfig($config);
$this->config = $config;
$this->addressFactory = $addressFactory ?: newAddressFactory();
}
/**
* Create http lookup request.
*
* @param array $input
* @return \Psr\Http\Message\RequestInterface
*/
protectedfunctionrequest(array$input): RequestInterface
{
$query = [
'postcode' => $input['postcode'],
'number' => $input['number']
];
$uri = Uri::create(self::HOST, $query);
$headers = [
'token' => $this->config['token'],
];
returnnewRequest('GET', $uri, $headers);
}
/**
* Parse http lookup response.
*
* @param \Psr\Http\Message\ResponseInterface $response
* @param array $input
* @return \DevMob\Postcodes\Address\Address[]
* @throws \DevMob\Postcodes\Exceptions\NoSuchCombinationException
* @throws \DevMob\Postcodes\Exceptions\ProviderException
*/
protectedfunctionparse(ResponseInterface$response, array$input): array
{
switch ($response->getStatusCode()) {
case400:
case401:
thrownewBadRequestException($this->getError($response));
case404:
thrownewNoSuchCombinationException($input['postcode'], $input['number']);
}
try {
$decoded = Json::decode($response);
} catch (JsonException$e) {
thrownewBadResponseException('Received a malformed JSON response.', $response, $e);
}
return [$this->addressFactory->create($decoded)];
}
/**
* Validate configuration.
*
* @param array $config
* @return void
*/
privatefunctionvalidateConfig(array$config): void
{
if (! isset($config['token'])) {
thrownewInvalidArgumentException('Configuration is missing the \'token\' key.');
}
}
/**
* Get the error message from the response.
*
* @param \Psr\Http\Message\ResponseInterface $response
* @return string
*/
privatefunctiongetError(ResponseInterface$response): string
{
$default = 'Unknown error: no error message in response.';
try {
return Json::decode($response)['error'] ?? $default;
} catch (JsonException$e) {
return$default;
}
}
}