-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiPostcode.php
More file actions
127 lines (110 loc) · 3.44 KB
/
ApiPostcode.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
namespace DevMob\Postcodes\Providers\ApiPostcode;
use DevMob\Postcodes\Exceptions\BadRequestException;
use DevMob\Postcodes\Exceptions\BadResponseException;
use DevMob\Postcodes\Exceptions\JsonException;
use DevMob\Postcodes\Exceptions\NoSuchCombinationException;
use DevMob\Postcodes\Providers\HttpProvider;
use DevMob\Postcodes\Util\Json;
use DevMob\Postcodes\Util\Uri;
use GuzzleHttp\Psr7\Request;
use InvalidArgumentException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* @link https://api-postcode.nl/documentation.html
*/
class ApiPostcode extends HttpProvider
{
protected const HOST = '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
*/
public function __construct(array $config, ?AddressFactory $addressFactory = null)
{
$this->validateConfig($config);
$this->config = $config;
$this->addressFactory = $addressFactory ?: new AddressFactory();
}
/**
* Create http lookup request.
*
* @param array $input
* @return \Psr\Http\Message\RequestInterface
*/
protected function request(array $input): RequestInterface
{
$query = [
'postcode' => $input['postcode'],
'number' => $input['number']
];
$uri = Uri::create(self::HOST, $query);
$headers = [
'token' => $this->config['token'],
];
return new Request('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
*/
protected function parse(ResponseInterface $response, array $input): array
{
switch ($response->getStatusCode()) {
case 400:
case 401:
throw new BadRequestException($this->getError($response));
case 404:
throw new NoSuchCombinationException($input['postcode'], $input['number']);
}
try {
$decoded = Json::decode($response);
} catch (JsonException $e) {
throw new BadResponseException('Received a malformed JSON response.', $response, $e);
}
return [$this->addressFactory->create($decoded)];
}
/**
* Validate configuration.
*
* @param array $config
* @return void
*/
private function validateConfig(array $config): void
{
if (! isset($config['token'])) {
throw new InvalidArgumentException('Configuration is missing the \'token\' key.');
}
}
/**
* Get the error message from the response.
*
* @param \Psr\Http\Message\ResponseInterface $response
* @return string
*/
private function getError(ResponseInterface $response): string
{
$default = 'Unknown error: no error message in response.';
try {
return Json::decode($response)['error'] ?? $default;
} catch (JsonException $e) {
return $default;
}
}
}