1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of the Geocoder package. |
||
7 | * For the full copyright and license information, please view the LICENSE |
||
8 | * file that was distributed with this source code. |
||
9 | * |
||
10 | * @license MIT License |
||
11 | */ |
||
12 | |||
13 | namespace Geocoder\Provider\Geopunt; |
||
14 | |||
15 | use Geocoder\Collection; |
||
16 | use Geocoder\Exception\InvalidArgument; |
||
17 | use Geocoder\Exception\InvalidServerResponse; |
||
18 | use Geocoder\Exception\UnsupportedOperation; |
||
19 | use Geocoder\Http\Provider\AbstractHttpProvider; |
||
20 | use Geocoder\Model\Address; |
||
21 | use Geocoder\Model\AddressBuilder; |
||
22 | use Geocoder\Model\AddressCollection; |
||
23 | use Geocoder\Provider\Provider; |
||
24 | use Geocoder\Query\GeocodeQuery; |
||
25 | use Geocoder\Query\ReverseQuery; |
||
26 | use Http\Client\HttpClient; |
||
27 | |||
28 | /** |
||
29 | * @author Jonathan Beliƫn <[email protected]> |
||
30 | */ |
||
31 | final class Geopunt extends AbstractHttpProvider implements Provider |
||
32 | { |
||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | const GEOCODE_ENDPOINT_URL = 'https://loc.geopunt.be/v4/Location?q=%s&c=%d'; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | const REVERSE_ENDPOINT_URL = 'https://loc.geopunt.be/v4/Location?latlon=%F,%F&c=%d'; |
||
42 | |||
43 | /** |
||
44 | * @param HttpClient $client an HTTP adapter |
||
45 | */ |
||
46 | 16 | public function __construct(HttpClient $client) |
|
47 | { |
||
48 | 16 | parent::__construct($client); |
|
49 | 16 | } |
|
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | 10 | public function geocodeQuery(GeocodeQuery $query): Collection |
|
55 | { |
||
56 | 10 | $address = $query->getText(); |
|
57 | // This API does not support IP |
||
58 | 10 | if (filter_var($address, FILTER_VALIDATE_IP)) { |
|
59 | 3 | throw new UnsupportedOperation('The Geopunt provider does not support IP addresses, only street addresses.'); |
|
60 | } |
||
61 | |||
62 | // Save a request if no valid address entered |
||
63 | 7 | if (empty($address)) { |
|
64 | throw new InvalidArgument('Address cannot be empty.'); |
||
65 | } |
||
66 | |||
67 | 7 | $url = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($address), $query->getLimit()); |
|
68 | 7 | $json = $this->executeQuery($url); |
|
69 | |||
70 | // no result |
||
71 | 2 | if (empty($json->LocationResult)) { |
|
72 | 1 | return new AddressCollection([]); |
|
73 | } |
||
74 | |||
75 | 1 | $results = []; |
|
76 | 1 | foreach ($json->LocationResult as $location) { |
|
77 | 1 | $streetName = !empty($location->Thoroughfarename) ? $location->Thoroughfarename : null; |
|
78 | 1 | $housenumber = !empty($location->Housenumber) ? $location->Housenumber : null; |
|
79 | 1 | $municipality = !empty($location->Municipality) ? $location->Municipality : null; |
|
80 | 1 | $zipcode = !empty($location->Zipcode) ? $location->Zipcode : null; |
|
81 | |||
82 | 1 | $builder = new AddressBuilder($this->getName()); |
|
83 | 1 | $builder->setCoordinates($location->Location->Lat_WGS84, $location->Location->Lon_WGS84) |
|
84 | 1 | ->setStreetNumber($housenumber) |
|
85 | 1 | ->setStreetName($streetName) |
|
86 | 1 | ->setLocality($municipality) |
|
87 | 1 | ->setPostalCode($zipcode) |
|
88 | 1 | ->setBounds( |
|
89 | 1 | $location->BoundingBox->LowerLeft->Lat_WGS84, |
|
90 | 1 | $location->BoundingBox->LowerLeft->Lon_WGS84, |
|
91 | 1 | $location->BoundingBox->UpperRight->Lat_WGS84, |
|
92 | 1 | $location->BoundingBox->UpperRight->Lon_WGS84 |
|
93 | ); |
||
94 | |||
95 | 1 | $results[] = $builder->build(); |
|
96 | } |
||
97 | |||
98 | 1 | return new AddressCollection($results); |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | 6 | public function reverseQuery(ReverseQuery $query): Collection |
|
105 | { |
||
106 | 6 | $coordinates = $query->getCoordinates(); |
|
107 | |||
108 | 6 | $url = sprintf(self::REVERSE_ENDPOINT_URL, $coordinates->getLatitude(), $coordinates->getLongitude(), $query->getLimit()); |
|
109 | 6 | $json = $this->executeQuery($url); |
|
110 | |||
111 | // no result |
||
112 | 1 | if (empty($json->LocationResult)) { |
|
113 | return new AddressCollection([]); |
||
114 | } |
||
115 | |||
116 | 1 | $results = []; |
|
117 | 1 | foreach ($json->LocationResult as $location) { |
|
118 | 1 | $streetName = !empty($location->Thoroughfarename) ? $location->Thoroughfarename : null; |
|
119 | 1 | $housenumber = !empty($location->Housenumber) ? $location->Housenumber : null; |
|
120 | 1 | $municipality = !empty($location->Municipality) ? $location->Municipality : null; |
|
121 | 1 | $zipcode = !empty($location->Zipcode) ? $location->Zipcode : null; |
|
122 | |||
123 | 1 | $builder = new AddressBuilder($this->getName()); |
|
124 | 1 | $builder->setCoordinates($location->Location->Lat_WGS84, $location->Location->Lon_WGS84) |
|
125 | 1 | ->setStreetNumber($housenumber) |
|
126 | 1 | ->setStreetName($streetName) |
|
127 | 1 | ->setLocality($municipality) |
|
128 | 1 | ->setPostalCode($zipcode) |
|
129 | 1 | ->setBounds( |
|
130 | 1 | $location->BoundingBox->LowerLeft->Lat_WGS84, |
|
131 | 1 | $location->BoundingBox->LowerLeft->Lon_WGS84, |
|
132 | 1 | $location->BoundingBox->UpperRight->Lat_WGS84, |
|
133 | 1 | $location->BoundingBox->UpperRight->Lon_WGS84 |
|
134 | ); |
||
135 | |||
136 | 1 | $results[] = $builder->build(); |
|
137 | } |
||
138 | |||
139 | 1 | return new AddressCollection($results); |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | 2 | public function getName(): string |
|
146 | { |
||
147 | 2 | return 'geopunt'; |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param string $url |
||
152 | * |
||
153 | * @return \stdClass |
||
154 | */ |
||
155 | 13 | private function executeQuery(string $url): \stdClass |
|
156 | { |
||
157 | 13 | $content = $this->getUrlContents($url); |
|
158 | 3 | $json = json_decode($content); |
|
159 | // API error |
||
160 | 3 | if (!isset($json)) { |
|
161 | throw InvalidServerResponse::create($url); |
||
162 | } |
||
163 | |||
164 | 3 | return $json; |
|
165 | } |
||
166 | } |
||
167 |