1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* (c) Wessel Strengholt <[email protected]> |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Usoft\PostcodeBundle\Services; |
10
|
|
|
|
11
|
|
|
use GuzzleHttp\ClientInterface; |
12
|
|
|
use GuzzleHttp\Psr7\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
use Usoft\PostcodeBundle\Exceptions\InvalidApiResponseException; |
15
|
|
|
use Usoft\PostcodeBundle\Exceptions\InvalidPostcodeException; |
16
|
|
|
use Usoft\PostcodeBundle\Model\Address; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class AddressClient |
20
|
|
|
* |
21
|
|
|
* @author Wessel Strengholt <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class AddressClient |
24
|
|
|
{ |
25
|
|
|
/** @var ClientInterface */ |
26
|
|
|
private $client; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
private $apiKey; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param ClientInterface $client |
33
|
|
|
* @param string $apiKey |
34
|
|
|
*/ |
35
|
|
|
public function __construct(ClientInterface $client, $apiKey) |
36
|
|
|
{ |
37
|
|
|
$this->client = $client; |
38
|
|
|
$this->apiKey = $apiKey; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $postcode |
43
|
|
|
* @param integer $houseNumber |
44
|
|
|
* |
45
|
|
|
* @throws InvalidPostcodeException |
46
|
|
|
* @throws InvalidApiResponseException |
47
|
|
|
* |
48
|
|
|
* @return Address |
49
|
|
|
*/ |
50
|
|
|
public function getAddress($postcode, $houseNumber) |
51
|
|
|
{ |
52
|
|
|
if (0 === preg_match('/^[1-9]{1}[0-9]{3}[\s]{0,1}[a-z]{2}$/i', $postcode)) { |
53
|
|
|
throw new InvalidPostcodeException('Given postcode incorrect'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$header = ['X-Api-Key' => $this->apiKey]; |
57
|
|
|
$url = sprintf('https://postcode-api.apiwise.nl/v2/addresses/?postcode=%s&number=%d', $postcode, (int) $houseNumber); |
58
|
|
|
$request = new Request('GET', $url, $header); |
59
|
|
|
|
60
|
|
|
try { |
61
|
|
|
$response = $this->client->send($request); |
62
|
|
|
|
63
|
|
|
if ($response->getStatusCode() !== Response::HTTP_OK) { |
64
|
|
|
throw new InvalidApiResponseException('The API does not return a 200 status-code'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$data = json_decode($response->getBody()->getContents(), true); |
68
|
|
|
|
69
|
|
|
if (false === isset($data['_embedded']['addresses'][0])) { |
70
|
|
|
throw new InvalidApiResponseException('Address cannot be set from API data'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$address = $data['_embedded']['addresses'][0]; |
74
|
|
|
|
75
|
|
|
$city = $address['city']['label']; |
76
|
|
|
$municipality = $address['municipality']['label']; |
77
|
|
|
$street = $address['street']; |
78
|
|
|
$province = $address['province']['label']; |
79
|
|
|
|
80
|
|
|
$geoLocation = [ |
81
|
|
|
'longitude' => isset($address['geo']['center']['wgs84']['coordinates'][1]) ? $address['geo']['center']['wgs84']['coordinates'][0] : null, |
82
|
|
|
'latitude' => isset($address['geo']['center']['wgs84']['coordinates'][0]) ? $address['geo']['center']['wgs84']['coordinates'][1] : null, |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
$address = new Address($street, $postcode, $city, $houseNumber, $province, $municipality); |
86
|
|
|
$address->setGeoLocation($geoLocation); |
87
|
|
|
|
88
|
|
|
return $address; |
89
|
|
|
|
90
|
|
|
} catch (\Exception $exception) { |
91
|
|
|
throw new InvalidApiResponseException($exception->getMessage()); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|